# Thread: yeah alil help for my switch problem

1. ## yeah alil help for my switch problem

Yo..I have a problem with my programm. I need a lil assistance…The part that I cant do is the random question part..If I choose five it does not give me the random question ..How would I do that?

Code:
```#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int riwo ( int ,int, int, int ); //function prototype .
void rightwords ( void ); //function for the right words.
void wrongwords (void ); //function for hte wrong words.

int main()
{
int num1, num2, awn,type;
int diff;

srand(time(NULL));

printf ("Enter the level of difficulty.1 for one-digit integers, and 2 for\ntwo-digit integers: ");
scanf ("%d", &diff );
if ( diff == 1 ){
num1 = 1+ rand () % 9;
num2 = 1+ rand() % 9;  }
else {
num1 = 10+rand() %99;
num2 = 10+rand() %99;}
printf ("\nEnter the type of arithmetic problem you would like to use: \n"
"2)<---Subtraction\n"
"3)<---Multiplication\n"
"4)<---Division\n"
"5)<---random ( Addition, Subtraction, Multiplication and Division\n"
"\nPlease choose from thefollowing: ");
scanf ("%d", &type );

for (;;){

switch ( type){
case 1:
printf ("How much is %d plus %d?", num1, num2 );
scanf ("%d", &awn );
break;
case 2:
printf ("How much is %d minus %d?", num1, num2 );
scanf ("%d",&awn );
break;
case 3:
printf ("How much is %d times %d?", num1, num2 );
scanf ("%d", &awn );
break;
case 4:
printf ("How much is %d divided by %d?", num1, num2 );
scanf ("%d", &awn );
break;
case 5:
type = 1+rand() % 4;
break;

}

if ( riwo (num1, num2, awn,type ) == 1) {
rightwords();
if ( diff == 1){
num1 = 1+ rand()  %9;
num2 = 1+ rand()  %9; }
else{
num1 = 10+rand() % 99;
num2 = 10+rand() % 99;

}}
else
wrongwords();
}

system ("PAUSE");
return 0;
}

int riwo (int a, int b, int tot, int ty )
{

if ( ty == 1 ){
if ( tot == a+b )
return (1);
else
return (0);     }
if ( ty == 2)  {
if ( tot == a-b )
return (1);
else
return (0);       }
if (ty == 3 )    {
if ( tot == a*b )
return (1);
else
return (0);}
if ( ty == 4 )   {
if ( tot == b/a )
return (1);
else
return (0);      }

}

void rightwords( void )

{
int right;

right = 1+rand () % 4;

switch ( right ){

case 1:
printf ("Very good!\n");
break;

case 2:
printf ("Excellent!\n");
break;

case 3:
printf ("Nice work!\n");
break;

case 4:
printf ("Keep up the good work!\n");
break;
}

}

void wrongwords ( void )
{
int wrong;
wrong = 1+rand() % 4;

switch ( wrong ){

case 1:
printf ("No. Please try again.\n");
break;

case 2:
printf ("Wrong. Try once more.\n");
break;

case 3:
printf ("Don't give up!\n");
break;

case 4:
printf ("No.keep trying\n");
break;
}

}```
Thanks

2. I suppose you need to start by dynamically building the question text. I'd do this by storing the following in an array like this:
>char *operators[] = {"plus", "minus", "times", "divided by"};

Then, when you have chosen your random number (between 0 and 3), use it to index the array and print the message.

>printf("How much is %d %s %d?", num1, operators[RandomNumber], num2);

Then, you just ask the user for their input in the normal way.

Does this help?

3. ## but i dont know how to use arrays or pointers

Hammer the book that i am using dint introduce us to pointer and arrays at the movement that is the next chapter after function so i cant use arrays and pointer in this programm....

4. ## Use functions

Use seperate functions for the questions.
use the switch statement to call different functions.
Think about it, you'll understand.

5. ## still need help

hammer man help me out yo.....

6. You could try this:

Code:
```int fiveChosen;  /* this would occur higher up, not right above what follows it here */

do
{

fiveChosen = 0;

switch (type)
{

case 1:
printf ("How much is %d plus %d?", num1, num2 );
scanf ("%d", &awn );
break;

case 2:
printf ("How much is %d minus %d?", num1, num2 );
scanf ("%d",&awn );
break;

case 3:
printf ("How much is %d times %d?", num1, num2 );
scanf ("%d", &awn );
break;

case 4:
printf ("How much is %d divided by %d?", num1, num2 );
scanf ("%d", &awn );
break;

case 5:
fiveChosen = 1;
type = 1 + rand() % 4;

}

} while (fiveChosen);```
What happens there is that the switch is in a do...while loop that repeats as long as "fiveChosen" is not 0. If the number 5 is chosen, then "fiveChosen" gets set to 1, which makes the condition true, which will then bring the randomized value for "type" back into the switch statement. The program will then continue normally as if the randomized value was what was inputted.

7. ## Re: still need help

Originally posted by datainjector
hammer man help me out yo.....
Code:
```case 5:
type = 1 + rand % 4;
switch (type)
{
case 1: printf ("What is %d plus %d", num1, num2); break;
case 2: printf ("What is %d minus %d", num1, num2); break;
case 3: printf ("What is %d times %d", num1, num2); break;
case 4: printf ("What is %d divided by %d", num1, num2); break;
default: /* never happens */ break;
}
scanf ("%d", &awn );
break;```

8. /* this compiles on gcc, sorry about the sloppy paste. It seems to work correctly, as well

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

int riwo ( int ,int, int, int ); //function prototype .
void rightwords ( void ); //function for the right words.
void wrongwords (void ); //function for hte wrong words.
int num1, num2, awn,type;
int diff;

int probswitch(int x)
{

switch(x) {
case 1:
printf ("How much is %d plus %d?", num1, num2 );
scanf ("%d", &awn );
break;

case 2:
printf ("How much is %d minus %d?", num1, num2 );
scanf ("%d",&awn );
break;
case 3:
printf ("How much is %d times %d?", num1, num2 );
scanf ("%d", &awn );
break;
case 4:
printf ("How much is %d divided by %d?", num1, num2 );
scanf ("%d", &awn );
break;
case 5:
type = 1+rand() % 4;
probswitch(type);
break;
}
return 0;
}
int main()
{

srand(time(NULL));

printf ("Enter the level of difficulty.1 for one-digit integers, and 2 for\ntwo-digit integers: ");
scanf ("%d", &diff );
if ( diff == 1 ){
num1 = 1+ rand () % 9;
num2 = 1+ rand() % 9; }
else {
num1 = 10+rand() %99;
num2 = 10+rand() %99;}
printf ("\nEnter the type of arithmetic problem you would like to use: \n"
"2)<---Subtraction\n"
"3)<---Multiplication\n"
"4)<---Division\n"
"5)<---random ( Addition, Subtraction, Multiplication and Division\n"
"\nPlease choose from thefollowing: ");
scanf ("%d", &type );

for (;{

switch ( type){
case 1:

case 2:

case 3:

case 4:

case 5:
probswitch(type);

}

if ( riwo (num1, num2, awn,type ) == 1) {
rightwords();
if ( diff == 1){
num1 = 1+ rand() %9;
num2 = 1+ rand() %9; }
else{
num1 = 10+rand() % 99;
num2 = 10+rand() % 99;

}}
else
wrongwords();
}

system ("PAUSE");
return 0;
}
int riwo (int a, int b, int tot, int ty )
{

if ( ty == 1 ){
if ( tot == a+b )
return (1);
else
return (0); }
if ( ty == 2) {
if ( tot == a-b )
return (1);
else
return (0); }
if (ty == 3 ) {
if ( tot == a*b )
return (1);
else
return (0);}
if ( ty == 4 ) {
if ( tot == b/a )
return (1);
else
return (0); }

}

void rightwords( void )

{
int right;

right = 1+rand () % 4;

switch ( right ){

case 1:
printf ("Very good!\n");
break;

case 2:
printf ("Excellent!\n");
break;

case 3:
printf ("Nice work!\n");
break;

case 4:
printf ("Keep up the good work!\n");
break;
}

}

void wrongwords ( void )
{
int wrong;
wrong = 1+rand() % 4;

switch ( wrong ){

case 1:
printf ("No. Please try again.\n");
break;

case 2:
printf ("Wrong. Try once more.\n");
break;

case 3:
printf ("Don't give up!\n");
break;

case 4:
printf ("No.keep trying\n");
break;
}

}

Popular pages Recent additions