Chris Avina Chris Avina - 1 month ago 4
C++ Question

C++ Clearing streams, and testing input - multiple issues

I'm currently learning C++ in class, and I have 3 problems with validation. My professor asked me to not user stringstream for validating input. I'm running into a problem where if the user enters two characters 'rr' for example, the error message displays twice (Which is why I just used stringstream originally to test for the first incorrect character). The second problem is if they enter two values, such as -45 -56. Somehow, the second value of 56 is coming to be positive after the loop and the program is running with it, even after I cleared the stream.
Lastly, if a user enters a float into an int, it truncates it - but how do I prevent that from happening? E.g. I only want an int entered. I tried less than and greater then with no avail. Notice in the below posted code, I do not have the truncating problem because the user input is a double - but I think someone should understand the theory of what I'm asking.

Please let me know if the formatting on here is not correct, I was having some issues with pasting it in the block, but it looks like it may be alright now.

Thank you!

Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;


int main()

{
const double EARTH_G = 1, SPACE_G = 0, EARTH_MOON_G = .17, VENUS_G = .90, MARS_G = .38,
MERCURY_G = .38, JUPITER_G = 2.36, SATURN_G = .92, URANUS_G = .89,
NEPTUNE_G = 1.14, PLUTO_G = .07;

double userWeight;

enum planets {Earth = 1, Space, Moon, Venus, Mars, Mercury, Jupiter, Saturn, Uranus,
Neptune, Pluto};


/*Validates input by making sure checking for only numbers greater than 0*/
{
bool flag = 0;
while (flag == 0)
{
cout << "What is your earth weight in lbs?" << endl;
cin >> userWeight;
if (!static_cast<double>(userWeight) || userWeight < 0)
{
cout << "Invalid Input.\nPlease enter only positive numbers." << endl;
cin.clear();
cin.ignore();


}
else
{
flag = 1;
}

}
}
{
//Displays menu with choices, and allows produces output based on choice.
//Loops while input is invalid.
bool flag = 0;
int choice;
double newWeight;
while (flag == 0)
{
cout << "Select a place in space with a number to see your weight there!\n\n";
cout << "1 - Earth\n\n2 - Space\n\n3 - Earth's Moon\n\n4 - Venus\n\n5 - Mars\n\n"
<< "6 - Mercury\n\n7 - Jupiter\n\n8 - Saturn\n\n9 - Uranus\n\n10 - Neptune\n\n11 - Pluto\n\n";
cout << "Select 1 - 11:\n";
cin >> choice;

switch (choice)
{
case(Earth):
{
newWeight = (userWeight * EARTH_G);
cout << "Your weight on earth is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Space):
{
newWeight = (userWeight * SPACE_G);
cout << "Your weight in space is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Moon):
{
newWeight = (userWeight * EARTH_MOON_G);
cout << "Your weight on the Earth's moon is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Venus):
{
newWeight = (userWeight * VENUS_G);
cout << "Your weight on Venus is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Mars):
{
newWeight = (userWeight * MARS_G);
cout << "Your weight on Mars is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Mercury):
{
newWeight = (userWeight * MERCURY_G);
cout << "Your weight on Mercury is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Jupiter):
{
newWeight = (userWeight * JUPITER_G);
cout << "Your weight on Jupiter is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Saturn):
{
newWeight = (userWeight * SATURN_G);
cout << "Your weight on Saturn is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Uranus):
{
newWeight = (userWeight * URANUS_G);
cout << "Your weight on Uranus is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Neptune):
{
newWeight = (userWeight * NEPTUNE_G);
cout << "Your weight on Neptune is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Pluto):
{
newWeight = (userWeight * PLUTO_G);
cout << "Your weight on Pluto is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
default:
{
cout << "Invalid Input." << endl;
cin.clear();
cin.ignore();
}

}
}
}
}

Answer

According to http://en.cppreference.com/w/cpp/io/basic_istream/ignore , the basic, no parameters ignore only removes one character from the input stream. According to the example provided in the above documentation link, the recommended usage is

input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

To consume the entire line. Note the additional include statement required to get numeric_limits::max.