I am stumped by wrong cout

This is a small portion of a program I am making. When the correct answer is entered, it still says incorrect. It is probably really simple, but I cant find my error.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int animalType;

int main()
{
string penguin;
string answer;
int numGuesses;


for (numGuesses = 4; numGuesses > 0; numGuesses--)
{
cout << "I like to swim." << endl;
cin >> answer;

if (answer == penguin)
cout << "That is correct!" << endl;

else
numGuesses--;
cout << "Incorrect. You have " << numGuesses << " guesses left." << endl;
cout << "I like to eat fish." << endl;
cin >> answer;

if (answer == penguin)
cout << "That is correct!" << endl;

else
numGuesses--;
cout << "Incorrect. You have " << numGuesses << " guesses left." << endl;
cout << "I am mostly black and white." << endl;
cin >> answer;

if (answer == penguin)
cout << "That is correct!" << endl;

else
numGuesses--;
cout << "Incorrect. You have " << numGuesses << " guesses left." << endl;
cout << "I usually live where it is very cold." << endl;
cin >> answer;

if (answer == penguin)
cout << "That is correct!" << endl;

else
numGuesses--;
cout << "Incorrect. You have " << numGuesses << " guesses left." << endl;
cout << "I am a penguin." << endl;


}




return 0;
}
You are declaring a variable named penguin. You never assign any string to it, therefore it is an empty string.

This line here:

 
if(answer == penguin)


This compares the variable answer to the variable penguin.

Since penguin=="" (and empty string), you're effectively doing this:

if(answer == "")


What you probably want to do is compare answer to the literal string, "penguin". In which case you don't need the penguin variable. Instead, do this:

 
if(answer == "penguin")  // note the quotes here 



EDIT: plus what Return 0 said.
Last edited on
You need to enclose your if...else statements with { } if there is more than one statement following an if or else or else if. Without { } only the first statement following the if, else, etc... will be executed if the condition is true. Everything else will always be output.
Last edited on
Thanks, I will put that into effect.
Topic archived. No new replies allowed.