/*Monster prototype: Coby ^_^ ver, 1.00 5/11/2007
ver, 1.01 5/12/2007 Added death,fixed life function
ver, 1.02 5/16/2007 Added random event to feed and walk */
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
int monster(int a,int b);
int main(){
string mname;
int health =100;
int hunger =100;
int life;
int choice;
int quit = 0;
int randn;
cout<<"Hello! Welcome to monster!\n";
cout<<"What will your monsters name be? \n";
cin>>mname;
cout<<mname<<" huh? sounds great!\n";
do{
life=monster(hunger,health);
cout<<"\nWhat would you like to do with "<< mname <<" ?\n1-Feed \n2-Walk\n3-Veiw health\n";
cin>>choice;
switch(choice){
case 1:
cout<<"You fed "<<mname<<"!\n";
hunger +=10;
health -=9;
srand(time(NULL));
randn=rand()%40+1;
if(randn==10){
cout<<"\nYou Got Food Poisoning!\n";
hunger -=50;
life-=20;
cout<<"Hunger: "<<hunger<<"\nLife: "<<life<<"\n";
}
cout<<"Your Hunger is: "<<hunger<<"\n\n";
break;
case 2:
cout<<"You took "<<mname<<" for a walk!\n";
health+=10;
hunger-=9;
srand(time(NULL));
randn=rand()%40+1;
if(randn == 10){
health-=50;
life-=20;
cout<<"You Broke Your Leg!/nFitness: "<<health<<"\nLife: "<<life<<"\n";}
cout<<"Your Fittness is: "<<health<<"\n\n";
break;
case 3:
cout<<"Your current health is: "<<life<<"\nfittness "<<health<<"\nHunger"<<hunger<<"\n";
break;
default:
cout<<"Please enter 1,2, or 3!";
}}while(quit==0 && life>0 && hunger>-129 && health>-129);
if(life<0 ||hunger<-130||health<-130){
cout<<mname<<" has died!\n";
}
end:
system("pause");
}
int monster(int a,int b)
{
int life;
life = (a+b)/2;
return life;
}
First game ^_^
ok I've been learning C++ for about a week now and decided to see if I had really learned any thing, so I made this.
Please, all suggestions are welcome. I plan on adding more to it as I learn and I just want to make sure this is a good way to start ^_^
I do have some questions about some things I'll ask a bit later so thanks!
Source:
[Edited by - link161 on May 17, 2007 2:34:00 AM]
I'm way too tired right now to actually read it and give proper advice, but one thing that caught my eye while scrolling: You only need to call srand once in your application, probably right at the start. It just sets a seed (time in this case) that ensures rand doesn't return the same sequence everytime you run your app.
Seems like a nice game for a beginner :)
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended as it tends to eat all available CPU. Instead I'd advice to use cin.get(); It's basically the same thing, only that you won't get that terminate text.
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended as it tends to eat all available CPU. Instead I'd advice to use cin.get(); It's basically the same thing, only that you won't get that terminate text.
There was one thing that I noticed. You have in your variable declarations and also in your while loop condition "quit", but nowhere else in the program is there anything that modifies the value of the variable. I think this just simply got overlooked, but is important none the less if someone wants to stop playing. :D :D Maybe make it option/case number zero or something so that when choice equals 0 it sets quit to 1 etc...
Anyhow good luck with your future games/programs. Just keep on going and one day you can be making monsters attack, run, make scary faces, beg for cookies, and all sorts of things :) Too bad you can't make them program for you the game of your dreams though :| Well not without it being way more trouble than it's worth anyways, haha. Good night all.
Anyhow good luck with your future games/programs. Just keep on going and one day you can be making monsters attack, run, make scary faces, beg for cookies, and all sorts of things :) Too bad you can't make them program for you the game of your dreams though :| Well not without it being way more trouble than it's worth anyways, haha. Good night all.
Source:
/*Monster prototype: Coby ^_^ ver, 1.00 5/11/2007 ver, 1.01 5/12/2007 Added death,fixed life function ver, 1.02 5/16/2007 Added random event to feed and walk */ #include<iostream>#include<string>#include<ctime>using namespace std; int monster(int a,int b);int main(){ string mname; int health =100; int hunger =100; int life; int choice; int quit = 0; int randn; cout<<"Hello! Welcome to monster!\n"; cout<<"What will your monsters name be? \n"; cin>>mname; cout<<mname<<" huh? sounds great!\n"; do{ life=monster(hunger,health); cout<<"\nWhat would you like to do with "<< mname <<" ?\n1-Feed \n2-Walk\n3-Veiw health\n"; cin>>choice; switch(choice){ case 1: cout<<"You fed "<<mname<<"!\n"; hunger +=10; health -=9; srand(time(NULL)); randn=rand()%40+1; if(randn==10){ cout<<"\nYou Got Food Poisoning!\n"; hunger -=50; life-=20; cout<<"Hunger: "<<hunger<<"\nLife: "<<life<<"\n"; } cout<<"Your Hunger is: "<<hunger<<"\n\n"; break; case 2: cout<<"You took "<<mname<<" for a walk!\n"; health+=10; hunger-=9; srand(time(NULL)); randn=rand()%40+1; if(randn == 10){ health-=50; life-=20; cout<<"You Broke Your Leg!/nFitness: "<<health<<"\nLife: "<<life<<"\n";} cout<<"Your Fittness is: "<<health<<"\n\n"; break; case 3: cout<<"Your current health is: "<<life<<"\nfittness "<<health<<"\nHunger"<<hunger<<"\n"; break; default: cout<<"Please enter 1,2, or 3!"; }}while(quit==0 && life>0 && hunger>-129 && health>-129); if(life<0 ||hunger<-130||health<-130){ cout<<mname<<" has died!\n"; } end: system("pause"); }int monster(int a,int b){ int life; life = (a+b)/2; return life;}
Quote:Original post by HomerSp
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended
Correct.
Quote:as it tends to eat all available CPU.
Eh? No, it doesn't. It's not recommended because:
1. It's not standard (not all computers have a program called 'pause') EDIT: Clarification -- yes, system() is standard but 'pause' is not
2. It's not safe -- the program 'pause' might well be replaced with one elsewhere in the $PATH directory which eats the user's children
3. It's ugly -- programs like this are meant to be run from a command shell, so it goes against the principle of least surprise; it's better just to learn to use a command shell (or a batch script, if nothing else
[Edited by - TheUnbeliever on May 16, 2007 12:59:50 PM]
Original post by TheUnbelieverQuote:Original post by HomerSp
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended
Correct.Quote:as it tends to eat all available CPU.
Eh? No, it doesn't. It's not recommended because:
1. It's not standard (not all computers have a program called 'pause')/quote]
I take issue with that only because that function is from the stdlib.h library, so if that is loaded then the PAUSE functionality is also there. N'est-ce pas?
system() is pure, 100% grade-A standard C.
However, everything you put in the quotes is completely platform specific. system() basically passes the string to the underlying shell; if you compile and run on Linux, for example, the program will not pause at the end, instead you'll likely get a message to the effect of "pause: Command not found."
Basically, while system() itself must exist and support a certain well-defined behavior (determine if a command processor exists if passed a null pointer), it can do pretty much anything it wants (including behavior inconsistently, in an undefined manner, or outright terminate) when passed a non-null pointer. It's a bad idea to use it in general, and it's just a hack to use it to "keep the window open" as the OP is probably doing here (use the appropriate command from the IDE to hold the window open, it's usually "Start without Debugging").
However, everything you put in the quotes is completely platform specific. system() basically passes the string to the underlying shell; if you compile and run on Linux, for example, the program will not pause at the end, instead you'll likely get a message to the effect of "pause: Command not found."
Basically, while system() itself must exist and support a certain well-defined behavior (determine if a command processor exists if passed a null pointer), it can do pretty much anything it wants (including behavior inconsistently, in an undefined manner, or outright terminate) when passed a non-null pointer. It's a bad idea to use it in general, and it's just a hack to use it to "keep the window open" as the OP is probably doing here (use the appropriate command from the IDE to hold the window open, it's usually "Start without Debugging").
Quote:Original post by jpetrie
system() is pure, 100% grade-A standard C.
However, everything you put in the quotes is completely platform specific. system() basically passes the string to the underlying shell; if you compile and run on Linux, for example, the program will not pause at the end, instead you'll get a message to the effect of "pause: Command not found."
@Alpha_ProgDes -- That was what I meant. (Also, I've gone back to fix my quote tags)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement