My approach to 'for' loops

#include
#include

using namespace std;

void printIntro();
void getGuessAndPrintIt();

int main() //entry point of the game
{
constexpr int MAX_TRIES = 5;
printIntro();

for (int count = 1; count <= MAX_TRIES; count++)
{
    getGuessAndPrintIt();
}

return 0;

}

void printIntro() //print the intro text
{
constexpr int WORD_LENGTH = 9;
cout << “Welcome to Bulls & Cows, a freaking awesome game by Mornarion.\n”;
cout << “Try to guess the " << WORD_LENGTH;
cout << " letters isogram that I’m thinking… If you dare!!!\n\n”;
return;
}

void getGuessAndPrintIt() //get a guess from player
{
string Guess = “”;
cout << "What is your guess?: ";
getline(cin, Guess);
//return the guess to the playa
cout << "Your guess is: " << Guess << endl << endl;
return;
}

You may notice my getGuessAndPrintIt() is not of type string but void… I chose so because it’s not really returning anything (anything that is used by the program, anyway), so I intend to change it to string type once it’s necessary.

I am also using camelcase for methods and variables. Call me a rebel, if you will… XP

Should be fine for now but using PascalCase would be preferred when using the engine for consistency sake.

1 Like

Indeed, I am already changing to PascalCase. Thanks for the advice.

Greetings,

Javier

Pasting your code directly like this, makes it really hard to read, I advice to use pastebin

Its free and easy to use, you dont need an account to use it, but account will make it easier to manage multiple snippets of code. Pastebin is used a lot by coders to show code.

1 Like

Forum already supports code formatting tthough it could be improved I guess as it’s rather narrow, @Mornarion just didn’t use it. e.g:

using namespace std;

void printIntro();
void getGuessAndPrintIt();

int main() //entry point of the game
{
    constexpr int MAX_TRIES = 5;
    printIntro();
    for (int count = 1; count <= MAX_TRIES; count++)
    {
        getGuessAndPrintIt();
    }
    return 0;
}

void printIntro() //print the intro text
{
    constexpr int WORD_LENGTH = 9;
    cout << "Welcome to Bulls & Cows, a freaking awesome game by Mornarion.\n";
    cout << "Try to guess the " << WORD_LENGTH;
    cout << " letters isogram that I'm thinking... If you dare!!!\n\n";
    return;
}

void getGuessAndPrintIt() //get a guess from player
{
    string Guess = "";
    cout << "What is your guess?: ";
    getline(cin, Guess);
    //return the guess to the playa
    cout << "Your guess is: " << Guess << endl << endl;
    return;
}

What you use for the code ? Preformated text ?

Still missing syntax highlighting , but this is much better

Yeah that’s what I used, and while syntax highlighting is nice, it’s not really needed for code to be readable.

Yeah that’s what I used, and while syntax highlighting is nice, it’s not really needed for code to be readable.

And I am wrong once again :smiley: the forum actually supports code snippets and syntax highlighting

using namespace std;

void printIntro();
void getGuessAndPrintIt();

int main() //entry point of the game
{
    constexpr int MAX_TRIES = 5;
    printIntro();
    for (int count = 1; count <= MAX_TRIES; count++)
    {
        getGuessAndPrintIt();
    }
    return 0;
}

void printIntro() //print the intro text
{
    constexpr int WORD_LENGTH = 9;
    cout << "Welcome to Bulls & Cows, a freaking awesome game by Mornarion.\n";
    cout << "Try to guess the " << WORD_LENGTH;
    cout << " letters isogram that I'm thinking... If you dare!!!\n\n";
    return;
}

void getGuessAndPrintIt() //get a guess from player
{
    string Guess = "";
    cout << "What is your guess?: ";
    getline(cin, Guess);
    //return the guess to the playa
    cout << "Your guess is: " << Guess << endl << endl;
    return;
}

to do this you have to add the code this way without using preformated text

  1. Start your code with " ```c++ " , this is markdown syntax which points that start of your code snippet and which language you use
  2. copy paste your code
  3. close the code snippet with " ``` "

be careful its ` not ’ , ` is located to the same key with ~ , at least on my keyboard

2 Likes

That information may be worthy of it’s own sticky / help section!

Yes I have created a thread about this here

Its up to moderators to turn this to a sticky