Help using a character as parameter

This is a discussion on Help using a character as parameter within the C Programming forums, part of the General Programming Boards category; I have a multiple choice function below that is part of a larger program but I am having trouble having ...

  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; // ERROR char userselect; //ERRRORR 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); //ERROR if (userselect == a){ //ERROR printf("Congratulations\n\n"); *Preward = 1; } else{ printf("incorrect, correct answer is a\n\n");
    }break;


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Hmm... looks like your code formatting got messed up. You might want to try again.

    Anyway, a character is an integer, more or less.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I fixed your code posting problem, but it wore me out so much that I don't really feel like looking at the code. I put a couple of notes in it though.
    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;
        char userselect;
    
        srand(time(NULL)); /// You probably want this in main 
                           /// (only call it once in the program)
    
        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) {    /// a hasn't been initialized to anything. Do you mean 'a' ?
                    printf("Congratulations\n\n");
                    *Preward = 1;
                }
                else {
                        printf("incorrect, correct answer is a\n\n");
                }
            break;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Continued at Help using a character as parameter.

    *thread closed*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-05-2011, 09:21 PM
  2. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  3. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 01:46 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 09:11 PM
  5. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 05:19 AM

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