Need help with very simple C++ program


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.

Please take a look. Thank you very much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace 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; 
}
just change your while loop to: while (again != "n" || again != "N")
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?

guatemala007: Thanks for the reply
Topic archived. No new replies allowed.