Having Problems With a Do-While Loop

#include <iostream>
using namespace std;
int main()
{
float height;
float weight;
int age;
float hatSize;
float jacketSize;
float waistSize;
char answer;
cout << "Please enter your height in inches.\n";
cin >> height;
cout << "Please enter your weight in pounds.\n";
cin >> weight;
cout << "Please enter your age.\n";
cin >> age;
do
{
cout << "Your hat size is " << (weight / height) * 2.9 << ".\n";
if (age > 30)
{
cout << "Your jacket size is " << ((height * weight) / 288) + (((age - 30) / 10) / 8) << " inches.\n";
}
else
{
cout << "Your jacket size is " << ((height * weight) / 288) << " inches.\n";
}
if (age > 28)
{
cout << "Your waist is " << (weight / 5.7) + ((age - 28) / 10) << " inches.\n";
}
else
{
cout << "Your waist is " << (weight / 5.7) << " inches.\n";
}
cout << "Again? (yes/no)\n";
cin >> answer;
} while (answer == 'yes');
return 0;
}

I need the program to repeat itself whenever the user inputs "yes" at (cin >> answer;). I've tried multiple ways and none have worked. Right now, it will not compile. Here's the error:

hw3_ch3_jforbes.cpp:39:21: warning: multi-character character constant
hw3_ch3_jforbes.cpp: In function ‘int main()’:
hw3_ch3_jforbes.cpp:39: warning: comparison is always false due to limited range of data type
char is a single character. That is, it can hold a 'y', but not 'yes' (because that's 3 characters).

What you want is a string:

1
2
3
4
5
6
7
8
9
#include <string>

//...

string answer;

//...

while( answer == "yes" ); // <- note:  "double" quotes, not 'single' quotes 
An object of type char can store only one character. So you cannot compare it with a multicharacter literal as you are doing

} while (answer == 'yes');

So either compare answer with one character or use type std::string instead of char for variable answer. For example

} while (answer == 'y');

or

std::string answer;
....
} while (answer == "yes");
Last edited on
Topic archived. No new replies allowed.