String Breaks Code, Void Lets It Run peacefully, help

Beginner here,
Did the challenge but I did it without making a string function and instead made it a void func. After fixing it, I noticed it crashed when “GetGuessAndPrintBack()” was written as a string instead of a void func.

When I say “crash,” I mean after inputting a value after running it, it stops running and Windows reports a crash with its error box.
It is also worth knowing that after the program “crashes” according to Windows, it immediately resumes and continues working.

Posting the entire code so it can be examined.

#include
#include

using namespace std;

void PrintIntro();
void GetGuessAndPrintBack();

//entry point of app
int main() {
PrintIntro();
GetGuessAndPrintBack();
GetGuessAndPrintBack();
return 0;
}

// introdurces game
void PrintIntro() {
constexpr int WORD_LENGTH = 5;
cout << “Welcome to Bulls and Cows, a fun word game.” << endl;
cout << "Can you guess the “;
cout << WORD_LENGTH;
cout << " letter isogram I am thinking of?\n”;
return;
}

//get a guess from player and plays back

void GetGuessAndPrintBack() {
string Guess = “”;
cout << “Input a guess now.\n”;
std::getline(std::cin, Guess);
cout << "Your word is ";
cout << Guess;
cout << “, correct?\n”;
return;
}

Right at the end here, with the GetGuessAndPrintBack(), it crashes if I switch that to a string and introduce it as a string.

What’s the issue?

hmm…when you do you function as a string, does it look like this?

string GetGuessAndPrintBack()
{
string Guess="";
…(rest of the code)…
return Guess;
}

or did you leave return empty? Because since you’re declaring a string function instead of a void, the program gonna want you to return something. Which you don’t if you leave a empty return.

Alright, I modified the code to look like that, and it functioned all right, and no more crashes occurred. Thanks for the help.

Yeah, I was unaware of that requirement for using string compared to void.

Privacy & Terms