How to translate cout/cins to printf/scanfs? - c++

I coded an easy game. I know how to use cout/cin but having problems with printf/scanf. The code below works well with cout/cin. Question is, how to convert them into printf/scanf? Why the codes in comment don't work?
Edit: I mean if I delete cout/cin lines and when I use printf/scanf instead it doesn't work properly.
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
int min=0, max=1000, guess, counter=0;
bool winner=false;
char answer;
while(counter<10){
guess = rand()%(max-min)+min;
// printf("Did you pick %d? \t", guess);
// scanf("%c", &answer);
cout << "Did you pick " << guess << "?\t";
cin >> answer;
if(answer=='y'){ // yes
winner=true;
// printf("Computer wins.\n");
// printf("You picked: %d", guess);
cout << "Computer wins." << endl;
cout << "You picked: " << guess;
break;
}
else if(answer=='m'){ // more
min=guess;
counter++;
}
else if(answer=='l'){ // less
max=guess;
counter++;
}
}
if(winner==false){
// printf("User wins.\n");
cout << "User wins." << endl;
}
return 0;
}

The problem is that scanf() doesn't remove the newline '\n' character from stdin, so on the next iteration the next scanf() reads it and tries to process it, seeming to ignore the input.
Try this instead:
scanf("\n%c", &answer);
That way, you expect the newline and scanf() consumes it.

Related

C++ program stuck in an infinite loop

Please note that I am a complete beginner at C++. I'm trying to write a simple program for an ATM and I have to account for all errors. User may use only integers for input so I need to check if input value is indeed an integer, and my program (this one is shortened) works for the most part.
The problem arises when I try to input a string value instead of an integer while choosing an operation. It works with invalid value integers, but with strings it creates an infinite loop until it eventually stops (unless I add system("cls"), then it doesn't even stop), when it should output the same result as it does for invalid integers:
Invalid choice of operation.
Please select an operation:
1 - Balance inquiry
7 - Return card
Enter your choice and press return:
Here is my code:
#include <iostream>
#include <string>
using namespace std;
bool isNumber(string s) //function to determine if input value is int
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
else if (rtrn == "2" and isNumber(rtrn)) { return true; }
else {cout << "Invalid choice." << endl; ReturnCard(); };
return 0;
}
int menu() //function for operation choice and execution
{
int choice;
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == 1 and isNumber(to_string(choice))) { cout << "Your balance is $" << balance; "\n\n"; }
else if (choice == 7 and isNumber(to_string(choice))) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; menu(); }
} while (ReturnCard()==false);
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}
I've tried every possible solution I know, but nothing seems to work.
***There is a different bug, which is that when I get to the "Do you wish to continue?" part and input any invalid value and follow it up with 2 (which is supposed to end the program) after it asks again, it outputs the result for 1 (continue running - menu etc.). I have already emailed my teacher about this and this is not my main question, but I would appreciate any help.
Thank you!
There are a few things mixed up in your code. Always try to compile your code with maximum warnings turned on, e.g., for GCC add at least the -Wall flag.
Then your compiler would warn you of some of the mistakes you made.
First, it seems like you are confusing string choice and int choice. Two different variables in different scopes. The string one is unused and completely redundant. You can delete it and nothing will change.
In menu, you say cin >> choice;, where choice is of type int. The stream operator >> works like this: It will try to read as many characters as it can, such that the characters match the requested type. So this will only read ints.
Then you convert your valid int into a string and call isNumber() - which will alway return true.
So if you wish to read any line of text and handle it, you can use getline():
string inp;
std::getline(std::cin, inp);
if (!isNumber(inp)) {
std::cout << "ERROR\n";
return 1;
}
int choice = std::stoi(inp); // May throw an exception if invalid range
See stoi
Your isNumber() implementation could look like this:
#include <algorithm>
bool is_number(const string &inp) {
return std::all_of(inp.cbegin(), inp.cend(),
[](unsigned char c){ return std::isdigit(c); });
}
If you are into that functional style, like I am ;)
EDIT:
Btw., another bug which the compiler warns about: cout << "Your balance is $" << balance; "\n\n"; - the newlines are separated by ;, so it's a new statement and this does nothing. You probably wanted the << operator instead.
Recursive call bug:
In { cout << "Invalid choice of operation."; menu(); } and same for ReturnCard(), the function calls itself (recursion).
This is not at all what you want! This will start the function over, but once that call has ended, you continue where that call happened.
What you want in menu() is to start the loop over. You can do that with the continue keyword.
You want the same for ReturnCard(). But you need a loop there.
And now, that I read that code, you don't even need to convert the input to an integer. All you do is compare it. So you can simply do:
string inp;
std::getline(std::cin, inp);
if (inp == "1" || inp == "2") {
// good
} else {
// Invalid
}
Unless that is part of your task.
It is always good to save console input in a string variable instead of another
type, e.g. int or double. This avoids trouble with input errors, e.g. if
characters instead of numbers are given by the program user. Afterwards the
string variable could by analyzed for further actions.
Therefore I changed the type of choice from int to string and adopted the
downstream code to it.
Please try the following program and consider my adaptations which are
written as comments starting with tag //CKE:. Thanks.
#include <iostream>
#include <string>
using namespace std;
bool isNumber(const string& s) //function to determine if input value is int
{
for (size_t i = 0; i < s.length(); i++) //CKE: keep same variable type, e.g. unsigned
if (isdigit(s[i]) == false)
return false;
return true;
}
bool ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
if (rtrn == "2" and isNumber(rtrn)) { return true; } //CKE: remove redundant else
cout << "Invalid choice." << endl; ReturnCard(); //CKE: remove redundant else + semicolon
return false;
}
int menu() //function for operation choice and execution
{
string choice; //CKE: change variable type here from int to string
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == "1" and isNumber(choice)) { cout << "Your balance is $" << balance << "\n\n"; } //CKE: semicolon replaced by output stream operator
else if (choice == "7" and isNumber(choice)) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; } //CKE: remove recursion here as it isn't required
} while (!ReturnCard()); //CKE: negate result of ReturnCard function
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}

cin.get() is taking input from previous cin.get()

I'm super new to this, and am trying to code a simple shop interface for a CLI adventure game.
The problem I'm having is in between uses of cin.get(). I've read another post but don't understand what's being said. Can someone explain like I'm 5?
I use cin.get() once in the MainMenu() to wait for Enter to continue. This punishes the player's health if they do anything other than press Enter.
I then move forwards to the Introduction(), where I'm trying to pull the same trick, but it carries the input from cin.get() from the MainMenu() function.
The other code in main() is just keeping track of the character's health and stopping the program if it reaches 0 by way of another function.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int MainMenu()
{
cout << "\nPress the ENTER key\n";
char Input = cin.get();
if (Input == '\n')
{
return 0;
}
else if (Input != '\n')
{
cout << "I meant ONLY the ENTER key... Oh well its your health pool.\n";
CharHealth(-2);
cout << "You took 2 damage. \nYou now have " << CharHealth(0) << "Health.\n";
return 0;
}
}
int Introduction()
{
cout << "you awake in a puddle, walk to town. \nPress [ENTER]";
char Input = cin.get();
if (Input != '\n')
{
cout << "\nfor real?\nTake Another 2 Damage\nwhat did you think?... Idiot" ;
CharHealth(-2);
return 0;
}
else
{
return 0;
}
}
int main()
{
MainMenu();
Introduction();
while (CharHealth(0) > 0)
{
cout << "winner";
return 0;
}
cout << "\n You died. idiot.";
return 0;
}
Don't judge my story telling, everything is still placeholders right now.
My console just reads:
Press the ENTER key
asdf
I meant ONLY the ENTER key... Oh well its your health pool.
You took 2 damage.
You now have 1Health.
you awake in a puddle, walk to town.
Press [ENTER]
for real?
Take Another 2 Damage
what did you think?... Idiot
You died. idiot.
C:\Users\KR's\Documents\text Shop>
cin.get() reads 1 char at a time. So, for example, if the user types in asdf, then the next cin.get() will read and return a, leaving sdf in cin's input buffer. Then the next cin.get() will read and return s, leaving df in cin's input buffer. And so on. Your code is not taking that into account. There is only 1 input buffer in cin, there is no per-function input buffer, like you are expecting.
If the user does not type in what you want, use cin.ignore() to discard the unwanted input. For example:
...
#include <limits>
...
int MainMenu()
{
cout << "\nPress the ENTER key\n";
char Input = cin.get();
if (Input != '\n')
{
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // <-- ADD THIS
cout << "I meant ONLY the ENTER key... Oh well its your health pool.\n";
CharHealth(-2);
cout << "You took 2 damage. \nYou now have " << CharHealth(0) << "Health.\n";
}
return 0;
}
int Introduction()
{
cout << "you awake in a puddle, walk to town. \nPress [ENTER]";
char Input = cin.get();
if (Input != '\n')
{
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // <-- ADD THIS
cout << "\nfor real?\nTake Another 2 Damage\nwhat did you think?... Idiot" ;
CharHealth(-2);
}
return 0;
}
cin.get() reads only one character and leave other inputted things on the stream.
It seems you want to read until \n is read. It can be done like this:
int MainMenu()
{
cout << "\nPress the ENTER key\n";
int count = 0;
while (cin.get() != '\n') count++;
if (count == 0)
{
return 0;
}
else if (count != 0) // we won't need this if statement, but I respect you
{
cout << "I meant ONLY the ENTER key... Oh well its your health pool.\n";
CharHealth(-2);
cout << "You took 2 damage. \nYou now have " << CharHealth(0) << "Health.\n";
return 0;
}
}
(warning: This code will fail into an infinite loop if we get EOF before newline)

How to check if std::cin has failed

I am making a game and at the start, the player needs to assign points to different categories of their character (like Fallout's SPECIAL). Player input is currently read using std::cin. If the player inputs a letter instead of a number, std::cin will fail and crash the entire game.
I have attempted to check for failure with std::cin.fail() but no luck.
The relevant code which reads player input can be found below:
for(int i = 0; i < 5; i++) {
switch(i) {
case 0:
cout <<"\n\nStrength (How strong you are)";
cout <<"\nHow many points (you have " << total_SKILL_points << " left): ";
std::cin >>Strength;
if(std::cin.fail()) {
std::cin.clear();
cout <<"\n\nPlease enter a number!";
i--;
break;
}
total_SKILL_points = total_SKILL_points - Strength;
break;
// keeps going, that's why no closing } for switch or for loop.
Is there any proper way to check if a letter has been passed to std::cin?
You can clear the error flags and ignore the rest of the line if you get bad input.
#include <iostream>
#include <limits>
#include <list>
#include <string>
int main() {
int total_SKILL_points = 100;
int Strength;
while(true) {
std::cout << "\n\nStrength (How strong you are)\n"
"How many points (you have " << total_SKILL_points << " left): ";
if(std::cin >> Strength) { // check that cin is in a good state after extraction
// success
break;
} else {
// failure
if(std::cin.eof()) {
std::cout << "user aborted\n";
return 1;
}
std::cout << "\n\nPlease enter a number!";
// clear error flags
std::cin.clear();
// ignore rest of line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
std::cout << Strength << "\n";
}
I think try and catch is a good option here. You could do something like:
for (int i = 0; i < 5; i++){
switch(i){
case 0:
std::cout <<"\n\nStrength (How strong you are)";
std::cout <<"\nHow many points (you have " << total_SKILL_points << " left): ";
std::cin >>Strength;
int intStrength;
try{
intStrength = std::stoi(Strength);
}
catch(std::invalid_argument const &e){
std::cout <<"\n\nPlease enter a number!" << std::endl;
i--;
break;
}
total_SKILL_points = total_SKILL_points - intStrength;
break;
I have used the method that Peter gave. Thanks for the help.
Peter's answer for those skimming this post:
Using std::cin.fail() doesn't work in your case because the stream itself is not in an error state. std::cin >> Strength has stopped reading because of invalid input and left the invalid input in the stream to be encountered on the next read operation - but it does NOT put the stream itself in an error state. Instead of using std::cin >> Strength, read a line of input using
std::getline(std::cin, astring)
where a string is of type std::string and parse the string to check if it has required input or other data.
Note: don't mix use of std::getline() with >> on the same stream.

Print out last line and then run program again Y/N

I am stuck on this random guessing game for school.
I have added the code that needed to be added, but the console keeps closing without returning the last strings.
I would also like to learn how to make the program run again with clicking Y to run again.
I am still learning C++, so any help would be appreciated.
Code:
// GuessingGameApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>//added to run string
#include <locale>//added toupper run again
using namespace std;
int main()
{
//Seed the random number generator
srand(time(0));
int selectedNumber = rand() % 20 + 1; int numberOfTries = 0;
int inputtedGuess;
std::cout << "Guess My Number Game\n\n";
//Ask the user for a value until the correct number is entered
do {
std::cout << "Enter a guess between 1 and 20:";
std::cin >> inputtedGuess;
++numberOfTries;
if (inputtedGuess > 20 || inputtedGuess < 1) {
cout << "Your guess is out of range.\n\n";
}
else if (inputtedGuess > selectedNumber) {
cout << "Too high!\n\n";
}
else if (inputtedGuess < selectedNumber) {
cout << "Too low!\n\n";
}
}
while (inputtedGuess != selectedNumber);
//Congratulate the user and end the program
std::cout << "\nCongratulations! You solved it in " << numberOfTries << " tries!\n" << std::endl;
//fix problem with console closing and (add "play again" option), so I can
//learn
//printf; did not work... Break did not work..
//
return 0;
}
I was able to get the console to stay open by putting a break at line 33, but I want to learn how to do this correctly so I deleted the break.
The last line of your output should actually be printed. The reason why the last line "is not printed" is probably that your IDE closes the console before you can see the final output (though it should be there). Many IDEs allow to make the console visible after program termination. BTW: Note that when pasting the code you probably lost a << before std::endl in std::cout << "\nCongratulations! You solved it in " << numberOfTries << " tries!\n" std::endl; But this has actually to be a copy-paste problem, because your program would not have compiled otherwise.
Anyway, by providing a "Try again?"-logic, your program does not terminate and the problem is solved.
I'd suggest to provide a separate function performing the guess, which is then called in a do-while loop with the "Try again="-question.
void guess() {
// your code (except srand) goes here...
}
int main() {
srand(time(0)); //Seed the random number generator only once
char doAgain;
do {
guess();
cout << "Try again (Y/N)?";
cin >> doAgain;
}
while (toupper(doAgain)=='Y');
return 0;
}
As people suggested, you can add another do-while loop to repeat the game.
int choice = 0;
do {
// first part of code..
do {
// inner do-while
} while (inputtedGuess != selectedNumber);
std::cout << "\nCongratulations! You solved it in " << numberOfTries << " tries!\n" std::endl;
cout << "\nWould you like to play again?\n\n";
cout << "1 - Yes\n";
cout << "2 - No\n\n";
cout << "Choice: ";
cin >> choice;
} while(choice == 1);

Guess the number - Infinite loop when bad read

So I am making this Guess the number game in c++ that looks like this:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int secretNumber = rand() % 100 + 1; //Generate "Random number"
int nbrOfGuesses = 0;
int userInput;
cout<<"\t************************************"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t* Guess the number! *"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t************************************"<<endl;
cout<<endl;
cout << "Try to find the secret int number: " << endl;
//While input is good
while(cin.good())
{
//Do this
do {
cin>>userInput;
nbrOfGuesses++;
if (userInput>secretNumber)
cout << "Smaller!\n";
else if(userInput<secretNumber)
cout << "Bigger!\n"; // <-- Infinite loop here when you enter something other than an integer
else //Also using this as a backup of (cin.good())
cout << "Something went wrong with the read";
break;
} while(userInput!=secretNumber);
cout << "\nCongratulations! You got it in " << nbrOfGuesses << " guesses\n";
}
system("pause");
return 0;
}
*Sorry if the code is note very elegant
As you can see, the code works great until you enter a random caracter like '&' or 'j' or anything else that isn't an integer...Then it loops at cout<<"Bigger!";
So my question is: What is causing this?
Check this post, it is about the same problem. To summarize:
cin>>userInput;
if (cin.fail()) {
cout<<"Invalid Entry, please try again."<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Thanks ildjarn for pointing the missing ignore statement, I missed that part even though its clearly mentioned in the post I linked to!!
See this FAQ: How can I get std::cin to skip invalid input characters?
cin>>userInput;
If it cannot read an integet, the bad bit is set for the cin stream.
You should check and clear afterwords.
if ( cin.fail() )
{
cin.clear();
try again
}