Problem with while statement

I am trying to get my program to ask for a character input, and based on that input, ask the user to put in different numerical values for certain questions. I tried to get the first portion done and when I enter "a" it terminates and says "Done". What am I doing wrong? Here's the code;

// Program to input the legs of a right triangle
// Calculate and print the hypotenuse
// a^2 + b^2 = c^2

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
double a,b, // triangle legs;input by user
c ; // triangle hypotenuse; calculated by program
char answer;

// print decimals with 2 places
cout << fixed << showpoint << setprecision (2);

cout<< "==================================================\n\n";
cout<< "\tCalculations with Geometric Solids\n\n";
cout<< "==================================================\n\n";
//Prompt the user to enter a character
// to select a solid
cout<<"This program will perform some calculations for a ";
cout<<"\nsphere, a cylinder or a rectangular solid.\n";
cout<<"\nTo calculate for a cylinder, input a (or A) ";
cout<<"\nTo calculate for a sphere, input b (or B )";
cout<<"\nTo calculate for a rectangular prism, input c ( or C)";
cout<<"\n\nInput a (or A), b (or B), or c (or C) ";

answer = 'a(orA),b(orB),c(orC)';

cin>>answer;

while (answer == 'a(orA)')
{
cout << "\nInput triangle legs ";
cin >> a >> b;
cout <<"Please input the value for the legs:" << endl<<endl;
cout << "\ta = " << a <<endl;
cout << "\tb = " << b << endl<<endl;

c = sqrt (a*a + b * b ) ;
cout <<"The length of the hypotenuse is " << c << "."<< endl;

cout << "\n\nWould you like to enter the legs for more triangles? Yes or no? ";
cin >> answer;
}

cout << "\n\nDone! \n\n";

return 0;
}
What do these two statement mean?

answer = 'a(orA),b(orB),c(orC)';
while (answer == 'a(orA)')

Can you say what value answer contains?



Last edited on
You can see what minor changes I've made and the print out.
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
45
46
47
48
49
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
double a,b, // triangle legs;input by user
c ; // triangle hypotenuse; calculated by program
char answer;

// print decimals with 2 places
cout << fixed << showpoint << setprecision (2);

cout<< "==================================================\n\n";
cout<< "\tCalculations with Geometric Solids\n\n";
cout<< "==================================================\n\n";
//Prompt the user to enter a character
// to select a solid
//cout<<"This program will perform some calculations for a ";
//cout<<"\nsphere, a cylinder or a rectangular solid.\n";
//cout<<"\nTo calculate for a cylinder, input a (or A) ";
//cout<<"\nTo calculate for a sphere, input b (or B )";
//cout<<"\nTo calculate for a rectangular prism, input c ( or C)";
//cout<<"\n\nInput a (or A), b (or B), or c (or C) ";

//answer = 'a(orA),b(orB),c(orC)';

//cin>>answer;

//while (answer == 'a(orA)')
//{
cout << "\nInput triangle legs ";
cin >> a >> b;
//cout <<"Please input the value for the legs:" << endl<<endl;
//cout << "\ta = " << a <<endl;
//cout << "\tb = " << b << endl<<endl;

c = sqrt ((a*a) + (b * b) ) ;
cout <<"The length of the hypotenuse is " << c << "."<< endl;

cout << "\n\nWould you like to enter the legs for more triangles? Yes or no? ";
cin >> answer;
//}

cout<< "\n\nDone! \n\n";

return 0;
}
/*
==================================================

Calculations with Geometric Solids

==================================================


Input triangle legs 3 4
The length of the hypotenuse is 5.00.


Would you like to enter the legs for more triangles? Yes or no? n


Done!


Process returned 0 (0x0) execution time : 22.891 s
Press any key to continue.*/
Topic archived. No new replies allowed.