-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.– Maniero
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 thebreak
in each case, all will be executed in sequence and its variable will be with the value of last execution regardless of the value ofrand1
(num1 = "NOVE"
, for example). So don’t forget thebreak
, okay? :)– Luiz Vieira