Hi Akash.
I don't like do....while loops much and i don't recommend it to beginners most miss the fact that it will always enter the loop at least once and its not what you want(most of the time you don't), anyway who cares what i like eh?
Your loop is kinda working i tested it on Microsoft's(my linux box is being used by my girlfriend) compiler but i see some problems. I will try to point you to the right direction ...
1º: initialize your variables before you use it you are trying to cout money and it's not initialized thats bad programming practice and will lead to bad results;
2º: gotos are ok but i would avoid them, the problem is that they can really go to anywhere in the code and when your code gets bigger you will wish that you never used it and its also considered by some programmers bad programming practice. I recommend a function here;
3º: its always good to tell the user how to exit the loop every time he chooses an option(a case in the loop) so the user know how to quit, go back to previous menu, etc... it also makes the program more user friendly.
4º comments is always nice but yeah thats a lot of comments you don't have to comment every single line in your code like magical said i see many redundant comments there but i will let you find your own style just try to not overdo it. The one that he pointed out should be enough to give you an idea.
so your code should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <string.h>
#include "includer.h" // Header file with all the #includes
#include "definer.h" // Header file with all the #defines
void land_a_1(); // Shows the welcome message.
void gameOption();// Displays the commads
int main() // Starts the file
{
char udoout = ' '; // Declaration of variable "udoout" (udoout really means what you do)
int hp = 0, full = 0, money = 0; // Declaration of integers full shouldn't really be 0 ;P
//bool keepPlaying = true; // sentinel variable to exit the game loop.
land_a_1();
//while(keepPlaying) //game loop
//{
while(1) /* game loop. warning infinite loop if you use one of these always tell the user how to get out
but sice you already are its ok but try to avoid it your better off with a sentinel variable. */
{
cin >> udoout; //command input
// I thought letters would be more interesting than typing in a command. I don't know if it's true.
// I'll find out! :D
switch (udoout) //Beginning of switch statement
{
case 'a': //If 'a'
cout << "You are successfully out of your home.\n"; //Confirmation
land_a_1(); // Why won't it go to land_a_1 here!?!
break; //End of case
case 'b': //If 'b'
cout << "You slept and are now fully healed.\n"; //Confirmation
hp = full; //HP set to full
break; //End of case
case 'c': //If 'c'
cout << "Your stats are: HP: " << hp <<"\t Money:" << money << "\n" ; //Stats
break; //End of case
case 'd': //If 'd'
cout << "\n"; //I still need to make this part.
break; //End of case
case 'e': //If 'e'
cout << "Type a command.\n"; //I still need to make this part.
break; //End of case
case 'f': //If 'f'
//Town goes here
break; //End of case
case 's': //If 's'
gameOption();
break; //End of case
//case 'q':
// cout << "Bye! been fun!\n"; //;P
// keepPlaying = false; // changes the sentinel so it exits the loop
// break;
default: // this will happens everytime he uses an unknow command.
cout << "Command not found.\n\a";// If the above is not true, say "Command not found." and beep.
gameOption();
break;
} //End of switch statement
} // End of while loop.
//cout << "Command not found.\n\a"; // If the above is not true, say "Command not found." and beep.
return 0; //End
} //End of main
void land_a_1()
{
cout << "Hi!\n" // Land piece A1
<< "\n\n\n\n\n\n\n\n\n\n\n" //Beginning spaces
<< "Welcome to Unitowny!\n" //Greeting
<< "You are at your home.\n" //Setting
<< "CTRL-D to quit.\n" //Info
<< "To look around the town you are in, type 'f'.\n" //Info
<< "But your best friend will be 's' because it will show you the commands to use.\n" //Info
<< "To use the tools below, type the letter that comes\n" //Info
<< "At any time you may also use 'c' to help you.\n" //Info
<< "before the task you want to do.\n"; //Things to remember
gameOption();
}
void gameOption() //Option
{
cout << "Options:\n" //These are the things you can do at your home
<< "a: Go out of your home.\n"
//<< "b: Go to sleep.\n"
<< "c: Check on your stats.\n"
<< "d: See your home.\n"
<< "e: Type a command.\n"
<< "f: Look around your town.\n\n"
<< "CTRL-D to quit.\n"; //<< "q: To quit.\n";
}
| |
I don't know if thats the behavior you want from land_a_1 function adapt it to your needs.
hope this helps its bed time here so i am sorry if i missed anything good luck with your game!!
sorry for the long post :D
Jeff.