Thread: Creating a Loop (PLEASE HELP!)

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Unhappy Creating a Loop (PLEASE HELP!)

    I have been having trouble with a really basic program and it's making me feel bad! I narrowed it down to 1 error but I can't figure out what is wrong...

    -------------------------------------------------------------------------------------

    bool end = false;

    It says there is a syntax error here --> while(!end)



    #include <iostream.h>

    int main ()

    {
    char answer;
    int m;
    int n;
    int c;//variables for Pythagorean Theorem

    cout << "Please enter a number.\n";
    cin >> m;//enter the larger number
    cout << "Now enter a number smaller than the first.\n";
    cin >> n;//enter the smaller number
    if(n >= m)//Check for correct input.
    cout << "You entered incorrect input.\n";
    if(n >= m)//I couldn't get it to perform 2 actions.
    return 0;

    c = (m * m) + (n * n); //find the solution
    cout << "Your answer is " << c << '\n';
    cout << "Would you like to perform another problem? (y, n) ";
    cin >> answer;
    if(n)
    end = true;
    return 0;
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Welcome to the boards. First off

    Code tags are nice

    Code:
    #include <iostream.h>
    
    int main ()
    {
    	char answer;
    	int m;
    	int n;
    	int c;//variables for Pythagorean Theorem
    
    	cout << "Please enter a number.\n";
    	cin >> m;//enter the larger number
    	cout << "Now enter a number smaller than the first.\n";
    	cin >> n;//enter the smaller number
    	if(n >= m)//Check for correct input.
    		cout << "You entered incorrect input.\n";
    	if(n >= m)//I couldn't get it to perform 2 actions.
    		return 0;
    
    	c = (m * m) + (n * n); //find the solution
    	cout << "Your answer is " << c << '\n';
    	cout << "Would you like to perform another problem? (y, n) ";
    	cin >> answer;
    	if(n)
    		end = true;
    	return 0;
    }
    umm....there's no while loop in that code...

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Ok, sorry...I jumped the gun a bit on that post and didn't read about code tags...

    Code:
    bool end = false;
    
    while(!end)
    
    
    
    #include <iostream.h>
    
    int main ()
    
    {
    	char answer;
    	int m;
    	int n;
    	int c;//variables for Pythagorean Theorem
    
    	cout << "Please enter a number.\n";
    	cin >> m;//enter the larger number
    	cout << "Now enter a number smaller than the first.\n";
    	cin >> n;//enter the smaller number
    		if(n >= m)//Check for correct input.
    		cout << "You entered incorrect input.\n";
    		if(n >= m)//I couldn't get it to perform 2 actions.
    		return 0;
    		
    	c = (m * m) + (n * n); //find the solution
    	cout << "Your answer is " << c << '\n';
    	cout << "Would you like to perform another problem? (y, n) ";
    	cin >> answer;
    		if(n)
    		end = true;
    	return 0;
    }

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    while(!end)
    you can't have while loops outside of functions. Place it inside of the main function and you'll be fine.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    the loop must be inside of main()

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    while(!end)
    why I cann't find this statement.

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
            bool end = false;
            int i = 0;
            while(!end){
               cout << i << endl;
               i++;
               if(i==3)
                 end = true;
            }
    
            return 0;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    I did that and now it says that "answer" is an undeclared identifier...

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    First, please use the [.code] and [./code] tags. It makes everyone's life much easier.

    Secondly, I'm going to re-write your program. See if you can make heads or tails of it.

    Code:
    #include <iostream>
    
    int main ()
    {
    	char answer;
    	int m, n, c; //variables for Pythagorean Theorem
    
    	std::cout << "Please enter a number.\n";
    	std::cin >> m; //enter the larger number
    	
    	std::cout << "Now enter a number smaller than the first.\n";
    	std::cin >> n; //enter the smaller number
    	
    	if(n >= m) //Check for correct input.
    	{
    		std::cout << "You entered incorrect input.\n";
    		return(0);
    	}
    
    	c = (m * m) + (n * n); //find the solution
    	
    	std::cout << "Your answer is " << c << '\n';
    	std::cout << "Would you like to perform another problem? (y, n) ";
    	std::cin >> answer;
    	
    	tolower(answer); // Convert your answer to lower-case.
    	
    	if(answer == 'n') return(0);
    	else return(0);
    }
    Third, I'm going to ask exactly what you're trying to achieve here.

  10. #10
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    you're using the while loop without any curly braces, meaning it only extends for one command.

    To make the while loop work you need to use:

    {

    }

    example:
    Code:
    while(<test something here>)
    {
    // Code goes here
    }

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    lol, it's a project for school...and I wanted to make it loop for brownie points.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    what's the std:: before cout and cin?

  13. #13
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi ChaosTony,

    If you're trying to calculate the hypotenuse of a right angle triangle I think you'll need a sqrt() in there somewhere.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Hi,

    lol, no hypotenuse...I just need the solution

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    I'd also like to comment that you guys respond REALLY fast...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM