It's my first time posting here, so I apologize if the post is not formatted correctly. I'm new to C++ and was looking for some help.
I can't seem to figure out what is stopping my code after roughly the 18th line. It processes the first cout and cin, but dosen't continue with the next cout?
If you enter a key, it will run through all of the conditions listed in the if/then statement and finish. However, my goal for what is here, is to have the computer generate a random number, followed by asking me for input (Y/N). Given the input, it's either supposed to generate another random number or end.
Using the compiler generate no errors, so I'm a bit dumbfounded right now as to what the issue is.
I'd appreciate any help, thank you.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
int comp;
using namespace std;
int main ()
{
comp= 1+(rand() % 10);
randomnumber:
string again;
cout <<"The computer chose this random number: "<< comp << endl;
cin >> comp;
cout << "Would you like to run this again? Y/N" << endl;
cin >> again;
if((again == "y")||(again == "Y"))
{
goto randomnumber;
}
else if((again == "n")||(again == "N"))
{
cout << "OK" << endl;
}
else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
{
cout << "Please type Y or N" << endl;
}
return 0;
}
First lets take a look at what the code should look for your requirements to be fulfilled.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
//int comp; No need for comp to be a global variable here.
using namespace std;
int main ()
{
int comp;
randomnumber://if you were to use goto, the label should have been here.
comp= 1+(rand() % 10);
//randomnumber: avoid goto statements at all if possible.
//string again; No need for it to be a string.
char again;
cout <<"The computer chose this random number: "<< comp << endl;
//cin >> comp; since you want computer to generate a random no, why ask the user for it?
cout << "Would you like to run this again? Y/N" << endl;
cin >> again;
if((again == "y")||(again == "Y"))
{
goto randomnumber;
}
else if((again == "n")||(again == "N"))
{
cout << "OK" << endl;
}
else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
{
cout << "Please type Y or N" << endl;
}
return 0;
}
Now lets take a look at how to do this in a more non-complex, simple way.
#include<iostream.h> //#include<iostream> if No such file error.
#include<time.h> // for seeding rand()
#include<stdlib.h> // for rand
using namespace std;
int main()
{
srand(time(NULL)); //seeding rand()
while(true)
{
int comp=1+(rand() % 10);
cout <<"The computer chose this random number: "<< comp << endl;
cout<<"Would you like to run this again?Y/N"<< endl;
char choice;
cin>>choice;
if ((choice == 'N')|| (choice =='n'))
{
cout<<"OK"<< endl;
break;
}
else
while(choice!='y' && choice!='Y') // forcing user to enter a valid input.
{
cout<<"Please type Y or N" << endl;
cin>>choice;
}
}
return 0;
}
I hope this helps.
Related
I've created a program that asks for shoe size. If the shoe size is correct, the user will get a message that says, "You are correct would you like to try again?" They can either answer yes or no. If they select "no" the program will break and they will get another message that says, "Thanks for playing!"
Is there a way to basically force the program to break back into the while loop if the user enters yes as an answer instead of no?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int shoesize = 13;
int input;
string answer;
while (input != shoesize) {
cout << "Guess my shoe size!";
cin >> input;
if (input == shoesize) {
cout << "You are correct! Thanks for Playing! Would you like to play again? \n";
cin >> answer;
if (answer == "no") {
cout << "Thank you for playing!";
}
}
}
return 0;
}
Easily the best way to write something that "repeats continuously until you want to quit" is to just do something like this:
while(true) {
.. do something ..
.. ask "do you want to quit" ..
if (answer == "yes") {
break;
}
}
Programmers will instantly recognize the while(true){} "endless" loop, and look for the break statement which – as the name implies, "breaks out of" the innermost loop, ending the "endless" loop. One nice thing about this design is that you can use the break statement in more than one place within the loop as needed. (Within a function, you can also return.)
How about this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int shoesize = 13;
string answer;
int input = 0;
while (input != shoesize && answer != "no") {
cout << "Guess my shoe size!";
cin >> input;
if (input == shoesize) {
cout << "You are correct! Thanks for Playing! Would you like to play again? \n";
cin >> answer;
if (answer == "no") {
cout << "Thank you for playing!";
return 0;
}
}
}
}
I want the user to choose between playing the game again or ending the program, however when prompted, if they press 'y' the same thing gets repeated over and over instead of the whole program from the very beginning. I've tried while loops, do/while loops, if statements, rearranging the code, but nothing has worked. Any advice?
#include <iostream>
#include <string>
using namespace std;
int main(){
string animal = "fish";
string guess;
char choose = 'Y' ;
int count = 0;//keeps a running total of how many times the user
has guessed an answer.
int limit = 5;//allows user to guess only 5 times, otherwise
they loose the game.
bool out_of_guesses = false;//to check whether the user has run
out of guesses.
cout << "I am thinking of an animal.\n" << endl;
do{
while(animal != guess && !out_of_guesses){//Nested while
loop inside main loop to keep track of how many tries the user has
attempted and to validate their answers.
if(count < limit){
cout << "Can you guess what animal I am thinking of?: ";
getline(cin, guess);
count++;
if(animal != guess){
cout << "\nHmm, nope. That's not the animal I'm
thinking of." << endl;
if(count > 2 && count <5){
cout << "I'll give you a hint. It lives in
water." << endl;
}
}
}
else{
out_of_guesses = true;
}
}//End nested while loop
if(out_of_guesses){
cout << "\nI'm sorry, but you are out of guesses." <<
endl;
}
else{
cout << "\n*** Good job! You guessed the correct animal!
***" << endl;
cout << "\t\t><)))º> ❤ <º)))><\t\t" << endl;
}
//The do-while loop is there to ask the user if they wish to
play the game again.
cout << "Would you like to try again?(y/n): ";
cin >> choose;
if(choose == 'N' || choose == 'n')
break;
}while(choose == 'Y' || choose == 'y');
return 0;
}
The bool out_of_guesses = false; must be in-between while(true) and while(animal != guess && !out_of_guesses), and not outside the first while loop. Because our while loop condition is always false, and then it does enter it.
You should also reset your guess variable in-between those 2 loops, else same thing could happen (false while loop) in case of the answer is found.
Here the code with some refactoring/review, which I used the guess as upper case to handle any typography of the answer. I also removed the out of guess variable to use the count and limit one instead.
#include <iostream>
#include <string>
#include <cctype>
int main()
{
const std::string animal = "FISH";
const int limit = 5;
do
{
std::cout << "I am thinking of an animal.\n";
int count = 0;
std::string guess;
while(animal.compare(std::toupper(guess)) != 0 && count < limit)
{
std::cout << "Can you guess what animal I am thinking of?: \n";
std::cin >> guess;
count++;
if(animal.compare(std::toupper(guess)) != 0)
{
std::cout << "\nHmm, nope. That's not the animal I'm thinking of.\n";
if(count > 2)
{
std::cout << "I'll give you a hint. It lives in water.\n";
}
}
}
}//End nested while loop
if(count >= limit)
{
std::cout << "\nI'm sorry, but you are out of guesses.\n";
}
else
{
std::cout << "\n*** Good job! You guessed the correct animal! ***\n";
std::cout << "\t\t><)))º> ❤ <º)))><\t\t\n";
}
char choose = 'Y' ;
std::cout << "Would you like to try again?(y/n): ";
std::cin >> choose;
if(std::toupper(choose) == 'N') break;
} while(true);
return 0;
}
I'm new to stackoverflow, and also somewhat new to programming, so please don't mind my poor formatting of the code. I have two problems with my code.
My continue statement, which I'm using to continue the loop if the player types 'y' or 'Y', doesn't work. It terminates the program after only getting the guess correctly, which leads me to:
2.My continue counter goes past 0 without stopping, and I just can't see my error in the logic of the program.
I can't see the problems with my logic.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <random>
using namespace std;
int getNumber(); //random number prototype
double getScore(); //gets score
int chances = 7; //chances to guess with
int main()
{
int guess = 0,
random;
char retry = 'y'; //initialize retry to 'y'
cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
<< "You have 7 chances. Good luck! \n \n" << endl;
random = getNumber(); //give the function a variable
do
{
cout << random << "\n" << "\n";
chances--;
cout << "Enter your guess: ";
cin >> guess;
if (guess == random)
{
cout << "You have won the game! " << "Your score was: " << getScore();
cout << "Would you like to retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue; //player can retry the game
}
else if (chances == 0)
{
cout << "You have no chances left. Retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue;
}
}
return 0;
}
else if (guess != random)
cout << "You got it wrong. \n" << "You have: " << chances << " chances left" << endl << endl;
else
cout << "Incorrect Input. Please type a number." << endl << endl;
} while (guess != random);
return 0;
}
int getNumber()
{
unsigned seed = time(0); //seed the random number
srand(seed);
int randNum = rand() % 10 + 1; //random number in the range of 1-10
return randNum;
}
if (retry == 'y' || 'Y')
This is incorrect logic, which is why your code does not work the way you want it to. You want it to be:
if (retry == 'y' || retry == 'Y')
Fix this logic error in your other if-else statements as well.
You'll wanna take a look at this
Your continue statement is jumping to the end and checking the condition, guess != random, which evaluates to false and exits the do while. What you need to do is reset guess to a value such as 0 so that the condition does evaluate to true.
Why wont the code ask the user again when I try running it if the answer is not y or n?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char answer;
cout << "Do you like cheese?" << endl;
cin >> answer;
if ( answer != 'y' && answer != 'n' ) {
cout << endl << "Wrong answer, Try again: ";
cin >> answer;
}
return 0;
}
I understand it's not a loop but my other code seems to work fine. If the scored is less than 0, it will keep asking for a number. Why wont this code work then?
cout << "Enter the number of goals that were scored: ";
cin >> scored;
if ( scored < 0 ) {
cout << "Error: the number of goals must be greater than 0. Try again: ";
cin >> scored;
}
Change if in your if statement and replace it with while.
while ( answer != 'y' && answer != 'n' ) {
cout << endl << "Wrong answer, Try again: ";
cin >> answer;
}
Then put the rest of your code below:
//Do what you want with 'answer' here...
#include <iostream>
using namespace std;
int main(){
char answer;
cout<<"do you like cheese?"<<endl;
cin>>answer;
while((answer!='y'&&answer!='n')){
cout<<"do you like cheese?"<<endl;
cin>>answer;
}
return 0;
}
I've been trying to make a simple game where the computer generates a random number and you try to guess it. It also stores the amount of guesses you make "tries".
However, when I run the program, it simply prints: "Let's play a game. I'll think of a number 1-100. Try to guess it."
Here's my code:
#include <iostream>
int main()
{
using namespace std;
int the_number;
int guess;
int tries;
the_number = rand() % 101 + 1;
cout << "Let's play a game!";
cout << "I will think of a number 1-100. Try to guess it.";
cout << endl;
cin >> guess;
for (tries = 0; tries++;)
{
if (guess == the_number)
{
cout << "You guessed it!";
cout << "And it only took you: " << tries;
}
else if (guess < the_number)
{
cout << "Higher";
tries++;
}
else if (guess > the_number)
{
cout << "Lower";
tries++;
}
else
cout << "That's not even in range!";
return 0;
}
}
I don't understand why this doesn't work, could someone explain why not?
The reason your program does not print anything after "Let's play a game. I'll think of a number 1-100. Try to guess it." is the way you have written your for loop.
for ( tries = 0; tries++; )
breaks out of the loop without doing anything because tries++ evaluates to 0.
Also, for your program to work correctly, you need to add more code to read guesses. Something like the code below, should work.
for (tries = 0; ; tries++)
{
if (guess == the_number)
{
cout << "You guessed it!";
cout << "And it only took you " << tries << " tries.\n";
break;
}
else if (guess < the_number)
{
cout << "Higher";
cin >> guess;
}
else if (guess > the_number)
{
cout << "Lower";
cin >> guess;
}
}
You can define a couple of variables that will make your code more understandable, something like this :
#include <iostream>
using namespace std;
int main()
{char EndGame = 'N';
int MyNumber = 150 , playerguess;
cout << "I have a number between 1 and 100.\nCan you guess my number ??\nPlease type your first guess.\n?" << endl;
do{
cin >> playerguess;
if (playerguess > MyNumber) {
cout << " Too High. Try again." << endl;
}
else if (playerguess == MyNumber) {
cout << "Excellent ! You Got It ! \n If you want to exit press Y" << endl;
cin >> EndGame;
break;
}
else {
cout << " Too Low. Try again." << endl;
}
} while (1);
return 0;
}
This will make the number equal to 150. Each time the user inputs a value, the console will determine whether it is higher, lower or equal to the number.
If you want instead to make it a random number each time, you can simply use the <random> library and use the module operator with a number like 100 or 101. Then, you can add 1; this will generate only positive integers.
You should use while loop here, not for:
while (the_number != guess)
{
//
//
}
And try using the new <random> header instead of rand() function:
#include <random>
std::random_device rd;
std::default_random_engine engine(rd());
std::uniform_int_distribution<int> uniform_dist(1, 100);
the_number = uniform_dist(engine);
Your for loop is wrong (it needs 3 things: initialization, check condition and the todo step after each loop.
For example:
for (tries = 0; tries < 5; tries++)
Also you loop the guessing part, but you forget to ask the user for a new number. I would suggest to move the cin << guess into the for loop.