Help using a character as parameter
I have a multiple choice function below that is part of a larger program but I am having trouble having it operate with characters as inputs and arguments. It has to be a character input, but I kinda want it to act like an integer, how do i do that?
Code:
void addition(int *Preward) //function that is called by the users selection
{
int random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char a, b, c; // ERROR
char userselect; //ERRRORR
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
rando1 = 2+(rand()%198); // 2 to 198
rando2 = 2+(rand()%198); // 2 to 198
order = 1+(rand()%3);
switch(order){
case 1: printf("\n%d + %d = ", random1, random2);
one = (random1+random2);
two = (rando1);
three = (rando2);
printf("\n select from the following\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect); //ERROR
if (userselect == a){ //ERROR
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is a\n\n");
}break;
Re: Help using a character as parameter
Code:
if (userselect == a)
Where do you initialize "a" in your code? I don't see it.
Code:
char a, b, c; // ERROR
Why are these errors? You're declaring variables, so what's the problem? The only problem I see is that you didn't initialize them.
Regards,
Paul McKenzie
Re: Help using a character as parameter
You actually don't need the three chars: a, b, and c.
I'm not completely familiar with c coding, but with c++ you would..
char charVariable;
cin >> charVariable;
if(charVariable == 'a');
You need 'a', 'b', or 'c'. If you do a without the apostraphes then it computes the actual ascii value of a.
So, by saying:
if(charVariable == a)
You're actually saying:
if(charVariable == 97)
Code:
void addition(int *Preward) //function that is called by the users selection
{
int random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char userselect; //ERRRORR
srand(time(NULL)); //generates fresh numbers each time.
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
rando1 = 2+(rand()%198); // 2 to 198
rando2 = 2+(rand()%198); // 2 to 198
order = 1+(rand()%3);
switch(order){
case 1: printf("\n%d + %d = ", random1, random2);
one = (random1+random2);
two = (rando1);
three = (rando2);
printf("\n select from the following\n a) %d b) %d c) %d\n", one, two, three);
scanf("%c", &userselect); //ERROR
if (userselect == 'a'){ //ERROR
printf("Congratulations\n\n");
*Preward = 1;
}
else{
printf("incorrect, correct answer is a\n\n");
}break;