Sign in to follow this  

My very fist Function I made in c++.

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
using namespace std;


int UpgradeShop ( Hp , Defense , Agility , Intellegence , Dexerity , WillPower , Luck ){

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;
coout << " Choice.\n";
cin >> choice;

      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 if Hp.\n";
	cout << " Do you want to do this.\n";
	int choice;
	cout << " 1 - Yes.\n";
	cout << " 2 - No.\n";
	cin >> choice;

	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 
			cout << " Please come again.\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";
	int choice2;
	cout << " Choice.\n";
	cin >> " choice2;
       
		if ( choice2 == 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 
				cout << " Please come back when you have the Money.\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";
	int choice3;
	cout << " choice .\n";
	cin >> choice3;
    
		if ( choice == 3 )
		{
			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
					cout << " Please come back when you have the money!\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";
	int choice4;
	cout << " Choice.\n";
	cin >> choice4;

		if ( choice4 == 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
				cout << " Please come back when u have the money.\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";
	int choice5;
	cout << " Choice.\n";
	cin >> choice5;

		if ( choice5 == 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 
				cout << " Please come back when u have the money.\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";
	int choice6;
	cout << " Choice.\n";
	cin >> choice6;
		
		if ( choice6 == 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
				cout << " Please come back When you have the money.\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";
	int choice7;
	cout << " Choice.\n";
	cin >> choice7;
		
		if ( choice7 == 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 
				cout << " Please come back when you have the money.\n";
			break;
}
}
	return 0;
}



[Edited by - RebornCrosis on September 5, 2005 6:59:58 PM]

Share this post


Link to post
Share on other sites
Cool.

One piece of advice though, it would be much better to break that down into multiple functions. Another thing is to never do "using namespace std;". There is no need to bring every member of std:: into this scope. Just bring the things you need.

ace

Share this post


Link to post
Share on other sites
So when you say not use to use everthe hole std u mean like doing something like this?
using std::cout;
using std::cin;
using std::endl;

like that? Can i ask if i use the using namespace std; does it make the programe are fucntion run slower if it does not need to inlcude the hole std?

Share this post


Link to post
Share on other sites
nearly everyone uses using namespace std; In most cases there is no reason not to.

Arguments against it: you might have your own list type and don't want to conflict with std::list.

Why that is a bad argument: The standard library is such a central part of C++ that you simply should not use the same identifiers that it does. It will make your code confusing. I am not forced to type std::int, and so I should not be forced to type std::string. By using std you allow yourself to use strings and such in the same way that you use ints, which is one of the main strengths of C++.

Share this post


Link to post
Share on other sites
No it doesnt affect the speed or size of the program... Infact I've rarely seen it suggested even to bring in part of a namespace. Seems like a good way to waste time always adding more using's to the top of your code. I'd take a broad approach and just use all of std. Unless a naming conflict occurs. Then you could consider limiting what you import, however, a better solution would be to avoid the naming conflict all together.

For example, don't name your 3d vector class "vector" as this is the same as std::vector which is used for dynamically size arrays. Perhaps you could name yours Vector, or Vector3 instead. It's probably not a good idea to name your classes the same as things in the std namespace, even though you can, since it will just create problems and waste your time messing with using statements and naming conflicts.

Share this post


Link to post
Share on other sites
first of all, it's "whole std" not "hole std". to answer your question, not noticably. there's nothing wrong with 'using namespace std;' and i suggest you just keep using that. it works for your purposes, so just do it.

Share this post


Link to post
Share on other sites
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> =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Isn't that a bit of plagurism posting someone else code without citing a reference agi_shi come on it is after all RebornCrosis's FIRST Function give some credit.

Share this post


Link to post
Share on other sites
In the main function, call the variables things like Hp as well isntead of changing them to the alphabet.

Also you need to break down your upgradeshop function into several smaller functions. (Basicaly look where code is repeated alot and make that into a function)

The stats like hp, defence and stuff are all to do with your character I assume, if so it would be a good idea to group them all in a structure if only to keep it neat and more together.

(This one might be a bit over the top) I kind of think it would be better to have the shop generated at the start and to have generic functions that make the shop work however many items there are for sale etc. This would meen learning about malloc, pointers and linked lists though but if you did it, it would be one step from being able to load a shop from a file.

Share this post


Link to post
Share on other sites
Oh I'm sorry I didnt me it like THAT! The abc letters are just for the main function so the program compiles. RebornCrosis, I'm sorry, and you use the function ay way you want! Let me explain: the case's(since you have more than one statement in the cases you have to put curly braces around them({})), the outputting(you had some errors in outputting stuff like putting /n instead of \n and so), and last, I (put elses so if the user enters something else he gets an answer)... I'm really sorry again and I DIDNT mean it like that.

EDIT: All the credit goes to RebornCrosis I just wanted to let the function run smoothly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Isn't that a bit of plagurism posting someone else code without citing a reference agi_shi come on it is after all RebornCrosis's FIRST Function give some credit.


I don't really see how (within the context of this thread) agi_shi claimed or even implied to have written the original code. It just seems kind of silly to bring up that point, especially when it's excedingly obvious that agi_shi was making a good faith act to help the OP out...

Share this post


Link to post
Share on other sites
well thank you agi_shi for shwoing me how to fix the code ok can ia sk some question in the book I am reading the show making the function but they did not show the Hp& I know that is the logical opertaor and, I thought all you had to do was use a coma sperated list in the parameter list. this is how i thouhgt a function should look like.
return-vaule-type function-name ( parameter-list)
{
declarations and statments.
}
and also why did you put the code in. Does A take the Hp name? Like does a basicly stand for Hp?
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);

Share this post


Link to post
Share on other sites

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this