This is my code. Everytime I want to run for the 2nd or 3rd time, before i even get a chance to type in a "y" or an "n" the program ends unexpectedly. The error says it is a normal program termination. Is there something wrong with my code?
I tried putting the cin.ignore() on line 43 but now it says exited, illegal instruction. I also made some expansions to my program but commented out the irrelevant parts to it.
#include <iostream>
#include <string>
#include <limits>
#include <cstdlib>
#include <ctime>
usingnamespace std;
// Get an integer with prompt
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
// Asks the user to play again
bool playAgain() {
int again {};
do {
again = getInt("Again? 1=Yes, 2=No: ");
} while ((again < 1 || again > 2) && (cout << "Not a valid input\n"));
return again == 1;
}
int main() {
srand(static_cast<unsignedint>(time(nullptr)));
do {
constauto times {getInt("How many times would you like to shuffle the phrase? ")};
string phrase;
cout << "Enter the phrase to scramble: ";
getline(cin, phrase);
} while (playAgain());
}
//test problem
#include <iostream>
#include <limits> // <--- Added.
#include <string>
#include <vector>
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
usingnamespace std; // <--- Best not to use.
//Asks the user to play again
bool playAgain()
{
char choice{}; // <--- Always good to initialize your variables.
cout << "Again? y=Yes, n=No: ";
cin >> choice;
cin.ignore(); // <--- Only clears 1 character or to "Traits::eof()". Unless another deliniter is specifird. https://en.cppreference.com/w/cpp/io/basic_istream/ignore
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. A more portable way.
if (std::tolower(choice) == 'y') // <--- What would happen if the user entered 'Y'?
{
returntrue;
}
//else
//{
returnfalse;
//}
}
int main()
{
do
{
string text; // <--- Empty when defined. Does not need initialized. Unless you want to give it a starting value other than ("").
cout << "Enter text: ";
getline(cin, text);
} while (playAgain());
return 0; // <--- Not required, but makes a good break point.
}
agent max makes a good point about mixing formatted input, (cin>>choice;, with unformatted input, the "getline". I tend to like following formatted input with the std::cin.ignore(...). statement to make sure that the buffer is clear before any unformatted input. But either way should work.
I will have to try your latest version, but for now I do not see anything wrong with what you did.
Also it helps if you mention what IDE/compiler yo are using and post the complete error message. Others may understand the error message better than you do right now.
Andy
In the function the "else" is not needed. Should the if statement be true you return true and leave the function. Should the if statement be false the if statement will be skipped and the only thing left to do is return false.