Thread: While loop issues

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    While loop issues

    I need to write a program that randomly generates multiplication problems based on how many the user says they want. So far this is what i've got:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main () {
    	
    	int number, initialnumber, first, second, guess, answer;
    
    	//Seed random number
    	srand(time(0));
    	
    	printf("How many problems do you want?\n");
    	scanf("%d", &initialnumber);
    	
    	number = initialnumber;
    	
    	int start = time(0);
    	while (number > 0) {
    	
    		//Create problem numbers
    		first = 1 + rand()%12;
    		second = 1 + rand()%12;
    		answer = first * second;
    
    		printf("Answer: %d x %d\n", first, second);
    		scanf("%d", &guess);
    		
    		if (guess == answer)
    			number --;
    		else
    			printf("Incorrect, try again.\nAnswer: %d x %d\n", first, second);
    			number --;
    		}
    	int end = time(0);
    	int timespent = end - start;
    	printf("You completed %d problems in %d seconds.\n", initialnumber, timespent);
    
    	return 0;
    }
    The problem I have is if they get it wrong it's supposed to display the same question again. I know that the else statement is completely useless as it doesn't even scan in their answer, I just put it there when I was brainstorming. How can I get it to check the new answer they've given instead of looping back around? Do I need to do something other than a while loop?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What you need is an inner loop where the exit condition is a correct answer...
    Code:
    	while (number > 0) {
    	
    		//Create problem numbers
    		first = 1 + rand()%12;
    		second = 1 + rand()%12;
    		answer = first * second;
     do
         { 
    		printf("Answer: %d x %d\n", first, second);
    		scanf("%d", &guess);
    
    
    		
    		if (guess == answer)
    			number --;
    		else
    			printf("Incorrect, try again.\nAnswer: %d x %d\n", first, second);
    			number --;
    
    }
    while (guess != Answer) 
    		}
    Obviously this isn't the total solution, but it should give you some idea how to set it up.

  3. #3
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    Aha I got it! Thank you!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    My pleasure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Newb needs help with loop issues
    By FatalError in forum C Programming
    Replies: 15
    Last Post: 09-04-2004, 02:19 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM