Thread: Loop in a quiz program

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Sweden
    Posts
    4

    Loop in a quiz program

    Hello! I'm trying to create a simple program where one user enters a number, then another user shall guess the right number. The problem i'm having is that i want the second user to be able to guess a number again if it turned out to be the wrong number. I guess i have to create some sort of loop but i really don't know how. Greatly appreciate any help.

    This is my code so far:
    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
    
    
        int number, guess;
        
        
        printf("Enter a number from 1 to 10: ");
        scanf("%d", &number);
        
        
        printf("Guess a number from 1 to 10:");
        scanf("%d", &guess);
        
        
            if (guess < number)
            {
                 printf("The number is bigger!");
            }
        
             if  (guess > number)
             {
                 printf("The number is smaller! ");
             
             }
         else if (guess == number)
         
            {
                printf("Right number! The number was: %d" , number);
            
            }
        
        
        
        
        return 0;
        
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you worked with loops before? If not, I'd suggest reading about them in your learning material and practicing with them until you get the hang of using them. There's also a tutorial here on loops: For, While and Do While Loops in C - Cprogramming.com

    The things you would need to figure out are:

    1. What code you want to be looped
    2. The criteria for the loop to continue or terminate

    Do some reading and give it a shot. Ask back if you have specific questions.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Sweden
    Posts
    4
    Nice! Thanks for the reply. No, i haven't done so much with loops, only read some examples with for and while loops counting from 1 to 100 etc.
    I thought maybe it was different if i had scanf in my code. But i will read more about it.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Location
    Sweden
    Posts
    4
    Nice! i've got it! It was easier than i thought.

    This is how it looks now:

    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
    
    
        int number, guess;
        
        
        printf("Enter a number from 1 to 10: ");
        scanf("%d", &number);
        
        
        
        printf("Guess a number from 1 to 10:");
        for ( guess; guess != number ; guess == number ) {
        
        scanf("%d", &guess);
        
        
            if (guess < number)
            {
                 printf("The number is bigger! Try Again: ");
            }
        
             if  (guess > number)
             {
                 printf("The number is smaller! Try Again: ");
             
             }
         else if (guess == number)
         
            {
                printf("Right number! The number was: %d\n" , number);
            
            }
        
        }
        
        
        return 0;
        
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It might work as is, but it seems this would be better suited for a "while()" loop than a "for()" loop.

    for ( guess; guess != number ; guess == number )

    This part of the "for()" loop is initialization. It's perfectly valid to not initialize there, so there's nothing really wrong with this. But it offers no benefits for your application.

    This part of the "for()" loop is the "update". Nothing needs to be updated after each iteration of the loop in your application, so again this offers you no benefits.

    "for()" loops are better suited for loops that update variables automatically each iteration, which your application doesn't.

    This part of the "for()" loop is the check. This is the part your application really needs. See if you can use this in a "while()" loop instead.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Location
    Sweden
    Posts
    4
    Ahh! I tried to remove the initialization and the update section and leave them blank, worked out well: for ( ; guess != number ; )
    As i read some more about the while loop i understand it's the same as a for loop but without initialization or update section.

    while ( guess != number )
    would suit this program just fine.

    Thank you so much for making this clear!

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome. Just make sure "guess" is initialized if you're checking it before it's given a value. Or, alternatively, use a "do-while" loop (similar to a "while" but the condition is not checked until the loop iterates once).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math Quiz Program Help
    By HystericalFool in forum C Programming
    Replies: 5
    Last Post: 11-17-2014, 12:00 AM
  2. Simple quiz program problem
    By paulb39 in forum C++ Programming
    Replies: 22
    Last Post: 03-05-2010, 05:13 PM
  3. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  4. Hi, Quiz C program
    By Eman in forum C Programming
    Replies: 0
    Last Post: 11-11-2009, 04:12 PM
  5. making a math quiz program
    By mackieinva in forum C Programming
    Replies: 10
    Last Post: 09-17-2007, 03:38 PM

Tags for this Thread