Thread: A guy who sucks at programming needs help!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    A guy who sucks at programming needs help!

    I need to write this quiz using functions. I'm only looking for directions, advice, etc.

    Do I have to write a function for each question or can do one to cover them all?

    How I store the input so I can display the correct/incorrect answers at the end? Would it be scanf()?

    1) Create a quiz that contains at least five questions about a hobby, popular music, astronomy, or any other personal interest.

    2) Each question can be multiple choice or true/false answers
    a) valid responses are a, b, c, or d
    b) t or f

    3) Data validation (hint: may need to use the phantom newline character solution from your book.)
    a) If the user responds to a question with an invalid character, display an error message and prompt the user again.
    b) If the user answers the question with a valid and correct response, display an appropriate message to reflect this.
    c) If the user responds to a question with a valid but incorrect response, display an appropriate message as well as the correct answer.

    4) At the end of the quiz, display the number of correct and incorrect answers.
    Option 2: Guess the Number
    1) Generate a random number from 1 through 100 and allow the user to guess the number.
    2) After each guess, display a message that informs the user if the guess was
    a) too high,
    b) too low, or
    c) correct

    3) Allow the user to play until the number is guessed correctly
    4) When the number is guessed correctly, display a count for the number of guesses that were needed. (hint: use the rand() or srand() function for the random number generator).
    Option 3: Quit
    1) Exit program.

    I feel like I jumped in a lake wearing concrete shoes. :/

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, helpneeded!

    You can make one function that will answer more than one question, but the first thing you'll need is a menu to display the user's 3 options, as you've listed them.

    A do while or while loop is usually best for that.

    When you have valid input in the menu loop, then you can call your separate function or functions, based on what is needed for the option the user chose.

    You can bust off those concrete shoes a bit, if you'll run through the C tutorial that is linked at the top of this forum.

    Here's the thing - it's up to YOU to start this - whatever shoes you have. We won't start your code for you. When you post for help with a problem, post the code, and describe the problem you're having with the program. The more specific you are, the easier and better our help will be.

    Don't wait for starting code here - it won't happen. If you post to several different programming forums, we'll pretty much ignore you here, because we're posting on those other forums also, and we don't want to duplicate our replies, here. If you post here, and don't answer back to replies to your thread, your post will be pushed down to the bottom of the page, or the second page even, and ignored. Replies happen fairly rapidly except in the middle of the night, US time.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start by reading this thread
    A development process

    Split the problem up into steps you can solve.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Thanks guys! Like I said, I'm only looking for direction, advice, etc. I want to learn this its just proving difficult for me.

    I have started the code but it wasn't using a do while loop (more proof that I suck). I'll try to correct what I have and post it here when I get out of class tonight.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would suggest starting by writing a function called from main to do the "Option 2: Guess the Number" choice.

    IMHO, This is easier than option 1.

    Then after you get that to work, I would add the menu display choice code.

    Then, you get the exit/quit to work
    Last, I would do the Quiz because that is likely harder to do.

    Tim S.
    Last edited by stahta01; 10-10-2012 at 12:15 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    I know this is very basic but I just wanted to see if I'm on the right track. This is something like my template?

    Code:
    include<stdio.h>
    
    
    int main()
    
    
    {
    
    
    	/* Menu */	
    	int menu = 1;
    
    
    	do {
    
    
    
    
    			/* Menu Options */
    		printf("1. Random Quiz\n");
    		printf("2. Guess a magic number!\n");
    		printf("3. Exit\n");
    		scanf("%d", menu);
    
    
    
    
    	    } while (menu !=3)
    
    
    	    	switch (menu)
    	    		{
    	    		case 1:
    
    
    	    		
    	    			/*Call first function */
    	    			FirstFunction
    
    
    	    			break;
    	    		case 2:
    
    
    	    			/*Call second function */
    	    			SecondFunction
    
    
    	    			break;
    
    
    	    		case 3:
    
    
    	    			menu = 3;
    	    			printf("Bye\n");
    	    			break;	
    	    		default: 
    
    
    	    			printf("Please enter a number 1 - 3\n");
    
    
    	    }			
    	    return 0;
    }
    
    
    
    
    
    
    /* Functions */

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by helpneeded View Post
    I know this is very basic but I just wanted to see if I'm on the right track. This is something like my template?
    Nope. When you have a menu program, you have to have it handle all the logic from the user's choices, right inside the do while or while loop.

    Note the changes in red:

    Code:
    include<stdio.h>
    
    
    int main()
    {
    	/* Menu */	
    	int menu = 1;
    
    	do {
    		/* Menu Options */
    		printf("1. Random Quiz\n");
    		printf("2. Guess a magic number!\n");
    		printf("3. Exit\n");
    		scanf("%d", menu);  //should be &menu for scanf()
    
            //} while (menu !=3) not here!
    
    
    	    	switch (menu)
        		{
            		case 1:
        			/*Call first function */
        			FirstFunction
        			break;
    
    	    		case 2:
       			/*Call second function */
        			SecondFunction
        			break;
    
    	    		case 3:
        			menu = 3;
        			printf("Bye\n");
        			break;	
    
    	    		default: 
        			printf("Please enter a number 1 - 3\n");
    
    	         }	//end of switch statement
    
               } while (menu !=3) 		
               return 0;
    }
    
    /* Functions */
    That's what I've noticed. Good start with the indentation - but 2 to 5 spaces instead of 8 char long indents per level, is what you want. White space is good, but don't make too much of it. Enough for clarity, only.
    Last edited by Adak; 10-10-2012 at 02:14 PM.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Thanks! I made the changes and tried to compile but I get this error:

    project2.c(1) : error C2143: syntax error : missing '{' before '<'
    project2.c(1) : error C2059: syntax error : '<'

    I'll have to work on it more when I get home.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by helpneeded View Post
    Thanks! I made the changes and tried to compile but I get this error:

    project2.c(1) : error C2143: syntax error : missing '{' before '<'
    project2.c(1) : error C2059: syntax error : '<'

    I'll have to work on it more when I get home.
    Be sure to put in the # char before the word "include" on row 1. And,
    if you haven't yet made the FirstFunction, and SecondFunction yet, then comment those lines out, for now with // or /* */ .

    Then see what your compiler thinks of it.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    oh wow. I can't believe I forgot the #. I think its safe to say none of you will be working with me as a professional programmer anytime soon. lol.

    *edit*


    Thought I would post the new errors before I left for class.

    project2.c
    project2.c(20) : error C2059: syntax error : 'switch'
    project2.c(29) : error C2046: illegal case
    project2.c(36) : error C2046: illegal case
    project2.c(41) : error C2047: illegal default
    project2.c(45) : error C2059: syntax error : 'while'
    project2.c(47) : error C2059: syntax error : '}'


    Am I getting illegal case because they are empty?
    Last edited by helpneeded; 10-10-2012 at 03:05 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Am I getting illegal case because they are empty?
    There was a missing ; at the end of the while(); statement. Do you have this now?
    Code:
    #include<stdio.h>
    
    int main()
    {
    	/* Menu */	
       int menu = 1;
    
    	do {
    		/* Menu Options */
    		printf("1. Random Quiz\n");
    		printf("2. Guess a magic number!\n");
    		printf("3. Exit\n");
    		scanf("%d", &menu);  
    
          switch (menu) {
         		case 1:
        			/*Call first function */
        			//FirstFunction
        			break;
    
        		case 2:
       			/*Call second function */
        			//SecondFunction
        			break;
    
        		case 3:
        			menu = 3; // ???????? ;)
        			printf("Bye\n");
        			break;	
    
    	    		default: 
        			printf("Please enter a number 1 - 3\n");
    
    	   }//end of switch statement
       }while(menu !=3); 		
       return 0;
    }
    Should cause your compiler to give no warnings or errors, yes?

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Still getting errors. :/
    Code:
    project2.cproject2.c(20) : error C2059: syntax error : 'switch'
    project2.c(26) : error C2046: illegal case
    project2.c(33) : error C2046: illegal case
    project2.c(36) : error C2047: illegal default
    project2.c(40) : error C2059: syntax error : '}'
    project2.c(41) : error C2059: syntax error : 'return'
    project2.c(42) : error C2059: syntax error : '}'

    Here is my current code.


    Code:
    #include<stdio.h>
    
    
    int main()
    
    
    {
    
    
    /* Menu */	
    int menu = 1;
    
    
    do {
    		/* Menu Options */
    	printf("1. Random Quiz\n");
    	printf("2. Guess a magic number!\n");
    	printf("3. Exit\n");
    	scanf("%d", &menu);
    
    
        } 
    	switch (menu) {
    		case 1:
    			/*Call first function */
    			/* FirstFunction; */
    
    
    			break;
    		case 2:
    
    
    			/*Call second function */
    			/*SecondFunction; */
    
    
    			break;
    
    
    		case 3:
    			printf("Bye\n");
    			break;	
    		default: 
    
    
    			printf("Please enter a number 1 - 3\n");
    		}
    	} while (menu != 3);	
    return 0;
    }

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You forgot to remove the closing brace before the switch statement (left over from the do-while statement)
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Thanks. It compiled.... but crashed.

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Alright, I have it up and running. Now I just have to create the functions. See you in roughly 10 days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. this REALLY sucks
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-31-2004, 12:46 AM
  2. XP Sucks
    By Darkman in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 01-19-2004, 07:54 PM
  3. this->sucks
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2002, 09:38 AM