Thread: yeah alil help for my switch problem

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Talking 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"
                 "1)<---Addition\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
    Last edited by datainjector; 07-21-2002 at 08:46 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    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. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Use functions

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

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    still need help

    hammer man help me out yo.....

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    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.
    Last edited by Gabu; 07-22-2002 at 09:05 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    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;
    Last edited by Hammer; 07-22-2002 at 09:14 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    --conrad
    */
    #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"
    "1)<---Addition\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 subscribe to a feed

Similar Threads

  1. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM
  2. problem with switch command
    By DoItAllMom115 in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2003, 04:33 PM
  3. double value switch problem
    By axon in forum C++ Programming
    Replies: 4
    Last Post: 02-14-2003, 05:03 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM