Hey guys, I need a little help with this game I am writing for a project.
I am having an issue with the do-loop function. When the game starts, the 3rd option is to quit the game; however, it doesn't do that right away. I think the error is where I put the "while" statement or maybe the brackets. I would appreciate your wisdom.
// This is a text based game
#include<ctime>
#include<iostream>
usingnamespace std;
int main()
{
int rules (1);
int play (2);
int quit (3);
int chose ;
char again ;
cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";
{
do{
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " ////\\\\\\\\ |||||||||\\\\\\\\ |||||||| |||\\\\\\\\ |||| ////\\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ ||||||||||\\\\\\\\ |||||||| ||||\\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ |||| \\\\\\\\ |||| |||| \\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " |||| |||| |||| //// |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||||||||||||| ||||||||||//// |||||||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||||||||||||| ||||||||||||| |||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\|||| |||| ||||"<< endl ;
cout << "\n1:Rules\n2:Play\n3:Quit\n";
cin >> chose ;
{
if (chose == 1) // Rules
{
cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";
}
elseif (chose == 2) // Race
{
int race;
int human (1);
int orc (2) ;
int elf (3) ;
cout << "\nYou are about to enter the arena.\nBut first" << endl;
cout << "select your race.\n";
cout << "1:Human\n2:Orc\n3:Elf\n";
cin >> race ;
if (race == 1) // Human
{
cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";
human = (100);
}
elseif (race == 2) //Orc
{
cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;
orc = (200);
}
elseif (race == 3) // Elf
{
cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";
elf = (300);
}
{
cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;
cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";
int sword (1);
int bow (2);
int magic (3);
int weapon ;
int score;
cin >> weapon ;
int random;
srand(time(NULL));
if (weapon == 1 || weapon == 2 )
{
random = rand() % 4 +1;
cout << random << endl;
if (random == 1 )
{
score = score + 100;
cout << "You hit your target! you have a final score of " << score << "." << endl;
}
if (random == 2 )
{
score = score + 200;
cout << "You hit your target! your final score is " << score << "." << endl;
}
if (random == 3 )
{
score = score + 300;
cout << "Critical hit, your final score is " << score << "." << endl;
}
if (random == 4 )
{
score = 0;
cout << "you missed your taget! you score is " << score << "." << endl;
}
}
elseif (weapon == 3)
{
random = rand () % 2 +1;
cout << random << endl;
if (random == 1)
{
score = score + 200;
cout << "You where successful, " << score << " is awarded." << endl;
}
if (random == 2)
{
score = score - 100;
cout << "You fail! " << score << " is deducted." << endl;
}
}
}
}
cout << "\nWould you like to return to the main menu (y/n)? ";
cin >> again;
}
}while ( again == 'y' || again == 'Y' );
}
if (chose == 3) // Quit
{
cout << "you quit the game.\n";
}
return 0;
}
a) its called a do-while loop
b) its not a function
c) whats up with the white space?
d) could you make it in a nicer format so we can read it easier?
e) why not use an enum instead of taking up space with unused variables?
Yeah, sorry about that. This is only my 4th week learning c++, So I am still in the basic. I was writing it on CentOs, but I understand I left many big gaps in the program. I haven't learned about enum yet. :/ I will fix it up to the best I can, then repost it again.
// By Christopher Alfonso
//cop1334c JC
// This is a text based game
#include<ctime>
#include<iostream>
usingnamespace std;
int main()
{
int rules (1);
int play (2);
int quit (3);
int chose ;
char again ;
cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";
{
do{
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " ////\\\\\\\\ |||||||||\\\\\\\\ |||||||| |||\\\\\\\\ |||| ////\\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ ||||||||||\\\\\\\\ |||||||| ||||\\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ |||| \\\\\\\\ |||| |||| \\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " |||| |||| |||| //// |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||||||||||||| ||||||||||//// |||||||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||||||||||||| ||||||||||||| |||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\|||| |||| ||||"<< endl ;
cout << "\n1:Rules\n2:Play\n3:Quit\n";
cin >> chose ;
{
if (chose == 1) // Rules
{
cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";
}
elseif (chose == 2) // Race
{
int race;
int human (1);
int orc (2) ;
int elf (3) ;
cout << "\nYou are about to enter the arena.\nBut first" << endl;
cout << "select your race.\n";
cout << "1:Human\n2:Orc\n3:Elf\n";
cin >> race ;
if (race == 1) // Human
{
cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";
human = (100);
}
elseif (race == 2) //Orc
{
cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;
orc = (200);
}
elseif (race == 3) // Elf
{
cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";
elf = (300);
}
{
cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;
cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";
int sword (1);
int bow (2);
int magic (3);
int weapon ;
int score;
cin >> weapon ;
int random;
srand(time(NULL));// Weapon 1 & 2 random sequence
if (weapon == 1 || weapon == 2 )
{
random = rand() % 4 +1;
cout << random << endl;
if (random == 1 )
{
score = score + 100;
cout << "You hit your target! you have a final score of " << score << "." << endl;
}
if (random == 2 )
{
score = score + 200;
cout << "You hit your target! your final score is " << score << "." << endl;
}
if (random == 3 )
{
score = score + 300;
cout << "Critical hit, your final score is " << score << "." << endl;
}
if (random == 4 )
{
score = 0;
cout << "you missed your taget! you score is " << score << "." << endl;
}
}
elseif (weapon == 3)// Weapon 3 random sequence
{
random = rand () % 2 +1;
cout << random << endl;
if (random == 1)
{
score = score + 200;
cout << "You where successful, " << score << " is awarded." << endl;
}
if (random == 2)
{
score = score - 100;
cout << "You fail! " << score << " is deducted." << endl;
}
}
}
} // Would you like to play again
cout << "\nWould you like to return to the main menu (y/n)? ";
cin >> again;
}
}while ( again == 'y' || again == 'Y' );
}
if (chose == 3) // Quit
{
cout << "you quit the game.\n";
}
return 0;
}
sorry i cant tell. your formatting is bad, i see at least three different styles of coding, you have brackets lying around for no reason, tons of unused and incorrectly used variables, and too much whitespace. its a mess
// By Christopher Alfonso
//cop1334c JC
// This is a text based game
#include<ctime>
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main()
{
int rules (1);
int play (2);
int quit (3);
int chose ;
char again ;
cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";
do{
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||||||||||||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| ||||||||||| ||||||||| \n";
cout << " |||| |||| |||| |||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " |||| |||| |||| ||||||||| \n";
cout << " ////\\\\\\\\ |||||||||\\\\\\\\ |||||||| |||\\\\\\\\ |||| ////\\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ ||||||||||\\\\\\\\ |||||||| ||||\\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " //// \\\\\\\\ |||| \\\\\\\\ |||| |||| \\\\\\\\ |||| //// \\\\\\\\ "<< endl ;
cout << " |||| |||| |||| //// |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||||||||||||| ||||||||||//// |||||||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||||||||||||| ||||||||||||| |||| |||| \\\\\\\\ |||| ||||||||||||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\ |||| |||| ||||"<< endl ;
cout << " |||| |||| |||| |||| |||||||| |||| \\\\\\\\|||| |||| ||||"<< endl ;
cout << "\n1:Rules\n2:Play\n3:Quit\n";
cin >> chose ;
{
if (chose == 1) // Rules
{
cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";
}
if (chose == 3) // Quit
break;
elseif (chose == 2) // Race
{
int race;
int human (1);
int orc (2) ;
int elf (3) ;
cout << "\nYou are about to enter the arena.\nBut first" << endl;
cout << "select your race.\n";
cout << "1:Human\n2:Orc\n3:Elf\n";
cin >> race ;
if (race == 1) // Human
{
cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";
human = (100);
}
elseif (race == 2) //Orc
{
cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;
orc = (200);
}
elseif (race == 3) // Elf
{
cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";
elf = (300);
}
{
cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;
cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";
int sword (1);
int bow (2);
int magic (3);
int weapon ;
int score;
cin >> weapon ;
int random;
srand(time(NULL));// Weapon 1 & 2 random sequence
if (weapon == 1 || weapon == 2 )
{
random = rand() % 4 +1;
cout << random << endl;
if (random == 1 )
{
score = score + 100;
cout << "You hit your target! you have a final score of " << score << "." << endl;
}
if (random == 2 )
{
score = score + 200;
cout << "You hit your target! your final score is " << score << "." << endl;
}
if (random == 3 )
{
score = score + 300;
cout << "Critical hit, your final score is " << score << "." << endl;
}
if (random == 4 )
{
score = 0;
cout << "you missed your taget! you score is " << score << "." << endl;
}
}
elseif (weapon == 3)// Weapon 3 random sequence
{
random = rand () % 2 +1;
cout << random << endl;
if (random == 1)
{
score = score + 200;
cout << "You where successful, " << score << " is awarded." << endl;
}
if (random == 2)
{
score = score - 100;
cout << "You fail! " << score << " is deducted." << endl;
}
}
}
} // Would you like to play again
cout << "\nWould you like to return to the main menu (y/n)? ";
cin >> again;
}
}while ( again == 'y' || again == 'Y' );
cout << "\nyou quit the game.\n";
return 0;
}