New to C++, Code Does not Function as Expected

Hello,

I have never posted to a forum before as I am completely new to the c++ world. I have been playing around with IF ELSE and DO WHILE statements. I wrote the code below as a sort of interactive story as I thought it would be good practice. The last section, lines 142 - 216, is where I am having the trouble. Everything runs, and the code does not fire an error; however, the code puts my error message every time I enter the appropriate answer for option1. I will still get the detail I want, but that populates after the error message. The error message does populate successfully when I type the wrong answer.

I'm looking to remove the error message from the beginning of every correct statement, so that it does not fire false alarms. Any help you can provide would be greatly appreciated. Below is the section that is not firing correctly. Not enough space for the entire code.

do
{
if (option1 != ask && option1 != fight && option1 != Oasis && option1 !=Ignore)
{
cout<<"I'm sorry, please try again. Enter the correct value:\n";
cin>>option1;
}

if (Gender == f && option1 == fight)
{
cout<<"Novelie has chosen to sneak in and fight the people in the cabin.\n";
cout<<"When Novelie scopes the area around the cabin, she realizes it is\n";
cout<<"an outpost of soldiers! She sneaks around and takes each of them \n";
cout<<"one at a time, slowly and brutally slitting their throats from behind \n";
cout<<"Novelie Lives!";
}

if (Gender == f && option1 == ask)
{
cout<<"Novelie is seen approaching the cabin.\n";
cout<<"The arrow that penetrates her skull came faster than she could\n";
cout<<"imagine. The last thing she saw was a child in a tree\n";
cout<<"and her world went dark.";
}

if (Gender == m && option1 == fight)
{
cout<<"Gaedel has chosen to sneak in and fight the people in the cabin.\n";
cout<<"When Gaedel scopes the area around the cabin, he realizes it is\n";
cout<<"an outpost of soldiers! He sneaks around and takes each of them \n";
cout<<"one at a time, slowly and brutally slitting their throats from behind \n";
cout<<"Gaedel Lives!";
}

if (Gender == m && option1 == ask)
{
cout<<"Gaedel is seen approaching the cabin.\n";
cout<<"The arrow that penetrates his skull came faster than he could\n";
cout<<"imagine. The last thing he saw was a child in a tree\n";
cout<<"and his world went dark.";
}

if (Gender == f && option1 == Oasis)
{
cout<<"Novelie has chosen to make her way to the Oasis in hopes that she\n";
cout<<"would find some water to quench her thirst. After 3 hours of trudging\n";
cout<<"through the dessert, she finds that the Oasis was real! It was not a trick! \n";
cout<<"she sets up camp in the Oasis and begins her path to recovery. \n";
cout<<"Novelie Lives!";
}

if (Gender == f && option1 == Ignore)
{
cout<<"Novelie chooses to ignore the Oasis seeing that it may not be real and could\n";
cout<<"loser her valuable time. She dies of thirst and exposure before she reaches\n";
cout<<"the nearest town. Her body is lost forever.";
}

if (Gender == m && option1 == Oasis)
{
cout<<"Gaedel has chosen to make his way to the Oasis in hopes that he\n";
cout<<"would find some water to quench his thirst. After 3 hours of trudging\n";
cout<<"through the dessert, he finds that the Oasis was real! It was not a trick! \n";
cout<<"He sets up camp in the Oasis and begins his path to recovery. \n";
cout<<"Gaedel Lives!";
}

if (Gender == m && option1 == Ignore)
{
cout<<"Gaedel chooses to ignore the Oasis seeing that it may not be real and could\n";
cout<<"lose him valuable time. He dies of thirst and exposure before he reaches\n";
cout<<"the nearest town. His body is lost forever.";
}
}
while (option1 != Oasis && option1 != Ignore && option1 != fight && option1 != ask);


return 0;
}
[/code]
It looks like you tried to use the code tags to format your code, but you lost the first tag.

Some questions for you:

How does option1 get set before starting your "do-while" loop?
How are Oasis, Ignore, fight, and ask defined?
How are f and m defined?
Because you haven't inserted your code between the code and /code statements there are no lines 142 - 216 to know what you refer to. Also incomplete code cannot be run so there is no chance of determining an error by behavior or context.
Last edited on
Here is how I have them defined:

char Gender;
const char m = 'm';
const char f = 'f';
char Season;
const char w = 'w'; //this is going to allow the user to select Winter
const char s = 's'; //this is going to allow the user to select Summer
char option1;
const char fight = 'fight';
const char ask = 'ask';
const char Oasis = 'Oasis';
const char Ignore = 'Ignore';

Thank you for your quick responses.

I have two loops before this last one that are working, they are in between the declarations that I have just listed and the last part of the code.
A char type can't have more than one character,

const char fight = 'fight';
const char ask = 'ask';
const char Oasis = 'Oasis';
const char Ignore = 'Ignore';

Doesn't your compiler flag this?

Make them strings.
Thank you! That did it!
Topic archived. No new replies allowed.