My very fist Function I made in c++.

Started by
33 comments, last by smr 18 years, 7 months ago
Guys, I don't think the whole "using namespace std" thing matters for him at this point =) Just keep practicing, and as you program more and more, you'll figure out new things and new ways to design. I commend you for being willing to try.

My suggestion for now is that you make a generalised buying function that allows you to avoid copying and pasting like you are. Make one function, BuyItems or something, that allows them to select an item to buy. Then make another function, DisplayPrice, that displays the price of the item and allows them to choose whether they want to pay for it.

Of course, DisplayPrice will need to know what item the player is buying, how much it costs, and how much money the player has. As you work on more programs, you'll figure out better and better ways to store and retrieve that kind of information.

For now, good work. I hope it forms into a cohesive whole. And you spell it "Interested", not "Intrested" </grammar_nazi> =)
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
Well i will still stand up for what i said about std. I was simply taught it at uni. It was basically something that was considered good practice, explicit programming.

We all code differently however.

ace
Aye, I agree there.. I tend to go off on rants sometimes... the std vs part of std thing is largely irrelevant. The important thing is of course what silverphyre673 said about making a generalized buying function and eliminate repeated code.
Seems pretty good for a beginning, however, you should have an open bracket after the function instead of semi-colong, because otherwise, you're declaring a prototype.
Another thing: Research arrays, and later, function pointers. Those could make your code much shorter.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
It's always good to get a good beginners book, too, because learning by yourself at the beginning goes much more slowly compared to when you are more experienced. Getting a good reference manual/learning book helps a lot to alleviate that problem. I used Sams "Learn C++ in 21 days" from Addison-Wesley. It helped a lot, and taught me many of the important concepts that I use in design today, as well as, of course, the syntax and semantics of the language.

A little money can make things go a long way. And when you start wanting to find out more info from the internet, make sure to check out GDNet's articles, if you haven't already. It is an absolutely amazing resource, for the most part.
my siteGenius is 1% inspiration and 99% perspiration
Thank all of you for you input it is much appericated. :)
Umm when you say bracket do you mean [ ? that are a { I know that one is a cruley bracer. The book I have that I am learing from is Learn how to programe in c++ 4 edt. I think it is a great book.
Quote:Original post by RebornCrosis
Umm when you say bracket do you mean [ ? that are a { I know that one is a cruley bracer. The book I have that I am learing from is Learn how to programe in c++ 4 edt. I think it is a great book.


He means {.

void FunctionDeclaration();void FunctionDefinition(){	//something}
Quote:
int UpgradeShop ( Hp , Defense , Agility , Intellegence , Dexerity , WillPower , Luck )


Does this really compile? Shouldn't it be more like
int UpgradeShop (int &Hp, int &Defense, int &Agility, int &Intellegence, int &Dexerity, int &WillPower, int &Luck)

[source language="CPP"]#include <iostream>using namespace std;void UpgradeShop (int &Hp , int &Defense , int &Agility , int &Intellegence , int &Dexerity , int &WillPower , int &Luck, int &Gold){    cout << "Hello and welcome to my Shop.\n\n";    cout << "Is there anything That I can Intrest you in.\n\n";    cout << "1 - Hp Upgrade.\n";    cout << "2 - Defense UpGrade.\n";    cout << "3 - Agility.\n";    cout << "4 - Intellegence.\n";    cout << "5 - Dexerity.\n";    cout << "6 - WillPower.\n";    cout << "7 - Luck.\n";        int choice;    cout<<"Your choice: ";    cin >> choice;    cin.ignore();        switch (choice)     {        case 1:        {        	cout << "Aye I see that you'r intrested in the Hpgrade.\n";        	cout << "It will cost 400 gold for 1 point of Hp.\n";        	cout << "Do you want to do this?\n";        	int choice;        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout<<"Your choice: ";            cin >> choice;            cin.ignore();                	if (choice == 1)        	{        		cout << "Ah thank you for the money.\n\n";        		Gold -= 400;        		cout << "Your Gold left after paying the man is " << Gold << endl;        		Hp += 1;        		cout << "Your Hp after the Upgrade is " << Hp << endl;        	}       		else if (choice == 2)      			cout << "Please come again.\n";     			 			else 			    cout<<"Error: Answer not listed.\n";        }  		break;        case 2:        {        	cout << "Aye I see that you'r intrested in the Defense Upgrade.\n";        	cout << "That will be 600 gold, for 1 point of Defense.\n";        	cout << "Do you want to do this.\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout<<"Your choice: ";        	cin >>choice;        	cin.ignore();                       		if ( choice == 1 )        		{                   cout << "Ah thank you for the money!\n";        		   Gold -= 600;        		   cout << "Your Gold Left after paying the man is " << Gold << endl;        		   Defense += 1;        		   cout << "Your Defense after the Upgrade is " << Defense << endl;        		}       			else if (choice == 2)      				cout << "Please come back when you have the Money.\n";				else                    cout<<"Error: Answer not found.\n";        }  		break;        case 3:        {        	cout << "Aye I see that you'r intrested in the Agility Upgrade.\n";        	cout << "That will be 200 for, 1 point of Agility.\n";        	cout << "Do you want to do this?\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout<<"Your choice: ";        	cin >> choice;        	cin.ignore();                    		if ( choice == 1 )        		{        			cout << "Ah Thank you for the Money!\n";        			Gold -= 200;        			cout << "Your gold after paying the man is " << Gold << endl;        			Agility += 1;        			cout << "You'r Agility after the upgrade is"<< Agility << endl;        		}       			else if (choice == 2)   					cout << " Please come back when you have the money!\n";				else				    cout<<"Error: Choice not listed!\n";        }        break;        case 4:        {        	cout << "Aye I see that you'r interested in the Intellegence Upgrade.\n";        	cout << "That will be 700 for 1 point of Intellegence.\n";        	cout << "Do you want to do this?\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout << "Your choice: ";        	cin>>choice;        	cin.ignore();              		    if ( choice == 1 )        		{        			cout << "Aye Thank you for the Money.\n";        			Gold -= 700;        			cout << "Your gold after paying the man is " << Gold << endl;        			Intellegence += 1;        			cout << "You Intellegence after the upgrade is " << Intellegence << endl;        		}       			else if (choice == 2)      				cout << "Please come back when u have the money.\n";				else				    cout<<"Uhhh sooo many errors.  Write them yourself.\n";        }        break;        case 5:        {        	cout << "Aye I see that you are intrested in the Dexerity upgrade.\n";        	cout << "It will cost you 600 gold, for 1 point of Dexerity.\n";        	cout << "Do you wish to do this?\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout << "Your choice: ";        	cin.ignore();                		if ( choice == 1 )        		{        			cout << "Aye Thank you for the money.\n";        			Gold -= 600;        			cout << "Your gold after paying the man is " << Gold << endl;        			Dexerity += 1;        			cout << "You Decerity after the Upgrade is " << Dexerity << endl;        		}       			else if (choice == 2)      				cout << "Please come back when u have the money.\n";				else				    cout<<"Error again\n";        }        break;        case 6:        {        	cout << "Aye I see that you are intrested in the willpower Upgrade.\n";        	cout << "It will cost 400 for, 1 point of willpower\n";        	cout << "Do you want to do this?\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout << "Your choice: ";        	cin >> choice;        	cin.ignore();        		        		if ( choice == 1 )        		{        			cout << "Aye Thank you for the Money.\n";        			Gold -= 400;        			cout << "Your gold after paying the man is " << Gold << endl;        			WillPower += 1;        			cout << "Your WillPower after the upgrade is " << WillPower << endl;        		}       			else if (choice == 2)				    cout << "Please come back When you have the money.\n";                else                    cout<<"uhhh\n";        				        }			        break;        case 7:        {        	cout << "Aye I see that you are intrested in the Luck upgrade.\n";        	cout << "It will cost you 100 gold, for 1 point of Luck.\n";        	cout << "Do you want to do this?\n";        	cout << "1 - Yes.\n";        	cout << "2 - No.\n";        	cout << "Your choice: ";        	cin >> choice;        		        		if ( choice == 1 )        		{        			cout << "Aye Thank you for the Money.\n";        			Gold -= 100;        			cout << "Your gold after paying the man is " << Gold << endl;        			Luck += 1;        			cout << "Your Luck after upgrade is " << Luck << endl;                }       			else if (choice == 2)      				cout << "Please come back when you have the money.\n";				else				    cout<<"";        }        break;                default:                cout<<"Huh?\n";    }	return;}int main(){    int a = 1000, b = 1000, c = 1000, d = 1000, e = 1000, f = 1000, g = 1000, h = 1000;        UpgradeShop(a, b, c, d, e, f, g, h);        cin.get();    return 0;}

Fixed it.

This topic is closed to new replies.

Advertisement