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);
Related
i am making a word guessing game in c++ (im new at programming btw) im just gonna ask how can i make the "already-answered" answers as wrong and cant get points from the same word again? here's my current code...
#include <iostream>
#include <string>
using namespace std;
int main()
{
int firstQPoint, secondQPoint, thirdQPoint, mane;
char yourChoice, levelChoice, goBack;
string yourFirstAnswer, yourSecondAnswer, yourThirdAnswer;
gameMenu:
cout << "\t GUESS THE WORD GAME";
cout << "\t\n\n MADE BY GROUP FIVE";
cout << "\t\t\n\n 1. PLAY | ENTER \"1\" TO PLAY ";
cout << "\t\t\n\n 2. QUIT | ENTER \"2\" TO QUIT ";
cout << "\t\t\n\n 3. RULES | ENTER \"3\" TO SEE THE RULES";
cout << "\t\t\n\n What Do You Want To Do : ";
cin >> yourChoice;
if (yourChoice == '1') {
cout << "\t GUESS THE WORD GAME";
cout << "\t\n\n MADE BY GROUP FIVE";
selectALevel:
cout << "\t\n\n OKAY, CHOOSE A LEVEL (1-3) : ";
cin >> levelChoice;
switch (levelChoice) {
case ('1'):
cout << "\t GUESS THE WORD GAME";
cout << "\t\n\n MADE BY GROUP FIVE";
cout << "\t\t\n\nGIVE 3 BODY PARTS THAT STARTS WITH LETTER \"T\"";
cout << "1 : ";
cin >> yourFirstAnswer;
if (yourFirstAnswer == "TOE", "TONGUE", "TOOTH") {
cout << "\n\n\t\tNICE, YOU GOT A POINT!";
firstQPoint = 1 + 0;
}
cout << "2 : ";
cin >> yourSecondAnswer;
if (yourSecondAnswer == "TOE", "TONGUE", "TOOTH") {
cout << "\n\n\t\tNICE, YOU GOT A POINT!";
secondQPoint = 1 + firstQPoint;
}
cout << "3 : ";
cin >> yourThirdAnswer;
if (yourThirdAnswer == "TOE", "TONGUE", "TOOTH") {
cout << "\n\n\t\tNICE, YOU GOT A POINT!";
thirdQPoint = 1 + secondQPoint;
}
break;
case ('2'):
break;
case ('3'):
break;
default:
goto selectALevel;
}
}
else if (yourChoice == '3') {
do {
cout << "\t GUESS THE WORD RULES";
cout << "\t\t\n\n1. ONLY USE UPPERCASE LETTERS";
cout << "\t\t\n\n1. ONLY USE SINGULAR WORDS";
cout << "\t\t\n\n ENTER \"1\" TO GO BACK : ";
cin >> goBack;
if (goBack == '1') {
goto gameMenu;
}
} while (goBack != '1');
}
else if (yourChoice == '2') {
cout << "\t\t\n\n Okay, Goodbye!";
}
return 0;
}
i tried the longer way, where i will manually code it like this
#include <iostream>
using namespace std;
int main()
{
int number, again;
cout << "give 2 number";
cin >> number;
cout << "again :";
cin >> again;
if (number == 1 && again == 2) {
cout << "correct";
else if (number == 2 && again == 1)
{
cout << "correct";
}
}
}
but it's very hard since im working with too many combinations! thanks in advance for answering!
Inside each switch case you can make a loop to repeat the question if the word is wrong.
At the beginning of the switch (before the loop) you can create an empty list where you will store every answer they give.
In the end you only need to make a function that goes through that same list and checks if a word is inside it or not.
{
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.
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;
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;
}
when press "1" to start the game a error message comes up first instead of playing the game and then I have to enter "1" 3 times before the game starts and also the quit game option only works when you select "2" first if its not selected first it just comes up as a error message I cant see why it does this can anyone help me please ?
#include "Questions.h"
#include <iostream>
using namespace std;
const int MAXITEMS = 10;
int main ()
{
string question[MAXITEMS] = {"How man cards in a suit",
"How_many_suits_are_there_in_a_standard_pack_of_card",
"How_many_kings_are_in_a_standard_pack_of_cards"};
string answers[MAXITEMS] = {"4", "5", "6"};
int userInput = 0;
int tries = 0;
bool isGameOver = false;
cout << "select 1 to start game" << endl; //gives option to start and quit game
cout << "select 2 to quit game" << endl;
cin >> userInput;
if (userInput == 2)
{
isGameOver = true;
return 0;
};
// when game starts gives option to select question and shows all questions
do
{
if (userInput != 1||2)
{
cout << " Your input is not valid! please try again:" << endl;
// try switch cases for the different outcomes
cout << "select 1 to start game" << endl;
cout << "select 2 to quit game" << endl;
cin >> userInput;
while (!(cin >> userInput))
{
cin.clear(); // clear the error flags
cin.ignore(INT_MAX, '\n'); // discard the row
cout << "Your input is not valid! please try again: ";
cout << "select 1 to start game" << endl;
cout << "select 2 to quit game" << endl;
}
cout << userInput << endl;
}
// reprisent all characters as number to stop while roblem
if(userInput == 1)
{
do
{
cout << "select question" << endl;
for(int i = 0; i != MAXITEMS; i++)
{
cout << i << " " << question[i] << endl;
}
int selectQestion;
cin >> selectQestion;
if(selectQestion == 0||1||2 && tries != 2)
{
cout << "Enter your answer" << endl;
string userAnswer;
cin >> userAnswer;
while (!(cin >> userAnswer))
{
cin.clear(); // clear the error flags
cin.ignore(INT_MAX, '\n');
// discard the row
cout << "Your input is not valid!
please try again: ";
}
if (userAnswer == answers[0])
{
cout << "Correct answer" << endl;
}
else{
cout << "incorrect try again" << endl;
tries++;
cin >> userAnswer;
if (userAnswer == answers[0])
{
cout << "Correct answer" << endl;
}
else
cout << "Incorrect" << endl;
}
}
if (selectQestion == 0 ||1 ||2 && tries == 2)
{
cout << "you can no longer answer this question" << endl;
cout << "try another question" << endl;
}
}
while(userInput == 1);
}
}
while(isGameOver == false);
}
// add stuct or class to handle questions,
if (userInput != 1||2) doesn't do what you think. With the proper paretheses inserted, it is
if ((userInput != 1) || 2)
and 2 is nonzero, hence the condition is always true.
You want
if (userInput != 1 && userInput != 2)
The problem lies here:
if (UserInput!=1||2)
In this line, there are two conditions:
UserInput!=1 , 2
Here , whether user input is 1/2, the second condition 2 is always evaluated as true, which runs the if block
So change it to
if (UserInput!=1 && UserInput!=2)