What is wrong with this code?

  1. somehow the game stopped running after I added the AskToPLayNewGame function. I can’t figure out why. The program doesn’t even let me type in the first 5 guesses. It crashes immediately after telling me to make the first guess.

  2. Also the using namespace std; function did not work for me. I was getting errors sometimes.

  3. If I try to use endl; then the editor tells me i have overloaded the function. What does this mean?

//
// main.cpp
// cows and bulls game
//
// Created by Skjalg Remme on 09/04/2017.
// Copyright © 2017 Skjalg Remme. All rights reserved.
//

#include
#include

void PrintIntro ();
std:: string GetGuess ();
std:: string printResult();
void PlayGame();
bool AskToPLayNewGame ();

//This is where the code initializes
int main ()
{

PrintIntro();

PlayGame();
std:: cout << AskToPLayNewGame();
return 0;

}

//functions in this game are placed below here and called before the main function is called.

//Introduces the game and sets word length
void PrintIntro ()
{
constexpr int word_length = 5;
std::cout << “Welcome to the Cows and Bulls game where you guess a " << word_length <<” letter isogram\n";

std::cout << "Please make your guess by typing in the word:\n";

return;

}

//gets the player guess
std:: string GetGuess ()
{
std:: string Guess = “”;
std:: getline ((std:: cin), Guess);
std:: cout << endl;

return Guess;

}

//Plays the game and sets number of turns

void PlayGame()
{
constexpr int Number_of_guesses = 5;
for (int count=1; count<=Number_of_guesses; count++)
{
std:: string Guess = GetGuess();
std:: cout << “You have guessed” << Guess << endl;
}
return;
}

bool AskToPLayNewGame ()
{
std:: cout << “Do you want to play again?\n”;
std:: string Response = “”;
std:: getline (std:: cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);

}

BTW. I am using xcode 8.3

Hello @Skjalg_Remme
I am not a participator of this UE-course.
Your program is doing what your code wants it to do.
To be more precisely:
You are asking the Player to play again, but after that the next instruction in your main()-function is “return 0”.
So the program will exit after asking.

Maybe you want to put a loop in your main-function.
Something like:

int main(void){
    bool playagain=true;
    while(playagain){
        PlayGame();
        playagain=askToPlayNewGame();
    }
    return 0;
}

Hope that helps.
Chris.

Thanks, Chris. Somehow its not doing it in xcode for me. I get the lldb error code as soon as it has printed the intro. I fell like a ****** for not seeing what I’m doing wrong.

Can you post a screenshot of the error-messages?

I don’t know if it is a formatting error here in your code, but what are you including?
I can only see “include” without any header-files.

hm… Can’t see any errors in there.
Hope anyone can help you.
Only thing I see is that you have some whitespace between the namespace operator “::” and the types (string) or functions/methods (cin/cout) . Is this allowed?

Did you try to comment-out some code-blocks or functions to find the location of the error.

Sorry. Cannot find an error.
Now I feel like a ****** too for not seeing what’s wrong. :smiley:

[edit] Did you try “std::endl” instead of “endl”?

I tried commenting out the code for AskForNewGame. The game still stops. The wierd thing is it was running fine until that point. I always put std:: endl; since xcode decided to sometimes ignore the using namespace std; command. I almost bought a pc today just to see if it was just xcode that was acting wierd. :joy:

DId you try removing the whitespace between the namespace-operators?
It is very strange, PrintIntro() is working, while the other functions are not.

use
std::string ...

instead of:
std:: string ...

Same for the std::cin, std::cout,…

I deleted all the whitespaces but stil the same. It should ignore any whitespaces anyway

I finally figured it out. after like 5 hours I noticed a little blue triangle on one of the lines. Apparently i had manually selected to put in a breakpoint in the code somehow?!? So weird… Thanks for the help! :smiley:

1 Like

Privacy & Terms