Reputation: 828
I need to make a program that reads in the users weight/height.
The user needs to be able to input a number and then a unit of measurement right after. This can be in feet/inches, meters, or cm.
I've gotten all that to work, but the user should also be able to write something such as 5' and have the inches be optional.
Here's the problem I'm having:
I have two variables I'm using for user input, one double (height), and one string (unitHeight). that works fine for m and cm, but for feet/inches I needed to add two more since the user needs to input two strings and two numbers (I kept it as doubles for simplicity's sake).
so I used an if statement:
if (unitHeight == "'"){
cin >> height2;
cin >> unitHeight2;
}
The problem is that I need to make it so that when the user inputs x' (x being whatever number), the program doesn't ask for any further input.
Upvotes: 1
Views: 2165
Reputation: 206567
Read the stdin one line at a time. Process each line. If a line contains a second number (for height) and second string (for the unit), then you have the feet+inches specification. Otherwise, you just have one number and one unit.
const int maxLength = 200;
while (true)
{
char line[maxLength+1];
std::cin.getline(line, maxLength);
if ( !cin.good() )
{
break;
}
std::istringstream str(line);
double height1;
std::string unit1;
double height2;
std::string unit2;
bool secondHeightFound = false;
str >> height1 >> unit1 >> height2;
if ( str.good() )
{
str >> unit2;
secondHeightFound = true;
}
}
Upvotes: 0
Reputation: 106076
What to do depends a bit on what other input (if any) might come after the height, and how you want to handle errors, but to get you started:
int height2 = 0;
if (unitHeight == "'" && cin >> height2)
{
if (!(cin >> unitHeight2))
{
std::cerr << "hey, " << height2 << " what? give me units baby!\n";
exit(EXIT_FAILURE);
}
// if we get here, then we have height2 and unitHeight2 to work with...
...
}
else if (cin.eof())
{
// might have hit EOF without inches, that could be legal - depends on your program
...
}
else
{
// saw some non-numeric input when expecting height2 - is that ok?
...
}
Since you posted, you've added a comment saying that you specifically want this input on a single line, after which the user can be expected to press enter. To handle that, surround the above code with std::string line; if (getline(std::cin, line)) { std::istringstream iss(line); >>above code goes here<< } else { ...couldn't read a line of input...}
.
Separately, you say:
the user needs to be able to input a number and then a unit of measurement right after. this can be in feet/inches, meters, or cm. ive gotten all that to work
...I hope so, but note that it's a bit tricky when supporting e.g. 5'11" and 180cm, as cin >> height1 >> unitHeight1
, when unitHeight1
is a std::string
, will read "'11". If you make unitHeight1
a char
then it will tend to read only the "c" from "cm", so neither type works for both notations. You'd best read a char then use it to decide whether to read another....
Upvotes: 2
Reputation: 101
You can do something like that:
string height;
cin >> height;
for(int i = 0; i < height.size(); i++) {
if(height[i] == "'"[0]) {
cout << "It's ok!" << endl;
break;
}
}
Upvotes: 0