I am struggling to get an input validation working for my game.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int iGumballs;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
cin.clear();
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
cin >> iUserguess;
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
}
iGuesses ++;
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
I basically want the program to display
Your Guess: Sorry, incorrect input - try again
When the user enters a number less then 1, and higher then 1000, as well as some sort of validation which makes sure a number is entered instead of a letter or symbol. I tried cin.fail() but I couldn't get it quite to work.
Thanks,
John
You will need some test to see if is a number or not, try this:
#include <iostream>
#include <string>
#include <ctime>
#include <boost/lexical_cast.hpp> //dependency that can be optional
using namespace std;
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int main()
{
int iGumballs;
std::string iUserguessStr;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
cin.clear();
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
cin >> iUserguessStr;
if(is_number(iUserguessStr))
iUserguess = boost::lexical_cast<int>(iUserguessStr); //you can make your own or maybe use lexical cast to transform a string into integer
else
continue; //put some fancy message here warning the user
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
}
iGuesses ++;
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
My answer is based on a related problem: How to determine if a string is a number with C++?
Yo can use if(cin) to check the state of the input stream and since operator>> will return the input stream that was passed to it you can use if(cin>>iUserguess)
If cin is in a failed state -maybe because the user entered a non number- the expression if(cin>>iUserguess) will evaluate to false.
If the user enters a non number you will need to call cin.clear() to clear the stream state and cin.ignore() to discard the input, before trying to read a number again.
so using your example, it could be changed to this:
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
int iGumballs;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
if(cin >> iUserguess)
{
iGuesses ++;
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
continue;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
continue;
}
}
else
{
cout<<"incorrect input - try again\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
continue;
}
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
To validate characters you could use the try-catch structure.
-First read in a string and try Typecasting it and handle the errors with try-catch.
-Then use conditions to make sure the input is in range.
-If the input isn't valid, you can write an error message to display.
Related
#include <iostream>
using namespace std;
bool play_game(int n) {
int guess;
bool noguesses = false;
int numofguesses = 0;
cout << "Welcome to my number guessing game\n";
while (n!=guess && !noguesses)
{
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false;
}
else
{
oog = true;
}
}
if (noguesses) {
cout << "I'm sorry. You didn't find my number.\n";
cout << "It was" << n << endl;
}
else
{
cout << "\n";
cout << "You found it in" << numofguesses << "guess(es)\n";
return true;
}
}
int main()
{
int secretnum = 5;
play_game(secretnum);
}
When I run this, the program stops after cout << "You entered: " << guess;. I want it to keep looping until the number of guesses reaches 6, or until the user inputs the correct answer.
Remove return false;
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false; //Remove this line
}
I am just studying so don't judge me hard please.
I have a problem. I know how to do a do-while loop. But today I have learned about functions. So I made do-while loops in functions and they are looping infinitely. How do I stop the loops?
#include <iostream>
using namespace std;
void text()
{
cout << "Log in to see the Menu. " << endl;
}
void lg()
{
const string login = "el1oz";
string input;
cout << "Login > " << flush;
cin >> input;
do{
if(login == input){
break;
}
else{
cout << "Try again." << endl;
}
}while(true);
cout << "Correct Login! " << endl;
}
void pw()
{
const string password = "Mau01171995";
string input1;
cout << "Password > " << flush;
cin >> input1;
do{
if(password == input1){
break;
}
else{
cout << "Try again. " << endl;
}
}while(true);
cout << "Correct Passsword! " << endl;
}
int main()
{
text();
lg();
pw();
return 0;
}
You're not changing input after the code enters in the loop. You should put the cin >> input inside the loop.
Also consider when to use a while loop vs a do while loop. In this case a while loop is better.
You probably should not use using namespace std; (More information here).
You should use more descriptive names.
#include <iostream>
using std::cin;
using std::string;
using std::cout;
using std::flush;
using std::endl;
void printWelcome()
{
cout << "Log in to see the Menu. " << endl;
}
void inputUser()
{
const string login = "el1oz";
string input;
cout << "Login > " << flush;
while(cin >> input){
if(login == input){
break;
}
else{
cout << "Try again." << endl;
}
}
cout << "Correct Login! " << endl;
}
void inputPassword()
{
const string password = "Mau01171995";
string input;
cout << "Password > " << flush;
while(cin >> input){
if(password == input){
break;
}
else{
cout << "Try again. " << endl;
}
}
cout << "Correct Passsword! " << endl;
}
int main()
{
printWelcome();
inputUser();
inputPpassword();
return 0;
}
Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.
So I'm trying to create a feature that will let me quit my game once you've completed it, or you have the option to play again. The replay option works, but whenever I try the quit part, it just goes back to the start of the game.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main() {
SetConsoleTitle("Guess the number! | v1.0");
int replay;
replay = 1;
int g, n;
play:
g = NULL;
srand(time(NULL));
n = rand() % 20 + 1;
system("cls");
cout << "Number guessing game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "The random number has been generated. It is between 1 and 20" << endl;
system("pause");
while (g != n) {
system("cls");
cout << "Number Guessing Game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "Type a guess between 1 and 20 then press ENTER" << endl;
cout << "Your guess: ";
cin >> g;
}
if (g = 1113) {
goto debugskip;
}
debugskip:
system("cls");
cout << "You have found the number!" << endl;
cout << "The number was " << n << endl;
cout << "Would you like to play again? 1 for yes, 2 for no.";
cin >> replay;
if (replay = 1) {
goto play;
}
else if (replay = 2) {
exit(1);
}
return 0;
}
You are using assignment operator instead of equals operator in your ifs.
Change
if (replay = 1) {
for
if (replay == 1) {
And do the same in the other places with the same problem.
I am making a simple guess the number game using C++.
My program checks if the user input is an integer or not.
But when I input for example "abc" the program keeps saying: "Input a number!" instead of saying it once and let the user input something again..
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int chances = 3;
void ask();
void checkAnswer(int ans);
void defineNumber();
int correctAnswer;
void defineNumber(){
srand(time(0));
correctAnswer = rand()%11;
}
void checkAnswer(int ans){
if(ans == correctAnswer){
cout << "The answer was right!\n" << endl;
exit(0);
}else{
if(chances > 0){
cout << "Wrong answer, try again!\n" << endl;
chances--;
ask();
}else{
cout << "You lost!" << endl;
exit(0);
}
}
}
void ask(){
int input;
cout << correctAnswer << endl;
try{
cin >> input;
if(input > 11 || input < 0){
if(!cin){
cout << "Input a number!" << endl; //HERE LIES THE PROBLEM
cin.clear(); //I TRIED THIS BUT DIDN'T WORK AS WELL
ask();
}else{
cout << "Under 10 you idiot!" << endl;
ask();
}
}else{
checkAnswer(input);
}
}catch(exception e){
cout << "An unexpected error occurred!" << endl;
ask();
}
}
int main(){
cout << "Welcome to guess the number!" << endl;
cout << "Guess the number under 10: ";
defineNumber();
ask();
}
Thanks in advance.
Try this:
try{
cin >> input;
if (cin.good()) {
if(input > 11 || input < 0) {
cout << "Under 10 you idiot!" << endl;
ask();
} else {
checkAnswer(input);
}
} else {
cout << "Input a number!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
ask();
}
}catch(exception e){
cout << "An unexpected error occurred!" << endl;
ask();
}
and don't forget to use this at the beginning: #include <climits>
The cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); line will ignore everything until the next int number.. Therefore it will not loop anymore..