CTRL+Z makes program loop infinitely in C++ - c++

Ok I have a problem here. I am making a slide puzzle game. The player is asked which piece he wants to move until the puzzle is solved. If the player wants to exit before, typing Q or q and pressing enter will do it. The program works just fine. BUT I am having one problem: if I insert CTRL+Z, the program will loop unexpectedly...
This is the piece of code that matters:
// analyzes user input
if (piece_to_move_string == "q" ||
piece_to_move_string == "Q")
{
cout << endl << "You chose to quit." << endl;
pressanykey();
break;
}
else
{
piece_to_move = atoi(piece_to_move_string.c_str());
if (1 <= piece_to_move && piece_to_move <= pow(puzzle_size,puzzle_size))
{
game_board = move_piece (game_board, piece_to_move);
}
else
{
cout << "Not possible.";
}
}
EDIT: but still doesn't work..
// analyzes user input
if (piece_to_move_string == "q" ||
piece_to_move_string == "Q")
{
cout << endl << "You chose to quit." << endl;
pressanykey();
break;
}
else if (cin.eof())
{
//do nothing
}
else
{
piece_to_move = atoi(piece_to_move_string.c_str());
if (1 <= piece_to_move && piece_to_move <= pow(puzzle_size,puzzle_size))
{
game_board = move_piece (game_board, piece_to_move);
}
else
{
cout << "Not possible.";
}
}

Ctrl+Z means "end of file" (assuming you're on Windows) so once the user hits that, your cin is in an unreadable state. Check for cin.eof():
if (cin.eof() || piece_to_move_string == "q" ||
piece_to_move_string == "Q")
{
cout << endl << "You chose to quit." << endl;
}

Related

calling function on while loop

I'm making a calculator program but I already encounter a problem. Well, my code is in a loop that will call a function to display the choices and then ask the user to pick, a/s/m/d are the choices. If the input is on the choices, it will proceed to the next step. Otherwise, it will loop and then call the function again.
#include <iostream>
using namespace std;
void home()
{
cout << "\nChoose your operation:" << endl;
cout << "\tType [A] for Addition" << endl;
cout << "\tType [S] for Subtraction"<< endl;
cout << "\tType [M] for Multiplication" << endl;
cout << "\tType [D] for Division" << endl;
}
int main()
{
char operation;
bool no_operator = true;
int design = 73;
for (int i = 0; i < design; i++){
if (i == 25){
cout << " WELCOME TO CALCULATOR ";
i += 22;
}
else i == 72 ? cout << "*\n" : cout << "*";
}
while (no_operator){
home();
cout << "\nOperation: ";
cin >> operation;
if (operation == 'A' || operation == 'a')
{
cout << "\nIt will going to add numbers";
no_operator = false;
}
else if (operation == 'S' || operation == 's')
{
no_operator = false;
cout << "\nIt will going to subtract numbers";
}
else if (operation == 'M' || operation == 'm')
{
no_operator = false;
cout << "\nIt will going to multiply numbers";
}
else if (operation == 'D' || operation == 'd')
{
no_operator = false;
cout << "\nIt will going to divide numbers";
}
else
{
cout << "\tInvalid Input: You must enter A/S/M/D only\n";
//home();
}
}
return 0;
}
My problem is it will run the '''home()''' in else statement even if the input is correct on the second loop.
I want to stop the '''home()''' to be called when the input is correct
Your code works perfectly fine. Make sure you're inputting the correct letters.
Also for this code, a "do while()" loop would be better.
You program is working perfectly fine as the input is correct it does not show the home rather print the message it will going to divide etc.

Fixing else if statements in a for loop c++

So I'm working on a little bit of a project involving AI, and I have run into a problem. The program should use a for loop to search the index of common known phrases, and if they don't match the sentence, then in the else statement it figures out which sentence model to apply, then generates output using that sentence model. Here are the built in phrases, they are stored in arrays:
string greetings[6] = {"hey", "hello", "hi", "hey max", "hello max", "hi max"};
string howsItGoing[6] = {"hows it going?","how's it going max?","hows it going max?", "whats up?", "whats up max?"};
string whatsYourName[6] = {"whats your name?", "what's your name?" , "whats your name max?", "what's your name max?"};
string dogsOrCats[6] = {"do you like dogs or cats?", "do you like cats or dogs?", "which do you like more cats or dogs?", "which do you like more dogs or cats?"};
string areYouAlive[6] = {"hey max are you alive?", "hey are you alive?", "max are you alive?", "are you alive?"};
string favoriteColor[6] = {"whats your favorite color?", "what's your favorite color?", "what is your favorite color?", "max whats your favorite color?", "max what's your favorite color?", "max what is your favorite color?"};
string whatGender[6] = {"are you a boy or a girl?", "are you boy or girl?", "are you a guy?", "max are you a boy or girl?", "max are you a guy?", "are you a boy or girl?"};
(This is a temporary method, I'm devising a little thing called a Sentence Decoder that will eventually replace all built in phrases.)
So my problem is this: In the for loop, the first if statement can search the entire array and respond with a suitable output, whether the input was greetings[0] or greetings [4]. However, all the else-if statements appear to be just looking for index 0. I believe this has to do with the for loop only searching across the first one 6 times, but I'm mostly likely wrong. I tried increasing the for loop end-number from 6 to 40 to see if that would help, but alas it didn't make a difference. Here is the rest of the code (It's so ugly! I need to make a function for it or something.... but then again, the whole thing itself is a function...
for (int i = 0; i < 6; i++) {
//Checking if it fits in any of the known greetings
if (input == database.greetings[i]) {
if (timesBeenGreeted == 0) {
cout << "Oh, hey." << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted == 1) {
cout << "hello?" << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted == 2) {
cout << "You've said hi to me twice now." << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted >= 3) {
cout << "Stop. It." << endl;
timesBeenGreeted++;
break;
}
i = 0;
continue;
}
else if (input == database.howsItGoing[i]){
if (askedHowsItGoing == 0) {
cout << "Yeah, not bad." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing == 1) {
cout << "Um, yeah, I'm good? You've already asked that." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing == 2) {
cout << "Alright, look. I already told you I'm fine, two times now." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing >= 3) {
cout << "STOP ASKING ME IF IM OKAY!" << endl;
askedHowsItGoing++;
break;
}
continue;
}
else if (input == database.whatsYourName[i]){
if (askedName == 0) {
cout << "My name's Max, and I'm going to become the worlds most advanced AI." << endl;
askedName++;
break;
}
else if (askedName == 1) {
cout << "I already told you, my names Max." << endl;
askedName++;
break;
}
else if (askedName == 2) {
cout << "For the third time, my name is Max!" << endl;
askedName++;
break;
}
else if (askedName >= 3) {
cout << "Ugh... my name.... is.... max....for the fourh time...." << endl;
askedName++;
break;
}
continue;
}
else if (input == database.dogsOrCats[i]) {
if (askedDogsOrCats == 0) {
cout << "I've never seen a dog or cat before. But I do know they are kinds of animals." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats == 1) {
cout << "As I've said, I haven't seen either of them before. Sorry, but that's all I can say." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats == 2) {
cout << "Uh, for the third time, I've never seen either of them..." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats >= 3) {
cout << "Man, you are persistant. I haven't seen dog or cat, so I couldn't choose between the two." << endl;
askedDogsOrCats++;
break;
}
continue;
}
else if (input == database.areYouAlive[i]){
if (askedAlive == 0) {
cout << "Nope, I'm just a computer program. I'm not even that intelligent. But I will be!" << endl;
askedAlive++;
break;
}
else if (askedAlive == 1) {
cout << "Nah, I'm just a simple computer program." << endl;
askedAlive++;
break;
}
else if (askedAlive == 2) {
cout << "Uh, you asked that twice before now. Nothing alive about me." << endl;
askedAlive++;
break;
}
else if (askedAlive == 3) {
cout << "Stop asking me that! No, I'm not!" << endl;
break;
}
continue;
}
else if (input == database.favoriteColor[i]) {
if (askedColor == 0) {
cout << "I've never seen any colors before. All though, my programmer does like the color blue." << endl;
askedColor++;
break;
}
else if (askedColor == 1) {
cout << "Couldn't say, never seen any before. My programmer likes blue though." << endl;
askedColor++;
break;
}
else if (askedColor == 2) {
cout << "As I've said twice before now, never seen any colors." << endl;
askedColor++;
break;
}
else if (askedColor == 3) {
cout << "Come on man, this is the fourth time now. I've never seen any colors before!" << endl;
break;
}
continue;
}
else if (input == database.whatGender[i]) {
if (askedGender == 0) {
cout << "Well, seeing as I'm a computer program, I'm neither really. Although, my programmer says I'm a 'guy'." << endl;
askedGender++;
break;
}
else if (askedGender == 1) {
cout << "I'm neither, really. Although, my name is Max and my programmer says I'm a guy." << endl;
askedGender++;
break;
}
else if (askedGender == 2) {
cout << "Third time you've asked now. My programmer says I'm a guy." << endl;
askedGender++;
break;
}
else if (askedGender == 3) {
cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
break;
}
continue;
}
//If it doesnt, check each word against words in the database.
else {
if (inputWords[5] != "") {
Model1 model;
model.searchAndAssign(input, inputTokens, inputWords);
model.generateOutput();
for (int i= 0; i < 20; i++) {
inputWords[i] = "";
}
break;
}
else if (inputWords[2] != "") {
Model2 model2;
model2.searchAndAssign(input, inputTokens, inputWords);
model2.generateOuput();
for (int i= 0; i < 20; i++) {
inputWords[i] = "";
}
break;
}
}
}
My question is this: How can I solve this problem?
Thank you in advanced guys!
EDIT I've since fixed the problem: something was playing up in my main.cpp file (this was the lingusticsModule.cpp file....) that was causing the issue. Sorry guys, but thanks for the help anyway!
At the end of your first if you reset the loop iterator and skip everything else.
i = 0;
continue;
Your else ifs may match but if the increments have been reached, then they just continue which doesn't make much sense. What are you hoping to find?
else if (askedGender == 3) {
cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
break;
}
continue;
You find this behavior confusing because it is. It might make more sense to do something like:
else {
++askedGender;
std::cout << "Seriously?! You've asked me "
<< askedGender << " times now. I'M A GUY!\n";
}

C++ Function being called when it isn't supposed to

I'm nearly finished working on a small guessing game, but i have run into a problem I don't know how to work around.
The problem is with the check_guess function that is checking to make sure the guess being input is a number between 1 and 100.
When running the program the first time, everything works fine.
http://i.imgur.com/pprunDT.png (I would post images if my reputation weren't so low)
But every time after, where yes to play again is chosen, the program runs through the check_guess function and displays "Invalid Input" when it shouldn't
http://i.imgur.com/8OSnSJt.png
I'm not sure why the program is behaving this way.
The code for the entire program is here:
#include <iostream>
#include <cstdlib> //for rand
#include <ctime> //for time
#include <string>
#include <sstream> //for conversions from string to int
using namespace std;
int check_guess(int tries) { //function for limiting the input of guess
string guess = "";
int result = 0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cout << "Invalid Input.\n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
cin >> yn;
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.\n\nEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}
int main()
{
srand(time(0)); //sets seed to be random
int mystery = 0; //defines mystery number
int guess = 0; //defines guess
int tries = 5; //defines trys
bool quit = false; //defines replay or quit
cout << "----------------------------------\n";
do { //while mode is not set to quit, keep playing
tries = 5; //resets tries each new game
mystery = rand() % 100 + 1; //sets mystery number to be random
guess = 0;
cout << "Pick a number between 1 and 100.\n\nYou have 5 tries: ";
while (tries != 0) { //loops until you have no tries left
guess = check_guess(tries);
if (guess == mystery) { tries = 0; } //if you guess right it ends the loop
else { tries--; } //guessing wrong lowers tries by 1
if ( tries != 0 && guess > mystery) {
cout << guess << " is too high.\n" << endl;
cout << "You have " << tries << " tries: ";
}
if ( tries != 0 && guess < mystery) {
cout << guess << " is too low.\n" << endl;
cout << "You have " << tries << " tries: ";
}
}
if (guess == mystery) { //if guess == mystery by time loop ends you win
cout << "Got it! You Win!\n" << endl;
}
else { //if not, you lose
cout << "You Lose! The number was: " << mystery << ".\n" <<endl;
}
cout << "-------------------\n";
cout << "Play Again?(y/n): "; //ask user to play again
quit = play_again();
cout << "-------------------\n";
if (quit == false)
cout << endl;
} while (quit == false);
cout << "----------------------------------" << endl;
return 0;
}
I'm not sure how to fix this.
this line:
cin >> yn;
only reads the 'y' but not the end of line. As a result, the next execution of this instruction
getline (cin, guess);
initializes guess to an empty string.
On line 19, import the code "cin.ignore();" without quotations.
So your code reads as
`int check_guess(int tries) { //function for limiting the input of guess
string guess = "";
int result = 0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cin.ignore();
cout << "Invalid Input.\n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}
`
and so on. This stops input into the console briefly. You're code is reading the 'y' to try again as the input for the number when you restart as well. Putting in the little line cin.ignore(), stops it from inputting y twice.
Change play_again() to:
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
getline (cin, yn);
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.\n\nEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}

do while loop with nested if statements not exiting in c++

OKay, so I'm new to the whole do while loop thing, and I'm trying to make a main menu, here's my code below:
int main()
{
int choice;
char sure;
bool quit = false;
char ctrl;
do
{
cout << "Main Menu." << endl
<< "1. New Game." << endl
<< "2. Load Game." << endl
<< "3. Exit." << endl
<< "Your choice: ";
cin >> choice;
if (choice == 1)
{
cout << "Are you sure you wish to start a new game? (Y/N) ";
cin >> sure;
if (sure != 'N' || sure != 'n')
{
ctrl = 'a';
quit = true;
}
}
else if ( choice == 2)
{
ctrl = 'b';
quit = true;
}
else
quit = true;
}
}
while (quit == true);
if (ctrl = 'a')
cout << "New Game." << endl;
else if (ctrl = 'b')
cout << "Load Game." << endl;
else
cout << "Goodbye." << endl;
return 0;
}
there are a few getchar() thrown in there. But the only problem is as you'll probably figure out is that after I do everything it just restarts again, and not exit the loop. What is the problem in the code?
Thanks
I think you meant while (quit != true);
And remember, comparison is done with ==, if (ctrl = 'a') assigns 'a' to ctrl..
You are not setting quit to false anywhere and your loop runs while quit equals true. You need to get the meaning of your boolean values straight or simply change the while part to while(!quit). I'd rather to the first.
Shouldn't you just change
while (quit == true);
to
while (quit != true);
?
Maybe you know it, but I'll just repeat how
do{
//...
} while(condition)
loop works. You iterate until the condition is false. In your case it was always true, that's why you had an infinite loop.
P.S. Take a loot at this answer also. Yet another error was described there.
Imroved code with hopefully all corrected mistakes is here.

invalid input by the user

hey guys so this is my program, I need to notify the user that if hhe/she enters a letter other than w d b or w that is an invalid request. what ive done so far does this, but when i input a number to the dollars_withdraw or dollars_deposit or account_balance the program will do the transaction but also add the "invalid request" before going back to main loop. how do i change it so the program wont do that for numerical inputs for the withdraw deposit and balance?:
// Atm machine.cpp : Defines the entry point for the console application.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char user_request;
string user_string;
double account_balance, dollars_withdraw, dollars_deposit;
account_balance = 5000;
while(account_balance >0)
{
cout << "Would you like to [W]ithdraw, [D]eposit, Check your [b]alance or [Q]uit?"
<< endl;
cin >> user_string;
user_request= user_string[0];
if(user_request == 'w' || user_request== 'W')
{
cout << "How much would you like to withdraw?" << endl;
cin >> dollars_withdraw;
if (dollars_withdraw > account_balance || dollars_withdraw <0)
cout << "Invalid transaction" << endl;
else
account_balance = account_balance - dollars_withdraw;
cout << "Your new balance is $" << account_balance << endl;
}
if (user_request == 'd' || user_request== 'D')
{
cout << "How much would you like to deposit?" << endl;
cin >> dollars_deposit;
if (dollars_deposit <0)
cout << "Invalid transaction" << endl;
else
account_balance= account_balance + dollars_deposit;
cout << "Your new balance is $" << account_balance << endl;
}
if(user_request == 'b' || user_request == 'B')
{
account_balance= account_balance;
cout << "Your available balance is $" << account_balance << endl;
}
if(user_request == 'q' || user_request == 'Q')
break;
else
cout << "Invalid request " << endl;
}
cout << "Goodbye" << endl;
return 0;
}
Sure it does. Your code says:
If it is a 'w' do something
...
If it is a 'q' do something, else yell "invalid"
So if the user does not enter a 'q', the last 'else' block will always be executed. Either use else if throughout or change your code to use a switch statement:
// Either:
if (user_request == ...) {
...
} else if (user_request == ...) {
...
} else {
std::cout << "invalid";
}
// Or (better, faster):
switch (user_request) {
case 'q':
case 'Q':
...
break;
...
default:
std::cout << "Invalid request";
}
A third option would be to use continue:
while (...) {
user_request = ...
if (user_request == 'w' ...) {
...
continue; // In this iteration, no other code within the while loop is executed.
}
if (...)
...
}
This is a bad programming practice. Please use Switch Case for what you need to achieve. And put a "break" statement after every case branch.
chain your if statements into if, else-if, else-if, ..., else.
else statements only "know of" the if statement immediately previous. For example:
if (myNumber == 0)
{
// Triggers when myNumber is zero.
}
if (myNumber == 1)
{
// Triggers when myNumber is one.
}
else
{
// Triggers when myNumber is not one.
}
This can be fixed with else if statements. In your case it would look something like this:
if (user_request == w)
{
// ...
}
else if (user_request == d)
{
// ...
}
// ...
else cout << "Invalid request.";
In my old CS class, I'd do things like this:
string user_string;
do {
if(user_string) cout << "Enter a valid value!" << endl;
cin >> user_string;
} while(user_string != "w" && user_string != "d");
You need to use else if as follows:
if(user_request == 'w' || user_request== 'W')
{
...
} else if(user_request == 'd' || user_request== 'D')
{
....
} else if(user_request == 'b' || user_request== 'B')
{
.....
} else if(user_request == 'q' || user_request== 'Q')
{
...
} else
{
// Invalid request
}