954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Break statments confuse me, need some help

I have tried various break statements and I cannot get my program to break correctly.

Original program is a nested loop asking user for input for either an ascending or descending triangle. That works. But when I try to add a break, it either runs after entering how I want my traingle displayed or asks the wrong cout statement. Program is to run either of the triangles and then ask me if I would like to run again by enter (A)scending or (D)escending. I also am giving the user 'Q' to quit.

I have written this much, but when I incorporate it into my program it does not work.

while((answer != 'Q') && (answer != 'q'))
cout << "Do you want to quit displaying triangle?" << endl;
cin >> answer;
break;

Then I go into my if and else statement for each triangle. Can someone help me understand what I am doing wrong. THANKS.

willow
Newbie Poster
4 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

Breaking out of a loop does not necessarily mean the use of the break statement. The way you wrote the while loop does it for you.
[php]// exit a loop (Dev-C++)

#include

using namespace std;

int main(int argc, char *argv[])
{
char answer;

while((answer != 'Q') && (answer != 'q'))
{
cout << "Do you want to quit displaying triangle?" << endl;
cin >> answer;
// does not need break!
// more code here ...
}

cin.get(); // wait
return 0;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You