# Char string review

#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;
}

-

#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;.

-
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"). –  Jamal Jan 1 '14 at 19:57
@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 –  200_success Jan 1 '14 at 20:00
Fair enough. You may add that to your answer, especially for others not too familiar with such IDEs. –  Jamal Jan 1 '14 at 20:03
• 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.

-

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.

-
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. –  Loki Astari Jan 1 '14 at 21:05