1,105,594 Community Members

Break statments confuse me, need some help

Member Avatar
willow
Newbie Poster
4 posts since Sep 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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.

// exit a loop (Dev-C++)

#include <iostream>

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;
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article