Thread: Loop trouble

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Loop trouble

    First off this website has been my saving grace so far for my intro to C++ class.

    Basically here is where i'm at:

    My instructions are to:

    Write a program solution to print all integer values between 1 and n that are multiples of 5 (i.e. evenly divisible by 5). This should be done with nested while loops. An inner while loop is controlled by a conditional expression determining when the last value for a particular n has been printed. An outer while loop should allow the user to enter a value, provide output for it, and then decide if he will enter a new value or quit.

    So far my code is:

    Code:
     #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int x,a=0;
    	char choice;
    
    	do
    	{
    		system ("cls");
    		cout<< " Enter a number to see all multiples of 5 "<<endl;
    		cin>> x ;
    		cout<<" Multiples " << endl;
    		while (x>=a)
    		
    		{
    			cout<< a << endl;
    			a=a+5;
    			
    		}
    	
    		cout<< " Try another number? Enter Y/N " <<endl;
    		cin>> choice;
    	
    	}	
    	while (choice == 'Y' || choice == 'y');
    
    	
    
    }
    The problem I am having is when I enter Y to run another number, and enter my new number, it doesnt count multiples of 5 from zero, it starts off where I left off on the previous program.

    I just dont know where to go from here.

    Thanks for any help!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Reset variable "a" inside your loop.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Wow, Simply amazing!

    If you were sitting next to me i'd buy you a beer (or whatever you like to drink)

    I cant believe how simple that was!

    Thanks, It is really appreciated!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's an important lesson here: Declare your variables as close to first use as possible.

    You never use a (or x) outside the do...while loop, so you should declare them inside. If you had done that, the problem would never have occurred.

    Instead, it seems that you learned to put all variable declarations at the start of the function. Some people consider it good style, but it's really a relic from C. In C++, you declare variables at the last possible location.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by peter94 View Post
    Wow, Simply amazing!

    If you were sitting next to me i'd buy you a beer (or whatever you like to drink)

    I cant believe how simple that was!

    Thanks, It is really appreciated!
    For remote fans, I usually suggest an iTunes gift card.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM