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.

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.