I'm designing a number guessing algorithm for use as a game.
Can anyone suggest why does the algorithm end after the first condition?
#include <iostream>
using namespace std;
int main()
{
int num = 5;
int guess;
cout << "Guess a number \n";
cin >> guess;
if (guess==num)
{
cout << "You guessed the correct number \n";
}
else if (guess < num)
{
cout << "Your guess is lower than the number \n";
cout << "Guess again \n";
cin >> guess;
}
else
{
cout << "Your guess is higher than the number \n";
cout << "Guess again \n";
cin >> guess;
}
return 0;
}
You need some kind of loop if you want that the algorithm would be repeated.
For example
#include <iostream>
using namespace std;
int main()
{
int num = 5;
int guess;
cout << "Guess a number \n";
do
{
cin >> guess;
if ( guess == num )
{
cout << "You guessed the correct number \n";
}
else if ( guess < num )
{
cout << "Your guess is lower than the number \n";
cout << "Guess again \n";
}
else
{
cout << "Your guess is higher than the number \n";
cout << "Guess again \n";
}
} while ( guess != num );
return 0;
}
If you wish to guess again I'd recommend a loop. Otherwise your code works as intended.
while(number != guess)
{
if(number * 2 < guess){
cout << "Way to high. Try again." << endl;
cin >> guess;
}
if(number / 2 > guess)
{
cout << "Tip : My number is NOT low. Try again." << endl;
cin >> guess;
}
if(number < guess)
{
cout << "To high try something lower. Feed me a number." << endl;
cin >> guess;
}
if(number > guess)
cout << "To low, try again." << endl;
cin >> guess;
}
Related
{
int i,game = 0,guess,count = 0;
int rando;
srand (time(0));
rando = rand() % 50 + 1;
cout << "****Welcome To The Game****\n";
cout << "1: Start the game\n";
cout << "2: End the game\n";
cin >> game;
while (game != 2 && count != 11)
{
cout << "Enter a number between 1 and 50: ";
cin >> guess;
count++;
if(guess == rando)
{
cout << "you got it!\n";
cout << "would you like to play again?\n";
cin >> game;
}
else if(guess < rando)
{
cout << "too low, try again " << endl;
}
else if(guess > rando)
{
cout << "Too high, try again" << endl;
}
if (count == 11)
{
cout << "too many guesses. the number was: " << rando << "." << endl;
cout << "would you like to play again?\n";
cin >> game;
}
if (game == 2)
{
cout << "### Thank you. See you next time.###\n";
}
}
return 0;
}
when I start the game, it generates a new number every time I run the program, but not when I ask to play again. I tried putting it in the while, but that messes up the code. how do I fulfill the second option in the first sentence?
int randomGame()
{
int gameMenuChoice = 0;
srand(time(0));
std::cout << "****Welcome To The Game****\n";
std::cout << "1: Start the gameMenuChoice\n";
std::cout << "2: End the gameMenuChoice\n";
std::cin >> gameMenuChoice;
while (gameMenuChoice != 2)
{
const int rando = rand() % 50 + 1;
int guess = 0;
for (int count = 0; count < 11 && guess != rando; ++count)
{
std::cout << "Enter a number between 1 and 50: ";
std::cin >> guess;
if (guess < rando)
{
std::cout << "too low, try again \n";
}
else if (guess > rando)
{
std::cout << "Too high, try again\n";
}
}
if (guess == rando)
{
std::cout << "you got it!\n";
}
else
{
std::cout << "too many guesses. the number was: " << rando << ".\n";
}
std::cout << "would you like to play again?\n"; // I think people will enter Y for yes here.
std::cin >> gameMenuChoice;
}
std::cout << "### Thank you. See you next time.###\n";
return 0;
}
I have split you menu and game routine loops and simplified the logic a bit.
I'm trying to make a validation loop in C++ that checks the user's input until they enter a number between 0 and 100 and however my loop only checks the first condition. Any guidance is appreciated!
#include <iostream>
using namespace std;
int main()
{
const int max_num = 100;
const int min_num = 0;
int num;
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
do {
if (!(cin >> num))
{
cout << "ERROR:The value provided was not a number" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
else if (num<min_num || num>max_num)
{
cout << "ERROR: value out of range" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
} while (!(cin >> num) || (num<min_num || num>max_num));
return 0;
}
Add lots of logging to your code so that you know what it's doing. This will help you find the problem. For example, instead of:
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
Try:
cout << "Enter a number between 0 and 100" << endl;
cerr << "About to read into num outside the loop" << endl;
cin >> num;
cerr << "Read into num outside the loop, got: " << num << endl;
And so on, throughout your code. This should give you enough information to find the bug. Alternatively, use a debugger with a single step function to accomplish the same thing.
Check that in the part of while:
instead of
while (!(cin >> num) || (num<min_num || num>max_num));
this:
while (!cin || (num<min_num || num>max_num));
the same for the upper if
cin >> num means putting user input to the variable num . So you are trying to take user inputs 2 times in the loop. Maybe the check condition: (num == (int)num)will solve your problem. It will try to verify the number you have stored in num is really of the type int
im trying to learn c++ and can't figure out the reason for this error it seems to match up with my other cout lines that aren't getting any errors
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int number = 42;
int guess;
bool notguessed = true;
while (notguessed);
cout << "Guess my number";
cin >> "Guess";
if (guess == number)
{
cout << "You got it" << endl;
notguessed = false;
}
if (guess < number)
{
cout << "To low try again" << endl;
}
if (guess > number)
{
cout << "To high try again" << endl;
}
return 0;
}
Try this code :-
# your code goes here
#include <iostream>
using namespace std;
int main()
{
int number = 42;
int guess;
bool notguessed = true;
cout << "Guess my number";
while (notguessed)
{
cin >> guess;
if (guess == number)
{
cout << "You got it" << endl;
notguessed = false;
}
if (guess < number)
{
cout << "To low try again" << endl;
}
if (guess > number)
{
cout << "To high try again" << endl;
}
}
return 0;
}
You were trying to input a string "Guess". Change that to cin>>guess.
And change the while loop semicolon.
Your program has two errors - Compilation error and Logical Error.
Compilation Error - Consider this code segment -
cin >> "Guess";
Here you trying to give a value to a constant. What you intended to do is -
cin>>guess
Logical Error - When you make above changes your code will compile fine but will not work as you want it to, this is because of the following line -
while (notguessed);
The above while loop will run infinitely as notguessed is true and no where in the loop its value is modified.
Change that to -
while (notguessed){
cout << "Guess my number";
cin >> guess;
if (guess == number)
{
cout << "You got it" << endl;
notguessed = false;
}
else if (guess < number)
{
cout << "Too low try again" << endl;
}
else
{
cout << "Too high try again" << endl;
}
}
Note - I converted your simple ifstatements to if else if, this is to avoid unecessary checking of other ifs when one if already evaluated to true.
There is an alternative approach to the above where you are not required to use the variable notguessed, by using break keyword -
while (true){
cout << "Guess my number";
cin >> guess;
if (guess == number)
{
cout << "You got it" << endl;
break;
}
else if (guess < number)
{
cout << "Too low try again" << endl;
}
else
{
cout << "Too high try again" << endl;
}
}
Make change in line 10
while loop with ;
remove ;
provide some
while(condition){// do code }
Make change in line 13
you mistakenly ""
it means string
you need variable
String guess;
cin >> guess;
cin >> "Guess";
should be
cin >> guess;
This is my first simple program. It keeps printing out Guess what it is. non-stop and doesn't even ask for user input. (the next line of code.)
What is my mistake?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
return 0;
}
You need to use brackets
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
{
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
return 0;
}
By default if you don't use them only the next line will be considered part of the while loop
in your case:
while (conti)
cout << "Guess what it is. \n";
you need braces for the while loop, or it will execute just that single statement forever.
while (conti)
cout << "Guess what it is. \n";
is equivalent to:
while (conti)
{
cout << "Guess what it is. \n";
}
i.e. the loop ends there. What you need is provide the opening and closing braces for the loop at the right place.
while (conti)
{
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
You have missed the braces for while loop. You may try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti){
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
return 0;
}
You're missing two curly braces to widen the scope of your while-loop. Note that without curly braces the scope of any loop in C++ will stop at the first semicolon. Here's a working solution:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
{ // <-- This curly brace was missing
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
} // <-- This curly brace was also missing
return 0;
}
well its a simple project it does everything as it should minus it wont exit when it is suppose to.
I have tried many different ways including goto statements I tried a if loop but got nothing but errors. The current code gives no errors just I dont know where to go to make it exit. It doesnt have to be fancy this is only my second program
// C// Guess My Number
// The classic number guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int secretNumberE = rand() % 10 + 1;
int secretNumberM = rand() % 100 + 1;// random number between 1 and 100
int secretNumberH = rand() % 1000 + 1;
int tries = 0;
int guess;
char play;
{
cout << "\tWelcome to Guess My Number\n\n";
START:
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Easy.\n";
do
{
cout << "Enter a guess 1-10: ";
cin >> guess;
++tries;
if (guess > secretNumberE)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumberE)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumberE);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n')
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 2:
cout << "You picked Normal.\n";
do
{
cout << "Enter a guess 1-100: ";
cin >> guess;
++tries;
if (guess > secretNumberM)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumberM)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumberM);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n')
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 3:
cout << "You picked Hard.\n";
do
{
cout << "Enter a guess 1-10: ";
cin >> guess;
++tries;
if (guess > secretNumberH)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumberH)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumberH);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n')
{
cout << " Thank you for playing. ";
return 0;
}
break;
default:
cout << "You made an illegal choice.\n";
goto START;
return 0;
}}}
if ( play = 'y' ){
goto START;
}
else if (play = 'n')
{
cout << " Thank you for playing. ";
return 0;
}
The first if will always be true, because you are using the assignment operator rather than the equality operator. You need to use == to compare two values.
...
char play;
{
...
There seems to be an extra { in your code that you are wrapping your stuff with.
Things that are clear to me that you need to work on, indentation. There is probably an auto-format shortcut in your IDE. Start using that. It's impossible to read your code well this will lead to a bunch of errors in the long run for simple things that you will be banging your head over.
goto highly recommend not using this unless you know what your doing.
Start learning functions/methods they will save your life and overall improve you workflow.
and you can replace goto with it.
example of a function..
int startGame(){
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";
int choice = 0;
cout << "Choice: ";
cin >> choice;
return choice;
}
Other things.
if ( play = 'y' ) needs to be if ( play == 'y' ) rinse repeat this because you are trying to check for equality(conditional) not setting a value which is what one = does.
Also, ALWAYS INITIALIZE VALUES int choice; should be int choice = 0; or some default value.
use the "break" keywork
do
{
if(mycondition)
{
break;
}
} while (loopCondition);