Thread: School project help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    School project help

    Okay, so I need to do a huck finn project and I decided I'd show off some cool computer programming knowledge by making a huck finn quiz in C, but, I believe i have everything right, no errors pop up when I compile it, but as soon as I put in my name it shows all the questions and the closes, any help? heres the script. also, i just found out if i dont use a space when i first enter my name, it wont skip, but it will only go to question 2, then it will become non responsive and crash, any help?


    Code:
    #include<stdio.h>
    
    main()
    {
          char name[30];
          int answer;
          int score;
          int total;
          
          printf("Enter Your Name\n");
          scanf("%s", name);
    
          
          printf("What pair of characters is this story about?\n 1.Tom and  Huck\n 2.Huck and Pap\n 3.Jim and Huck\n 4.Pap and Judge Thatcher\n");
          scanf("%d", answer);
          if (answer = 3)
           score + 1;
    
           
           printf("What River did our main characters travel on?\n 1.Ohio\n2.Arkansas\n3.Mississippi\n4.Wisconsin");
           scanf("%d", answer);
          if (answer = 3)
           score + 1;
    
           
           printf("What was the name of the family that took 'George Jackson' in?\n1.Sheperdsons\n2Grangerfords\n3.Watson\n4.Sawyer");
           scanf("%d", answer);
           if (answer = 2)
           score + 1;
    
           
           printf("Where was Jim's destination?\n1.Cairo\n2.Ohio\n3.Iowa\n4Mississippi");
           scanf("%d", answer);
           if (answer = 1)
           score + 1;
    
           
           printf("Who was declaired dead at the end of the book?\n1.Jim\n2.Huck\n3.Pap\n4Tom\n");
           scanf("%d", answer);
           if (answer = 3)
           score + 1;
    
           
           printf("%s got %d out of 5",name, score);
           
           getchar();
           getchar();
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Put one of those getchar()-s after reading in the name to clear out the '\n' left in the buffer. Also, = is not ==.
    if (answer = 3) is always true.
    and the statement
    score + 1 does nothing.
    Last edited by Tclausex; 11-14-2011 at 05:29 PM.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You also want
    Code:
    if( answer == 3) { /*do stuff */ }
    Note the double equals. A double equals tests for equality, a single equals sets the thing on the left to be equal to the thing on the right.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Add ...
    Code:
    while (getchar() != "\n");
    ... at line 12

    The scanf("%s"... line is leaving a carriage return in the input buffer, the integer inputs don't absorb it so they just keep on a trucking by...

    (Yeah I know... WTF??? ... but that's how it is)

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a lot of problems with that code:
    Code:
    $ make foo
    gcc -Wall -g -std=c99  -lcurl -lpthread -lm  foo.c   -o foo
    foo.c:4: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:15: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:16: warning: suggest parentheses around assignment used as truth value
    foo.c:17: warning: statement with no effect
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:22: warning: suggest parentheses around assignment used as truth value
    foo.c:23: warning: statement with no effect
    foo.c:27: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:28: warning: suggest parentheses around assignment used as truth value
    foo.c:29: warning: statement with no effect
    foo.c:33: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:34: warning: suggest parentheses around assignment used as truth value
    foo.c:35: warning: statement with no effect
    foo.c:39: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:40: warning: suggest parentheses around assignment used as truth value
    foo.c:41: warning: statement with no effect
    foo.c:8: warning: unused variable ‘total’
    foo.c:15: warning: ‘answer’ is used uninitialized in this function
    foo.c:44: warning: ‘score’ is used uninitialized in this function
    1. It should be int main(void), and return 0; at the end. Read this link.
    2. getchar may not the best way to keep the console open. Read this: Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?.
    3. You need an & before answer in each scanf call. scanf requires a pointer to where you want to store the results: scanf("%d", &answer);
    4. You should check the reutrn value of scanf. It's possible that the user enters bogus data and scanf doesn't put anything in answer, then, you check that bogus answer for correctness. This is especially possible considering the following point.
    5. The "%s" modifier in scanf stops at the first white space (tab, space, newline, etc). If you try to do a full name like "Jon Doe", the " Doe" part is left in the input buffer, and subsequent scanf calls for a %d will fail.
    6. = is for assignment, == is for comparison.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Hey guys, Thanks for your replys, i got it to work, for the most part, is there any way to get around that space thing so you can put a whole name? Also, it doesnt seem to be adding to the score, any idea why? Sorry if i sound like a noob, I'm only 15 and starting to learn programming.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The preferred way to get user input (especially for a string like name, is to use fgets():
    Code:
    fgets(charArrayName, sizeof(charArrayName), filePointer);
    Now your name will include the newline ( unlike with scanf() ), but nothing is left behind to goof up the input stream for the next input of variable.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    score + 1 doesn't DO anything. It is simply evaluated and thrown away.

    Your intention is
    Code:
    score = score + 1 ;
    //or
    score += 1 ;
    //or better still
    ++score ;

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    It works! Thanks guys, My grade has been saved by you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  2. school project help
    By yogai in forum C Programming
    Replies: 8
    Last Post: 09-09-2004, 06:03 PM
  3. Help with School Project
    By jtouron in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2003, 12:27 AM
  4. school project been killin me..
    By oldsaintd in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 03:47 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM