In this program the do and while statement isn't working...maybe I did it wrong I don't know but all I wanted the do and while to do was restart the program.. Here is the code:
#include <iostream>
usingnamespace std;
string i;
string n;
string s;
string w;
string e;
int m;
char restart;
int main()
{
do{
//Initial Welcome statement and Question
cout << "This is CAPS Sensitive so copy your answer the way you see it" << endl;
cout << "Hello world! Do you wish to continue with your adventure? YES or NO" << endl;
cin >> i;
//this is the yes continuing
if (i == "YES"
){
cout << "Which Direction do you want to move? NORTH , SOUTH , EAST , or WEST" << endl;
cin >> i;
//Here start the options for the last question
//Start of north
if (i == "NORTH"){
cout << " You walk about 30 Miles North and start a campfire.. What will you fuel it with? GASOLINE or TIMBER " << endl;
cin >> i;
if (i == "GASOLINE"){
cout << " The fire explodes flames and catches yourself and your surroundings on fire, You die alive in the fire " << endl;
}
elseif (i == "TIMBER"){
cout << "The fire burns beautifully in the sky.... Do you wish to SLEEP or LOOK AT THE STARS" << endl;
cin >> n;
} if (n == "SLEEP"){
cout << " You sleep peacefully until a pack of wolves come and find you sleeping and eat you " << endl;
}
elseif (n == "LOOK AT THE STARS"){
cout << "you look at the stars until it starts to rain....when you get up to go into the tent you get hit by lightning and die instantly" << endl;
}
}
}
//start of south
if (i == "SOUTH"){
cout << " You fall in a ditch break a leg and can't crawl out.. Will you SUICIDE or CRAWL?" << endl;
cin >> i;
if (i == "SUICIDE"){
cout << " You have ended your misery " << endl;
}
elseif (i == "CRAWL"){
cout << "A pack of wolves have tracked your scent and finding easy lunch they eat you " << endl;
}
}
//start of west
elseif (i == "WEST"){
cout << " You come face to face with a bear....do you wish to STRIKE or RUN?" << endl;
cin >> i;
if (i == "STRIKE"){
cout << " You don't hurt the bear in the slightest but with rage the bear rips you to shreds " << endl;
}
elseif (i == "RUN"){
cout << " You run for about 10 minutes before you trip and fall down a steep cliff while bash your head against the cliff side and die instantly " << endl;
}
}
//start of east
elseif (i == "EAST"){
cout << " You get lost in the wilderness and can't find your way back after about 5 hours of walking.... Do you wish to keep walking in the same DIRECTION or just QUIT? " << endl;
cin >> e;
if (e == "DIRECTION"){
cout << " You eventually come upon an abandoned house and see a radio inside and call for help...You get airlifted to the nearest hospital and you live happily ever after " << endl;
cout << "if you liked it or got annoyed by the endless solutions feedback is welcome!!" << endl;
}
elseif (e == "QUIT"){
cout << " You died a lonely death " << endl;
}
}
//This is the NOT Wishing to continue the adventure
elseif (i == "NO"){
cout << "Oh, Well maybe you will play another day... How about that!" << endl;
}
std::cin >> m;
cout << "Again? 0 for Yes,1 for No" << endl;
cin >> restart;
}
while (restart == 0);
return 0;
}
You must use consistent indentation and whitespace if you ever hope to be able to read and examine all the nested scopes here. I took your code and indented it, which now clearly shows that your NORTH, SOUTH, EAST, and WEST choices aren't chained together like you think they are. Look closely at the comments on lines 49-50:
#include <iostream>
#include <string>
usingnamespace std;
string i;
string n;
string s;
string w;
string e;
int m;
char restart;
int main()
{
do
{
//Initial Welcome statement and Question
cout << "This is CAPS Sensitive so copy your answer the way you see it" << endl;
cout << "Hello world! Do you wish to continue with your adventure? YES or NO" << endl;
cin >> i;
if (i == "YES")
{
cout << "Which Direction do you want to move? NORTH , SOUTH , EAST , or WEST" << endl;
cin >> i;
if (i == "NORTH")
{
cout << " You walk about 30 Miles North and start a campfire.. What will you fuel it with? GASOLINE or TIMBER " << endl;
cin >> i;
if (i == "GASOLINE")
{
cout << " The fire explodes flames and catches yourself and your surroundings on fire, You die alive in the fire " << endl;
}
elseif (i == "TIMBER")
{
cout << "The fire burns beautifully in the sky.... Do you wish to SLEEP or LOOK AT THE STARS" << endl;
cin >> n;
}
if (n == "SLEEP")
{
cout << " You sleep peacefully until a pack of wolves come and find you sleeping and eat you " << endl;
}
elseif (n == "LOOK AT THE STARS")
{
cout << "you look at the stars until it starts to rain....when you get up to go into the tent you get hit by lightning and die instantly" << endl;
}
} //this closing brace matches the opening one on line 28
} //this closing brace matches the opening one on line 23
if (i == "SOUTH")
{
cout << " You fall in a ditch break a leg and can't crawl out.. Will you SUICIDE or CRAWL?" << endl;
cin >> i;
if (i == "SUICIDE")
{
cout << " You have ended your misery " << endl;
}
elseif (i == "CRAWL")
{
cout << "A pack of wolves have tracked your scent and finding easy lunch they eat you " << endl;
}
}
elseif (i == "WEST")
{
cout << " You come face to face with a bear....do you wish to STRIKE or RUN?" << endl;
cin >> i;
if (i == "STRIKE")
{
cout << " You don't hurt the bear in the slightest but with rage the bear rips you to shreds " << endl;
}
elseif (i == "RUN")
{
cout << " You run for about 10 minutes before you trip and fall down a steep cliff while bash your head against the cliff side and die instantly " << endl;
}
}
elseif (i == "EAST")
{
cout << " You get lost in the wilderness and can't find your way back after about 5 hours of walking.... Do you wish to keep walking in the same DIRECTION or just QUIT? " << endl;
cin >> e;
if (e == "DIRECTION")
{
cout << " You eventually come upon an abandoned house and see a radio inside and call for help...You get airlifted to the nearest hospital and you live happily ever after " << endl;
cout << "if you liked it or got annoyed by the endless solutions feedback is welcome!!" << endl;
}
elseif (e == "QUIT")
{
cout << " You died a lonely death " << endl;
}
}
elseif (i == "NO")
{
cout << "Oh, Well maybe you will play another day... How about that!" << endl;
}
std::cin >> m;
cout << "Again? 0 for Yes,1 for No" << endl;
cin >> restart;
} while (restart == 0);
return 0;
}
Assign the variable, restart, as char restart = '0'; at the start. Then use while (restart == '0'); at the end. Right now, you're checking for an int. with restart, but need a char, instead. Or, you can change the declaration of restart TO an int, and leave the while check, as is.
Thank You I really appreciate your help but would there be a way to make it to where you can add a string instead of a number? Like YES or NO words instead of 0 and 1?
Never mind I found that instead of Char i can just use string r; and for the while statement just change it to while (r == "YES");.... Thank you all for your help!