Like Tree2Likes
  • 2 Post By tabstop

Having trouble creating an arithmitec game, please take a look if you can very short

This is a discussion on Having trouble creating an arithmitec game, please take a look if you can very short within the C Programming forums, part of the General Programming Boards category; So this is part of my code to create and arithmetic game, it will ask for the maximum number you'd ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    17

    Having trouble creating an arithmitec game, please take a look if you can very short

    So this is part of my code to create and arithmetic game, it will ask for the maximum number you'd like for an addition game. Every time you get the answer correct you get a point. I have the code spitting out teh random numbers but it wont output if the answer is correct it only prints out if its incorrect. Please help if you can any help is appreciated.....
    Code:
    switch(choice){
            case 1:
                printf("What is the maximum number you'd like?\n");
                scanf("%d", &maxnum);
                for (i=0; i<10; i++){
                    srand(time(0));
                    x = rand() % maxnum;
                    y = rand() % maxnum;
                    printf("What is %d+%d\n", x, y);
                    scanf("%d" , answer);
                    correct = x+y;
                        if (answer == correct){
                            printf("Correct, great job!\n");
                            count1++;
                            score += count1;
                        }
                        else
                            printf("Sorry, that's incorrect, the answer is %d\n", correct);
                }//end case1 for
            break;

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Are 'answer' and 'correct' both ints? ('answer' appears to be judging by the scanf, but confirmation would be nice)

    Also, what type should the second parameter of the scanf() be? Hint: it shouldn't be an int

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    For your scanf, you need an ampersand in front of answer, just like maxnum.
    And srand should only be called once at the very beginning of your program, not every time you want another random number. It's just used to "initialize" the random number generator (RNG).
    Also, you don't seem to be calculating score correctly !?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by oogabooga View Post
    Also, you don't seem to be calculating score correctly !?
    You're a hard boss. It's an arithmetic game, surely you don't expect the programmer to get it correct as well.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    17
    I've placed the ampersand in there but it still wont work correctly :/

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post it....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    17
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define ADD 1
    #define MULT 2
    
    int main() {
    int choice, roundscore, totalscore, count1, count2, x, y, maxnum, answer, correct, i, z, guess, guessnum;
    srand(time(0));
    
    
    
    while (choice!=4){
        printf("Please make a selection from the following:\n");
        printf("1. Play Arithmetic Game.\n");
        printf("2. Play Guessing Game.\n");
        printf("3. Print Score.\n");
        printf("4. Quit.\n");
        scanf("%d", &choice);
    
        switch(choice){
            case 1:
                printf("What is the maximum number you'd like?\n");
                scanf("%d", &maxnum);
                for (i=0; i<10; i++){
                    x = rand() % maxnum;
                    y = rand() % maxnum;
                    printf("What is %d+%d\n", x, y);
                    scanf("%d\n" , &answer);
                    correct = x+y;
                        if (answer == correct){
                            printf("Correct, great job!\n");
                            count1++;
                            roundscore += count1;
                            }
                        else
                            printf("Sorry, that's incorrect, the answer is %d\n", correct);
                }//end case1 for
                printf("Your score for the round is %d.\n", roundscore);
                totalscore += roundscore;
            break;
    
            case 2:
                guessnum = rand() % 100;
                for(z=0; z<17; z++){
                    printf("Enter a guess!\n");
                        scanf("%d\n", &guess);
                            if (guess != guessnum && guess > guessnum){
                                printf("Your guess is to high, guess again.\n");
                                count2++;
                            }
                            if (guess != guessnum && guess < guessnum){
                                printf("Your guess is to low, guess again\n");
                                count2++;
                            }
                            if (guess == guessnum){
                                printf("Great, you guessed the correct number %d in %d guesses.\n", guessnum, count2);
                                count2++;
                                break;
                            }
                                }// end case 3 for loop
            break;
    
            case 3:
                printf("Your total score is %d.\n", totalscore);
            break;
    
            case 4:
            exit(0);
    
            default:
                printf("You have made an invalid choice.\n");
        }
    
        }//END WHILE LOOP
    
    return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    \n is useless in your scanf (and on some terminals/OS probably requires you to type in another number just so it can stop skipping whitespace).

    Additionally: you are giving them an undefined number of points: roundscore starts out as an undefined number; count1 starts out as an undefined number, to which you are successively adding one to get further undefined numbers; those undefined numbers are then being added to roundscore to get another undefined number.

    You should strongly consider initializing your variables to meaningful values when you start the game.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by tabstop View Post
    [\n at the end of scanf format] ... on some terminals/OS probably requires you to type in another number just so it can stop skipping whitespace
    Incorrect!!! (Did you just pull that out of your butt?)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by oogabooga View Post
    Incorrect!!! (Did you just pull that out of your butt?)
    Have you ever used Windows? I have, and I know it requires it. You can test it with this program (assuming you have Windows installed somewhere ... I'm using MinGW but I believe it works the same in VS but I don't have it to hand right this minute):
    Code:
    #include <stdio.h>
    
    int main(void) {
    
        int i;
        printf("Type in a number:");
        scanf("%d\n", &i);
        printf("Your number was %d.\n", i);
        return 0;
    }
    You can wait as long as you like, but unless you type in another non-whitespace character your input will not be processed. (EDIT: I should add, it can be on the same "input line" -- typing "4 7<enter>" will do fine, but you can type "4<enter><enter><space><tab><enter><enter><tab><en ter>" and the program will still be waiting to finish input.)

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by oogabooga View Post
    Incorrect!!! (Did you just pull that out of your butt?)
    I doubt it... I'd say it was pulled out of the hat!

    Code:
                             _.---,_
                           .'       `'.
                            \    __..-'\
                             }-"`       \
                            /__,,..---.._|
                            \            |
                            |---..__     |
                           /        ``"-./
                         .'---...__      |
                       .'          ``"-./
                  ,--./...,,,__        /
                  '--.'__    __```.-. /._
                    / `  `  '  `=/.-.|-._)
                    | .-.  .-.  "\\  /
                    || O| | O|  ""=='_\
                  .-' '-'o '-'    ""=\`
            `''--/-              ""=-,\--._
            .---|-            ( ""=-. \`
                \             /`)"=."=|'-.
                 '.       _.-' '  "=|\|
                   (`----`    '="=|/
                    `-.       "=/`
                       '.     =/
                         \   =|
              .-.        |` "=|
             (   ~._     | "==|        _.-~`\
              \     ~.   |'"="|    _.-~      )
               ;      ~-.|.-._|_.-~         /
              /        _-(  /-.__          (
             '._..--~~`/`/-'\-._ `~~-       ;
                 jgs  /"=|  |" =\~-...___.-~
                     /=" /  | "==\
                    / = (_  \  "==\
                   ;="=   `\_)  =="\

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by tabstop View Post
    Have you ever used Windows? I have, and I know it requires it.
    Wow ... sorry. I don't know what I was thinking. My butt hurts.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I can now confirm that the same happens on *nix as well as Windows, and I can also confirm because that's what the standard says it has to do (admittedly I haven't bought the new standard yet, because I'm a bad person, but):
    Quote Originally Posted by C99 standard, section 7.19.6.2, para 5
    A directive composed of white-space character(s) is executed by reading input up to the
    first non-white-space character (which remains unread), or until no more characters can
    be read.
    You cannot just read a convenient number of whitespace characters; a non-white-space character or EOF must be read before scanf can finish. (Technically this is fscanf, but scanf just refers you to this section.)

    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  2. Debugging a short game
    By monmon_4 in forum C Programming
    Replies: 2
    Last Post: 10-02-2011, 01:34 AM
  3. Replies: 14
    Last Post: 03-11-2010, 08:08 PM
  4. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  5. trouble creating an array
    By s_ny33 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 11:18 AM

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