Skip to main content
GameDev.net gamedev.net
🔒 Locked

My first program..

Started by Zero-51 Oct 30, 2004 at 11:19 PM 8 replies 1.4k views
Original Post
Zero-51
Zero-51
Well, uhh.. This is the first thing I've actually worked on, so I just wanted some input on it. This is a simple unit upgrade situation for a game I am working on. Instead of having a random integer for the resources it would be from the experience the unit has earned. #include #include #include using namespace std; int main() { srand(time(0)); int aliensKilled = rand() % 20 + 1; int multi = rand() % 5 +1; int score = aliensKilled * multi; //Determines if they have enough resources to upgrade.. cout << "Kills: " << aliensKilled << endl; cout << "Resources: " << score << endl; char choice; int resources = score; enum ship {FIGHTER = 0, BOMBER = 50, CRUISER = 100, DESTROYER = 200}; ship myShip = BOMBER; cout << "\nDo you want to upgrade your Bomber to a Cruiser for " << (CRUISER - myShip) << " resource points?"; cin >> choice; int newResource; switch (choice) //Choosing yes or no.. { case 'y': //If the choice is yes.. if (resources >= (CRUISER - myShip)) { newResource = ((score) - (CRUISER - myShip)); cout << "\n\nYou have upgraded to a Cruiser. \nYou now have " << newResource << " resource points."; cout << "\n\You now have a Cruiser."; cout << "\n\n\nPress the enter key to exit."; cin.ignore(cin.rdbuf()->in_avail() + 1); cin.ignore(cin.rdbuf()->in_avail() + 1); } else { cout << "\n\nYou do not have enough resources."; cout << "\nYou are left with a Bomber."; cout << "\n\n\nPress the enter key to exit."; cin.ignore(cin.rdbuf()->in_avail() + 1); cin.ignore(cin.rdbuf()->in_avail() + 1); } break; case 'n': //If choice is no.. cout << "\nYou choose not to upgrade at this time."; cout << "\nYou are left with a Bomber."; cout << "\n\n\nPress the enter key to exit."; cin.ignore(cin.rdbuf()->in_avail() + 1); cin.ignore(cin.rdbuf()->in_avail() + 1); break; } return 0; }
iMalc
iMalc
Looks alright to me. Nothing worth picking at.
You might want to learn loop constructs next as most useful programs have at least one loop in them.
Ekim_Gram
Ekim_Gram
It looks good, it makes me happy to see that you used switch/case statements.
Zero-51
Zero-51
thanks
Zero-51
Zero-51
updated..



/*This is just a upgrading routine that I could use for a game that needs
the person to upgrade their ship or a unit. Could possibly be used with
an If statement to determine if the unit's experience is high enough to
be ready for an upgrade./././*/

#include
#include
#include
using namespace std;

int main()
{
srand(time(0));

int aliensKilled = rand() % 20 + 1;
int multi = rand() % 5 +1;
int score = aliensKilled * multi; //Determines if they have enough resources to upgrade..
cout << "Kills: " << aliensKilled << endl;
cout << "Resources: " << score << endl;
char choice;
int resources = score;


enum ship {FIGHTER = 25, BOMBER = 50, CRUISER = 100, DESTROYER = 200};
ship myShip = BOMBER;
question:
if (resources < (CRUISER - myShip))
{
cout << "\n\nYou do not have enough resources.";
cout << "\n\n\nPress the enter key to exit.";
cin.ignore(cin.rdbuf()->in_avail() + 1);
}
else
{
cout << "\nDo you want to upgrade your Bomber to a Cruiser for "
<< (CRUISER - myShip) << " resource points?";

cin >> choice;

int newResource;
switch (choice) //Choosing yes or no..
{
case 'y': //If the choice is yes..
if (resources >= (CRUISER - myShip))
{
newResource = ((score) - (CRUISER - myShip));
cout << "\n\nYou have upgraded to a Cruiser. \nYou now have " <<
newResource << " resource points.";
cout << "\n\You now have a Cruiser.";
cout << "\n\n\nPress the enter key to exit.";
cin.ignore(cin.rdbuf()->in_avail() + 1);
cin.ignore(cin.rdbuf()->in_avail() + 1);
}
break;

case 'n': //If choice is no..
cout << "\nYou choose not to upgrade at this time.";
cout << "\nYou are left with a Bomber.";
cout << "\n\n\nPress the enter key to exit.";
cin.ignore(cin.rdbuf()->in_avail() + 1);
cin.ignore(cin.rdbuf()->in_avail() + 1);
break;

default:
cout << "\n\nYou must choose yes or no. (y/n)";
goto question;
}
}

return 0;
}
baddogj
baddogj
not bad.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/
nprz
nprz
For future programming's sake, I would suggest not using the goto statement... so changing it and making it a do..while loop might be better.

/*This is just a upgrading routine that I could use for a game that needs the person to upgrade their ship or a unit. Could possibly be used with an If statement to determine if the unit's experience is high enough to be ready for an upgrade./././*/#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){	srand(time(0)); 		int aliensKilled = rand() % 20 + 1;	int multi = rand() % 5 +1;	int score = aliensKilled * multi; //Determines if they have enough resources to upgrade..	cout << "Kills: " << aliensKilled << endl;	cout << "Resources: " << score << endl;	char choice;	int resources = score;			enum ship {FIGHTER = 25, BOMBER = 50, CRUISER = 100, DESTROYER = 200};	ship myShip = BOMBER;		do	{				if (resources < (CRUISER - myShip))		{			cout << "\n\nYou do not have enough resources.";			cout << "\n\n\nPress the enter key to exit.";			cin.ignore(cin.rdbuf()->in_avail() + 1);		} 		else		{			cout << "\nDo you want to upgrade your Bomber to a Cruiser for " 				<< (CRUISER - myShip) << " resource points?";						cin >> choice;						int newResource;			switch (choice) //Choosing yes or no..			{			case 'y': //If the choice is yes..				if (resources >= (CRUISER - myShip))				{					newResource = ((score) - (CRUISER - myShip));					cout << "\n\nYou have upgraded to a Cruiser. \nYou now have " <<						newResource << " resource points.";					cout << "\n\You now have a Cruiser.";					cout << "\n\n\nPress the enter key to exit.";					cin.ignore(cin.rdbuf()->in_avail() + 1);					cin.ignore(cin.rdbuf()->in_avail() + 1);				}				break;							case 'n': //If choice is no..				cout << "\nYou choose not to upgrade at this time.";				cout << "\nYou are left with a Bomber.";				cout << "\n\n\nPress the enter key to exit.";				cin.ignore(cin.rdbuf()->in_avail() + 1);				cin.ignore(cin.rdbuf()->in_avail() + 1);				break;							default:				cout << "\n\nYou must choose yes or no. (y/n)";			}		}	}while(choice != 'y' && choice != 'n');		return 0;}


or something like that.
Zero-51
Zero-51
that makes sense.

I'm having a problem with a different thing.
I need to know how to make the program pauses until the user presses enter, and continues after they do.
Toolmaker
Toolmaker
#include <stdlib.h>system("pause"); // Press any key to continue


Toolmaker
 
Zero-51
Zero-51
thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.