5
\$\begingroup\$
#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
    for (int i = 0; i <= 3; i++) //give the user 4 guesses at the word.
    {   
        std::string guess; 
        cout <<"Guess the word" <<endl;
        cin >>guess;

        if (guess == "Dog")
        {
            cout <<"You guessed correctly. " <<endl;
            break;
        }
        else
        {
            cout <<"You guessed incorrectly. " <<endl;
        }
    }
    char f;
    cin >>f;
    return 0;
}
\$\endgroup\$

3 Answers 3

5
\$\begingroup\$

#include "stdafx.h" is superfluous.

The standard idiom in C++ for doing something 4 times is:

for (int i = 0; i < 4; i++) {
    ...
}

Don't deviate from that pattern unless there is a good reason.

Requiring an extra character input to exit the program is a weird user experience, especially since there is no prompt. I would just get rid of char f; cin >> f;.

\$\endgroup\$
3
  • \$\begingroup\$ With such a "pause" in place, it's fair to assume the OP is using an IDE (probably with no built-in pause). It is also much better than system("PAUSE"). \$\endgroup\$
    – Jamal
    Jan 1, 2014 at 19:57
  • \$\begingroup\$ @Jamal That makes sense. (I usually don't use an IDE.) In that case, a comment would be useful: // Pause so that output can be examined when running in an IDE \$\endgroup\$ Jan 1, 2014 at 20:00
  • \$\begingroup\$ Fair enough. You may add that to your answer, especially for others not too familiar with such IDEs. \$\endgroup\$
    – Jamal
    Jan 1, 2014 at 20:03
5
\$\begingroup\$
  • Try not to use using namespace std.

  • Be sure to include <string> since you're using std::string.

  • Prefer std::getline() over operator>> for all user input into an std::string:

    std::getline(std::cin, guess);
    
  • If you want to avoid case-sensitivity issues, I'd recommend having both guess and the word in the same case (lowercase or uppercase).

    You can use std::transform() on guess to make it lowercase or uppercase, and have the word provided in the same case:

    // get the input
    
    // transform the guess string to all lowercase
    std::transform(guess.begin(), guess.end(), guess.begin(), ::tolower);
    
    if (guess == "dog")
    {
        // ...
    }
    
  • The "pause" at the end is okay, but here's another one:

    std::cin.get();
    

    This one doesn't involve an extra variable, although it doesn't really matter. The program still "waits" for user input before exiting.

\$\endgroup\$
0
0
\$\begingroup\$

Looks good to me. Don't know what the last cin >> f is for but the function runs well. Just a tip, you don't need to include the std:: scope in front of string guess, as you've already declared that you're using namespace std.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Note he should not be using using namespace std; so prefer to add std:: to all the places it has been left off. The cin >> f forces the application to pause until the user hits return. If running in a hosted environment this prevents the window from closing too quickly to see the answer. \$\endgroup\$ Jan 1, 2014 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.