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

0

I have to make a college program whose main purpose is another one, but in one of the program steps, I need to pass two whole random numbers from 0 to 9 to their extensive written forms, in string . I wrote a few snippets of code that would do that, but I did not have much success.

#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;


}

The error occurs in the assignment of the string num1 and num2 with the message

  

Incompatible types in assignment of 'const char [5]' to '[char [7]'

    
asked by anonymous 24.05.2014 / 15:55

3 answers

8

You have declared char num1[7] . It is an array with 7 characters. But in C there is no arrays assignment operation. Then num1 = "ZERO" does not work.

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

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

  • 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];
    
        
    24.05.2014 / 16:08
    2

    Can not assign values to arrays. You need to assign the array elements one by one. The C language has a few shortcuts to facilitate this assignment when it comes to strings .

    num1[0] = 'Z';
    num1[1] = 'E';
    num1[2] = 'R';
    num1[3] = 'O';
    num1[4] = '
    strcpy(num1, "ZERO"); // lembra-te de incluir o header <string.h>
    
    ';

    or, using the shortcut

    num1[0] = 'Z';
    num1[1] = 'E';
    num1[2] = 'R';
    num1[3] = 'O';
    num1[4] = '
    strcpy(num1, "ZERO"); // lembra-te de incluir o header <string.h>
    
    ';
        
    24.05.2014 / 16:09
    0

    In C language you can not assign a string to a vector simply by using the assignment operator '='. This would be possible if you were using the C ++ string type.

    However, there is a set of functions in C included in the header file of which I can cite strcpy that copies a string to a char array. So in your comparison, if it is number 6, you would do:

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

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

        
    25.04.2015 / 07:49