Okay, this is a very simple "Calculate the area of a triangle" question for an intro to Comp Sci class. The program works fine. I added a loop to ask the user if he/she wants to calculate another area, and that works fine as well.
The only problem is, when I ask "Would you like to calculate the base of another triangle?", I'm trying to test for when a user enters an inappropriate response (like a number instead of Y or N), and then restart the loop. For some reason, I cannot restart the loop.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string again = "y";
float base = 0.0, height = 0.0, area = 0.0;
cout << setw(40) << setfill('*') << "*" << endl;
cout << " Calculate the Area of a Triangle " << endl;
cout << setw(40) << setfill('*') << "*" << endl << endl;
while (again == "y" || again == "Y")
{
cout << "Enter the Triangle's Base: ";
cin >> base;
cout << "Enter the Triangle's Height: ";
cin >> height;
cout << endl;
area = (base * height) / 2;
cout << "The Area of the Triangle is: " << area << endl << endl;
cout << "Would you like to calculate the base of another triangle?" << endl;
cout << "Press Y for Yes or N for No: ";
cin >> again;
cout << endl;
if (again != "y" && again != "Y" && again != "n" && again != "N")
{
again = "y"; // THIS seems to be the problem. The Dev C++ compiler is ignoring my resetting the value to "y". Am I doing something wrong?
cout << "Input error." << endl;
}
}
cout << endl;
system ("pause");
return 0;
}
Uhgh. The program is working fine as is. It was the Dev C++ debugger mode that wasn't working. Does anyone know of a more modern, Free C++ compiler/debugger, for Windows?