Randomized multiple choice

This is a discussion on Randomized multiple choice within the C Programming forums, part of the General Programming Boards category; okay so I have gotten my code sorted out, and now it works. But to be adaptable to 3pi robots, ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    27

    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: %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 fr 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; 02-20-2012 at 02:06 PM.

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    somethings wrong with the code tags or is it just me

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's not just you, I have seen this before. Seems like the new lines are ignored, try again I suppose.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    I've seen this using notepad and Windows wordpad as well. The reason is the sequence used as newline( \n, \r or \r\n or even \n\r ).

    EDIT: Try again by using a different editor
    Last edited by GReaper; 02-20-2012 at 01:56 PM.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    u can jump down to the add and subtraction functions. i figured thats where the random multi choice should be implemented

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    okay I figured out how to do multichoice kinda. below are my two new addition and subtraction functions. but my problem is when trying to input the character here:

    Code:
    scanf("%c", &userselect);
    it just skips it, im pretty sure it is because characters dont work that way, but can someone show me an alternative way?

    Here is the rest of the goods:

    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: %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 fr 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 random1, random2;
        int order;
        int one, two, three;
        int rando1, rando2;
        char a, b, c;
        char userselect;
        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);
                    if (userselect == a){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                    }
                    else{
                        printf("incorrect, correct answer is a\n\n");
                     }break;
    
    
    
    
            case 2: printf("\n%d + %d = ", random1, random2);
    
    
    
    
                one = (rando1);
                two = (random1+random2);
                three = (rando2);
                printf("\n select from the following letters\n a) %d b) %d  c) %d\n", one, two, three);
                scanf("%c", &userselect);
                    if (userselect == b){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                    }
                    else{
                        printf("incorrect, correct answer is b\n\n");
                    }break;
    
    
            case 3: printf("\n%d + %d = ", random1, random2);
                one = (rando1);
                two = (rando2);
                three = (random1+random2);
                printf("\n select from the following letters\n a) %d b) %d  c) %d\n", one, two, three);
                scanf("%c", &userselect);
                    if (userselect == c){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                    }
                    else{
                        printf("incorrect, correct answer is c\n\n");
            }break;
            default: printf("error\n"); break;
    }
    }
    
    
    void subtraction(int *Preward) // follows same model as previous function, but instead subtraction.
    {
        int random1, random2;
        int order;
        int one, two, three;
        int rando1, rando2;
        char a, b, c;
        char userselect;
        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);
                    if (userselect == a){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                    }
                    else{
                        printf("incorrect, correct answer is a\n\n");
            }break;
    
    
    
    
    
    
            case 2: printf("\n%d - %d = ", random1, random2);
                one = (rando1);
                two = (random1-random2);
                three = (rando2);
                printf("\n select from the following letters\n a) %d b) %d  c) %d\n", one, two, three);
                scanf("%c", &userselect);
                    if (userselect == b){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                        }
                    else{
                        printf("incorrect, correct answer is b\n\n");
            }break;
    
    
            case 3: printf("\n%d - %d = ", random1, random2);
                one = (rando1);
                two = (rando2);
                three = (random1-random2);
                printf("\n select from the following letters \na) %d b) %d  c) %d\n", one, two, three);
                scanf("%c", &userselect);
                    if (userselect == c){
                        printf("Congratulations\n\n");
                        *Preward = 1;
                        }
                    else{
                        printf("incorrect, correct answer is c\n\n");
            }break;
            default: printf("error\n"); break;
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple choice conditional
    By SpudCake in forum C++ Programming
    Replies: 22
    Last Post: 10-20-2011, 12:02 PM
  2. Multiple choice question.
    By ungalnanban in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 09:40 AM
  3. multiple choice
    By iLike in forum C Programming
    Replies: 6
    Last Post: 10-30-2009, 03:53 PM
  4. Multiple Choice!!!
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-08-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21