Thread: Help using a character as parameter

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

    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; //HERE IS ISSUE char userselect; //HERE IS ISSUE 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); //HERE IS ISSUE if (userselect == a){ //HERE IS ISSUE printf("Congratulations\n\n"); *Preward = 1; } else{ printf("incorrect, correct answer is a\n\n"); }break;
    Last edited by Chucker; 02-20-2012 at 09:01 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The switch statement works just fine with char type, because a char is really just a small integer. You could do something like

    Code:
    switch( userselect ) {
       case a:
          /* hurrah!!!! */
          break;
       default:
          /* noooooooooooooo!!!! */
    }
    Last edited by TheBigH; 02-20-2012 at 10:39 PM.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    Quote Originally Posted by TheBigH View Post
    The switch statement works just fine with char type, because a char is really just a small integer. You could do something like

    Code:
    switch( userselect ) {
       case a:
          /* hurrah!!!! */
          break;
       default:
          /* noooooooooooooo!!!! */
    }
    i need to keep the switch(order) as is so that the resst of the cases below are randomized. its supposed to be a random multiple choice switch. and the choices i really want to be letter select somehow

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you should tell us what your program is actually supposed to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    Quote Originally Posted by laserlight View Post
    I think you should tell us what your program is actually supposed to do.
    its a math game and you chose either addition or subtration then it gives u a random problem and keeps ur score. I m still trying to work on the multiple choice interface.

    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;
        int a, b, c;
        int 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);
        a = 0;
        b = 0;
        c = 0;
        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)l;
                    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;
    }
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Chucker
    its a math game and you chose either addition or subtration then it gives u a random problem and keeps ur score.
    Well, here's the thing: your random problem consists of just two random numbers. From these two numbers, you compute the correct answer. Now, you also need two (plausible) "wrong answers", and then the correct answer and the wrong answers should be in random order.

    Instead of trying to account for all the cases, create an array of 3 possible answers. Populate the array with the correct answer and the wrong answers, then shuffle the array. Now, you display the array of possible answers. Whatever answer is selected, check if it is equal to the correct answer. In fact, this part can be used for both the addition and subtraction parts, so you don't need to rewrite code if you place it in a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    Quote Originally Posted by laserlight View Post
    Well, here's the thing: your random problem consists of just two random numbers. From these two numbers, you compute the correct answer. Now, you also need two (plausible) "wrong answers", and then the correct answer and the wrong answers should be in random order.

    Instead of trying to account for all the cases, create an array of 3 possible answers. Populate the array with the correct answer and the wrong answers, then shuffle the array. Now, you display the array of possible answers. Whatever answer is selected, check if it is equal to the correct answer. In fact, this part can be used for both the addition and subtraction parts, so you don't need to rewrite code if you place it in a function.
    rando1 and rando2 are the plausibles. There are three cases so that a, b and c all get a chance to be the right answer. and they are randomly accessed by the order variable. i am just trying to make letters act as a truth value; a) b) or c) instead of typing in 1) 2) or 3). but when i do letters, there is an infinte loop error or just something else messed up

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Chucker
    There are three cases so that a, b and c all get a chance to be the right answer. and they are randomly accessed by the order variable.
    Yes, and I'm telling you that that makes your code very verbose. What if you had four cases instead? Five? I've done MCQ tests with five possible answers before.

    Granted, part of why your code is so verbose is because you did not abstract out the parts of the cases that are the same. For example:
    Code:
    char correct = '\0';
    printf("\n%d + %d = ", random1, random2);
    switch (order) {
    case 1:
        one = random1 + random2;
        two = rando1;
        three = rando2;
        correct = 'a';
        break;
    case 2:
        one = rando1;
        two = random1 + random2;
        three = rando2;
        correct = 'b';
        break;
    case 3:
        one = rando1;
        two = rando2;
        three = random1 + random2;
        correct = 'c';
        break;
    default:
        printf("error\n");
    }
    
    printf("\n select from the following\n a) %d b) %d c) %d\n", one, two, three);
    scanf("%c", &userselect);
    if (userselect == correct) {
        printf("Congratulations\n\n");
        *Preward = 1;
    } else {
        printf("incorrect, correct answer is %c\n\n", correct);
    }
    Quote Originally Posted by Chucker
    when i do letters, there is an infinte loop error or just something else messed up
    The problem with reading characters is that the newline character you get when you enter input is also counted as a character. You must remember to account for that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    I see what you did there!! ty soooo much c++ witch! lol i have no idea why i was cramming so much into cases.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    What happens if one of the plausible wrong answers happens to be equal to the right answer?
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using a character as parameter
    By Chucker in forum C Programming
    Replies: 3
    Last Post: 02-20-2012, 11:03 PM
  2. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  3. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:19 AM