How to transform, for example, "0" to "ZERO" in C? What problem in this code?

Asked

Viewed 409 times

-1

I have to do a program for college whose main objective is another, but in one of the stages of the program, I need to pass two integer random numbers from 0 to 9 for their extensive written formats, in string. I wrote some excerpts from a code that would perform this, but I wasn’t very successful.

#include <time.h>
#include <stdlib.h>
#include <stdio.h>


int main(){
    int i=0, rand1, rand2;
    char num1[7], num2[7];

    printf("Bem vindo! Pronto pra começar? Vamos la!");

    srand(time(NULL));

    while (i<10){
          rand1=rand()%10;
          rand2=rand()%10;

           switch (rand1){
            case 0:
                 num1 = "ZERO";
            case 1:
                 num1 = "UM";
            case 2:
                 num1 = "DOIS";
            case 3:
                 num1 = "TRES";
            case 4:
                 num1 = "QUATRO"
            case 5:
                 num1 = "CINCO";
            case 6:
                 num1 = "SEIS";
            case 7:
                 num1 = "SETE";
            case 8:
                 num1 = "OITO";
            case 9:
                 num1 = "NOVE";
            }
          switch (rand2){
            case 0:
                 num2 = "ZERO";
            case 1:
                 num2 = "UM";
            case 2:
                 num2 = "DOIS";
            case 3:
                 num2 = "TRES";
            case 4:
                 num2 = "QUATRO"
            case 5:
                 num2 = "CINCO";
            case 6:
                 num2 = "SEIS";
            case 7:
                 num2 = "SETE";
            case 8:
                 num2 = "OITO";
            case 9:
                 num2 = "NOVE";
            }


          printf("Quanto e %s vezes %s?: ", num1, num2);

          i++;
          }
    system("pause");
    return 0;


}

Error occurs in string assignment num1 and num2 with the message

incompatible types in assignement of 'const char [5]' to '[char [7]'

  • Welcome to Sopt. You can start by writing the words of your text in full. It is not necessary to abbreviate, it has enough space to write the whole words. If you want to save typing, just do not put words that add anything to the understanding of what is being written. Use tags correct to get the attention of the right people. I fixed the problem of formatting your code. Use the icon {} in the editor to put code.

  • 1

    In addition to what has already been reported by colleagues in replies, your code has another little problem on commando switch. Like you didn’t use the break in each case, all will be executed in sequence and its variable will be with the value of last execution regardless of the value of rand1 (num1 = "NOVE", for example). So don’t forget the break, okay? :)

4 answers

8


You stated char num1[7]. It is an array with 7 characters. But in C there is no array assignment operation. So num1 = "ZERO" doesn’t work.

  1. Can transform num1 and a pointer to char only, so when do num1 = "ZERO" it will point to the static chars array. I recommend so since you do not intend to change the data in any way.

  2. Uses the function strcpy to copy the string to the array: strcpy(num1, "ZERO");. It works for what you want, but the copy is unnecessary since you will not change the data. A mere pointer suffices.


Unrelated, but a better way to write your code would be as follows:

const char* words[10] = {"ZERO", "UM", "DOIS", "TRES", "QUATRO", "CINCO",
                         "SEIS", "SETE", "OITO", "NOVE"};

char* num1 = words[rand1];

2

It is not possible to assign values to arrays. You need to assign array elements one by one. The C language has some shortcuts to facilitate this assignment when it comes to strings.

num1[0] = 'Z';
num1[1] = 'E';
num1[2] = 'R';
num1[3] = 'O';
num1[4] = '\0';

or, using the shortcut

strcpy(num1, "ZERO"); // lembra-te de incluir o header <string.h>

0

About build error already have good answers: you cannot assign values like this in C. int and char[7] with a text representing the int are very different things and there is no direct conversion.

And the case is wrong: missing the break;

Some time later, I will suggest the potentially much faster and much simpler

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    const char* numero[] =
    {
        "ZERO", "UM", "DOIS", "TRES", "QUATRO",
        "CINCO", "SEIS", "SETE", "OITO", "NOVE"
    };
    char num1[7] = {0};
    char num2[7] = {0};
    printf("Bem vindo! Pronto pra começar? Vamos la!\n");
    srand(210821);
    int total = 10;
    for(int i=1;i<=total;i+=1)
    {
        int rand1 = rand() % 100;
        strcpy(num1, numero[rand1 / 10]);
        strcpy(num2, numero[rand1 % 10]);
        printf("[%2d/%2d]  %s   e   %s\n", i, total, num1, num2);
    }
    return 0;
}

Which shows

Bem vindo! Pronto pra comeþar? Vamos la!
[ 1/10]  SEIS   e   TRES
[ 2/10]  SEIS   e   SEIS
[ 3/10]  UM   e   OITO
[ 4/10]  QUATRO   e   SEIS
[ 5/10]  ZERO   e   ZERO
[ 6/10]  TRES   e   DOIS
[ 7/10]  SEIS   e   SEIS
[ 8/10]  UM   e   ZERO
[ 9/10]  UM   e   OITO
[10/10]  SEIS   e   QUATRO

With no switch and a single call to rand().

0

In the C language you cannot assign a string to a vector simply using the assignment operator '='. This serial possible if using the C string type++.

However, there is a set of functions in C included in the header file among which I can quote strcpy that copies a string to a char vector. So in your comparison, if it’s number six, you’d do:

char str[256];
strcpy(str, "SEIS");

And you would now have the string saved in your vector. It copies the right string into its left vector provided it is large enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.