### check integer

This checks for non numerical input but doesn't warn error when a double is entered. How can I correct this?

 ``12345678`` `````` while(ShapeChoice !=1 && ShapeChoice !=2 && ShapeChoice !=3 && ShapeChoice !=4) { cout <<"INVALID INPUT: Enter [1], [2], [3], [4] only."<>ShapeChoice; } ``````
Last edited on
use a cast to turn the number to an integer then do your checks or do this:

 ``12345`` ``````#define isBetween(A,B,C) ( (A <= B ) && (B <= C) ) bool Accept(char shape) { return isBetween('1', shape, '4'); }``````

 ``12`` ``````while ( !Accept ( ShapeChoice ) ) ...``````
Last edited on
`(ShapeChoice != 1 || ShapeChoice != 2)`

Try to evaluate this manually for a moment. If ShapeChoice is 1, then:
 ``123`` ``````(1 != 1 || 1 != 2) (false || true) (true)``````

If ShapeChoice is 2, then:
 ``123`` ``````(2 != 1 || 2 != 2) (true || false) (true)``````

If ShapeChoice is 3 (not 1 or 2), then:
 ``123`` ``````(3 != 1 || 3 != 2) (true || true) (true)``````

There is no way that this expression can evaluate `false`. What you are looking for is the && operator:
`while(cin.fail() || (ShapeChoice !=1 && ShapeChoice != 2 && ...) )`
My code requires me to cin multiple user inputs. But if I enter a double, it doesn't warn an input error but thinks the digit after the decimal place is for the next input. How can I correct this?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````int main() { cout.precision(3); vector PointShape; vector::iterator PointShapeIterator; char RepeatCreateShape; int Count(0); cout<<"=================================================================="<Polygons: Isosceles triangle, rectangle, pentagon, hexagon."<Modifications: translation, rotation, Rescale."<>ShapeChoice; while(cin.fail() || ShapeChoice !=1 && ShapeChoice !=2 && ShapeChoice !=3 && ShapeChoice !=4) { cout <<"INVALID INPUT"<>ShapeChoice; } //================================================================== // Start switch case for shape creation //================================================================== switch (ShapeChoice) { //================================================================== // Triangle //================================================================== case 1: { Count++; double Base(0); double Height(0); cout<<"Enter base length of triangle:"<>Base; while(cin.fail() || Base <= 0) { cout<<"INVALID INPUT"<>Base; } cout<<"Enter height of triangle:"<>Height; while(cin.fail()|| Height <= 0) { cout<<"INVALID INPUT"<>Height; } PointShape.push_back(new Triangle(Base, Height)); cout <<"Vertex coordinates of shape "<GetInfo(); } break; ``````
Instead of using `cin >> value;` I recommend you use `getline(cin, value)`. While getline only supports getting a string you can then convert that string to an int and do a check to ensure it's not a double etc.

e.g.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include #include using namespace std; bool converToInt(const string& value, int &result) { stringstream s(value); if ( !(s >> result) || s.peek() != (int)string::npos) return false; return true; } int main() { string user_input = ""; int user_int = 0; cout << "Please enter your choice (q to quit): "; while(getline(cin, user_input)) { if (user_input == "q") break; if (user_input == "" || !converToInt(user_input, user_int)) { cout << "You choice of " << user_input << " was invalid." << endl; cout << "Please try again" << endl; } else { cout << "Yay! you entered " << user_int << endl; } cout << "Please enter another choice (q to quit): "; } cout << "Finished" << endl; return 0; }``````
How can I make it so that when the input is 1,2,3 or 4, it doesn't have to go through the loop? At the moment, even when the user inputs the correct integer, it loops through so that the user has to input 'q' to progress.

Also, how can I prevent the user from progressing by entering 'q' when he doesn't enter 1,2,3, or 4?

 ``12345678910111213141516171819202122232425262728293031323334`` `````` do { string ShapeChoice=""; int ShapeChoiceInt(0); char RepeatModification; const int DivideZeroFlag(-1); cout<<"Select shape [1] isoceles triangle, [2] Rectangle, [3] Pentagon, [4] Hexagon:"<
Zaita wrote:
 ``1234567`` ``````bool converToInt(const string& value, int &result) { stringstream s(value); if ( !(s >> result) || s.peek() != (int)string::npos) return false; return true; }``````

What relationship does peek() have with string::npos? I would expect that to look more like:
 ``12345678910`` ``````bool converToInt(const string& value, int &result) { typedef std::istringstream in_stream ; in_stream s(value); if ( !(s>>result) || s.peek() != in_stream::traits_type::eof() ) return false; return true; }``````

Last edited on
@cire the return value form eof and string::npos are both -1 so the functionality is the same. Your way is more technically correct I would think, but as it is in C++ there are a million ways to write the same thing.
EOF is not required to evaluate to -1, iirc.
Topic archived. No new replies allowed.