Randomized multiple choice
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Randomized multiple choice

  1. #1
    Join Date
    Feb 2012
    Posts
    68

    Randomized multiple choice

    okay so I have gotten my code sorted out, and now it works. But to be adaptable to 3pi robots, which I will be loading it into eventually, the user has to be given three letter (a, b, c) multiple choices for answer input to the math questions. The three choices have to be randomized everytime the program loops too. Currently it just runs on number input. Any ideas of what sort of statements or functions i should use to do this?


    Code:
     
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    
    
    
    
    int menu();
    void addition(int *Preward);       // function to return truth values
    void subtraction(int *Preward);
    
    int main()
    {
        int Finished = 0;
        int choice, score = 0;
        int reward = 0;
    
        while(Finished != 1)       // while finished is not true
        {
            printf("\nscore: &#37;d", score); // print the score
            {
                choice = menu();                   // calls for user input and enters it to switch
                switch  (choice){                  // accesses the functions
                case 0: Finished = 1;                         break;
                case 1: addition(&reward);                  break;
                case 2: subtraction(&reward);             break;
                default : printf("Error in input\n\n");      break;
                }
                if(reward == 1){
                    score += reward; // score = score + reward
                }
            }
        }
        return 0;
    }
    
    int menu()             //main menu that gets called and allows for user input
    {
        int MenuChoice;
        printf("\n");
        printf("Enter 1 for addition\n");
        printf("Enter 2 for Subtraction\n");
        printf("Enter 0 to quit\n");
        printf("-----------------\n");
        printf("Choice: ");
        scanf("%d", &MenuChoice); // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    void addition(int *Preward)                                //function that is called by the users selection
    {
        int answer;
        int random1, random2;
        srand(time(NULL));                        //generates fresh numbers each time.
        random1 = 1+(rand()%99);              // from 1-99
        random2 = 1+(rand()%99);              // from 1-99
    
        printf("\n%d + %d = ", random1, random2);         //code to format the random math problem
        scanf("%d", &answer);                                   // user input of answer
        {
            if (answer == (random1+random2)){
                printf("Congratulations\n\n");
                *Preward = 1;                                    // tellsesult whether to add a pt or not
            }                                                        //if user is correct than the score is incremented
            else{
                printf("Incorrect\n\n");}                //if not than the score is not incremented
    
        }
    }
    
    void subtraction(int *Preward)                     // follows same model as previous function, but instead subtraction.
    {
        int answer;
        int random1, random2;
    
        srand(time(NULL));
    
        random1 = 1+(rand()%99);              // from 1-99
        random2 = 1+(rand()%99);               // from 1-99
    
        printf("\n%d - %d = ", random1, random2);
        scanf("%d", &answer);
    
        if (answer == (random1-random2)){
            printf("Congratulations\n\n");
            *Preward = 1;                          // tells result whether to add a pt or not
        }
        else{
            printf("Incorrect\n\n");
        }
    
    }
    Last edited by chucker; February 20th, 2012 at 01:06 PM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Randomized multiple choice

    Scanf can read characters as well, see this http://msdn.microsoft.com/en-us/library/9y6s16x1.aspx

    The switch can also work on characters (as long as they are constant) so you can use case 'a', case 'b' and so on.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Feb 2012
    Posts
    68

    Re: Randomized multiple choice

    Yes but how about randomization every time? So that you are not just selecting A because you know it has the right answer every time.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Randomized multiple choice

    Now I don't understand. You randomize the question, are you going to add that the user have three choices to select from instead of giving the answer as a number?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Feb 2012
    Posts
    68

    Re: Randomized multiple choice

    Ya i want to get rid of the integer input for the answer and replace with character input of 'a' 'b' or 'c' for possible answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center