# help with cin << string

I am attempting to run a while loop:
``````int main() {

double measurement=0;
string unit_of_measurement = " ";
//conversion factors
double meterToCentimeter = 100;
double inchToCentimeter = 2.54;
double footToInches = 12;
double centimeters=0.0;

//define two variables to keep track of which is smallest and largest so far
double smallestSoFar=0.0, largestSoFar=0.0;

cout<<"Enter a float and then hit enter.\n";
vector<double> Values;
unsigned int size_of_values = 0;

while (cin >> measurement >> unit_of_measurement) {   // *** Crashes here when ft or in ***
//convert measurement to centimeters
if (unit_of_measurement == "m")  centimeters = measurement*meterToCentimeter;
if (unit_of_measurement == "in") centimeters = measurement*inchToCentimeter;
if (unit_of_measurement == "ft") centimeters = (measurement*footToInches)*inchToCentimeter;

Values.push_back(measurement);
//Assign the size of the vector to size_of_values
size_of_values = Values.size();
//Using ranged based for loop
for (double i : Values) {
//if this is the first time around the while loop then the first value is both largest and smallest
if (size_of_values==1) {
cout << "The largest and smallest so far is " << measurement << unit_of_measurement <<  "\n";
largestSoFar = centimeters;
smallestSoFar = centimeters;
// else determine if measurement is greater than largestSoFar or smaller than smallestSoFar (which were assigned the first time through the loop
} else {
if (centimeters > largestSoFar) {
largestSoFar = centimeters;
}
if (centimeters < smallestSoFar) {
smallestSoFar = centimeters;
}
}

}
cout << largestSoFar << "cm "  << " is the largest so far \n" << smallestSoFar << "cm " << " is the smallest so far: \n";
cout << "End of Outside For Loop\n\n";
} // end of for loop
return 0;
}
``````
When I enter a double and "m" (bolded above), the program works as I expect.  When I try to use "in" or "ft", the program execution exits the loop and terminates.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Clarification: I get the expected output when I enter a double, whitespace and then a string but not when I enter a double, no whitespace, and then a string.  When I do that, program execution exits the loop and terminates.
0
Commented:
Your cin is parsing based on whitespaces.
If your first entry is not an integer or a double, then the parsing fails.
One way to handle the case of 14.3in is to read those 6 chars into a string, and then parse it yourself.
1
Author Commented:
Why does 14m work fine then?
0
Commented:
I just put your program in VS and 14ft does not crash and the 14 and "ft" are parsed correctly. Looks like VS C++ is parsing the digits up to the first non-digit, and that is the first value going into the double.
If I enter a letter for the first character instead of a number, then the program crashed.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.