Oh and that reminds me of one more thing, how can I make it so that a variable only accepts integers? Basically a command that checks if the variable is an integer and asks you again to write something if it's not an integer.
Oh and that reminds me of one more thing, how can I make it so that a variable only accepts integers? Basically a command that checks if the variable is an integer and asks you again to write something if it's not an integer.
An intalways holds an integer. It cannot hold anything else ever.
If you do this....
1 2 3
// prompt the user for an integer:
int foo;
cin >> foo;
...and the user does NOT enter an integer, then the stream (in this case cin) will enter a bad state, which you can check for in a simple if statement:
1 2 3 4
if(!cin)
{
// stream in a bad state. User probably did not input an integer
}
To put the stream back in a good state, you'll need to call clear(). You'll also want to call ignore to wipe out the data left in the input buffer.
Combine all of that with a loop to ask the user again and you can have something like this:
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int x;
cout << "Input an integer: ";
cin >> x;
while(!cin) // bad state?
{
cin.ignore( numeric_limits<streamsize>::max() ); // clear input buffer. IE: erase the
// non-integer stuff they just input
cin.clear(); // tell cin to go back in a good state
cout << "That was not an integer. Try again: ";
cin >> x;
}
}
Yes, it's overly complicated for what should be very simple. Welcome to iostream.
By deafult it is set to EOF so I overwrote it so it ignores until end of the line.
If the snippet I provided wasn't clear enough here is a sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <limits>
int main()
{
int x;
while(std::cout << "Please enter an integer: " && !(std::cin >> x))
{
std::cin.clear(); //clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore anything left in buffer
//cin.ignore(1024, '\n'); would suffice
std::cerr << "That was not an integer.\n";
}
}
Please enter an integer: a
That was not an integer.
Please enter an integer: 1
Thx, it works just I wanted now. If you want to, I could paste the entire program's code right here. So far there's protection against everything I could think of, or rather, I think error-handling would be the better word:
- not entering a number between 1 and 100;
- not entering any number at all;
- entering a number so large, it would normally bug out the program.
And I also shortened a few functions where possible as well as a few class functions
#include "class.h"
usingnamespace std;
//Note: the point of this program is to act as an exercise for classes and objects, which is why it's so long, yet it doesn't do much.
int main()
{
while (1 == 1)
{
int tHappy = 0;
do
{
cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
while (!(cin >> tHappy))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << "Error! You either didn't enter a number or your number was too long! Retry: ";
}
if (tHappy > 100 || tHappy < 0)
{
warning("I said, on a scale of 1 to 100! Retry!: ");
}
} while (tHappy > 100 || tHappy < 0);
int tHunger = 0;
do
{
cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
while(!(cin >> tHunger))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << "Error! You either didn't enter a number or your number was too long! Retry: ";
}
if (tHunger > 100 || tHunger < 0)
{
warning("I said, on a scale of 1 to 100! Retry!: ");
}
} while (tHunger > 100 || tHunger < 0);
vTom obj(tHappy, tHunger);
string choiceUser = "";
cout << endl << endl << obj.retStat() << endl << endl;
do
{
cout << "Repeat?: ";
cin >> choiceUser;
if (choiceUser == "yes")
{
}
elseif (choiceUser == "no")
{
return 0;
}
} while (choiceUser != "yes" && choiceUser != "no");
}
return 0;
}