Here is all challenges for my unreal course

This is where I’ll put all my challenge results when the course prompts me to do so.

Here is the code I used for the forloop challenge

In this challenge he asks to have the player guess 5 times using a forloop.

#include <iostream> 
#include <string>
using namespace std; 

void PrintIntro();
string GetGuessAndPrint(); 

int main() {

    PrintIntro();
    //have the user guess a number of times
    int numberOfGuesses = 5;
    for(int i = 0; i < numberOfGuesses; i++) {
        GetGuessAndPrint();
    }
    

    return 0; 
}

void PrintIntro() { 
     //introduce the game 
    constexpr int WORD_LENGTH = 5; 
    cout << "In this game we guess a word isogram with " << WORD_LENGTH << " letters!";  
    return;
}

string GetGuessAndPrint() {

    //get a guess from the player
    string guess = "";
    cout << endl << "Hey, Guess a word!\n";
    getline(cin,guess);
    //repeat their guess back to them
    cout << "Your guess was " << guess;

    return guess; 
}
1 Like

and here is where I changed “GetGuessAndPrint” to “GetGuess” and moved the code for printing it back to the player

#include <iostream> 
#include <string>
using namespace std; 

void PrintIntro();
string GetGuess(); 
void PlayGame();

//the entry point of our application
int main() {

    PrintIntro();
    PlayGame();
   
    return 0; 
}

void PrintIntro() { 
    //introduce the game 
    constexpr int WORD_LENGTH = 5; 
    cout << "In this game we guess a word isogram with " << WORD_LENGTH << " letters!";  
    return; 
}

string GetGuess() {

    //get a guess from the player
    string guess = "";
    cout << endl << "Hey, Guess a word!\n";
    getline(cin,guess);

    return guess; 
}

void PlayGame() {
     //have the user guess a number of times with loop
    constexpr int NUMBER_OF_GUESSES = 5;
    for(int i = 0; i < NUMBER_OF_GUESSES; i++) {
        string guess = GetGuess();
        cout << "Your guess is: " << guess << endl; 
    }
}

Here is a screenshot of my theoretical Class for the “Bull Cow Game”

1 Like

Privacy & Terms