Monday, May 24, 2010

I need help with functions!!! c++ please!?

#include %26lt;iostream%26gt;





using namespace std;


int stores(int);





int main()


{


stores(int);





system("pause");


return 0;


}





void stores(int)


{


int store_choice;


cout %26lt;%26lt; "Welcom to my shop! What would you like to buy?" %26lt;%26lt; endl;


cout %26lt;%26lt; "1. weapons... 2. shields... 3. guns... 4. boats..." %26lt;%26lt; endl;


cin %26gt;%26gt; store_choice;

















}








it doesnt work for some reason plz help

I need help with functions!!! c++ please!?
Your "stores" function is prototyped to return "int", but it is defined to return "void". You should fix that; I think the second one is wrong. If you do that, then you need to add a return statement to your "stores" function.





Also, when you call "stores" in the main program, you are passing the value "int". That is not correct either. It should be an integer constant or the name of an integer variable or some other expression that yields an int. The word "int" is not what you want.





In fact, I can't see what you need that int for anyway. Apparently neither do you since you did not give your int parameter a name when you defined the function.





I suggest that you change the prototype to something like this:


-------------------


int stores(void);


-------------------





Change the actual call in the main program to something like this:


-------------------


cout %26lt;%26lt; "You picked " %26lt;%26lt; stores() %26lt;%26lt; endl;


-------------------





And change the actual function definition to something like this:


-------------------


int stores(void)


{


int store_choice;


cout %26lt;%26lt; "Welcom to my shop! What would you like to buy?" %26lt;%26lt; endl;


cout %26lt;%26lt; "1. weapons... 2. shields... 3. guns... 4. boats..." %26lt;%26lt; endl;


cin %26gt;%26gt; store_choice;


return store_choice;


}


-------------------





I think you will be much happier with the result.
Reply:#include %26lt;iostream%26gt;





using namespace std;


int stores(int);





int main(int) %26lt;------Heres what i added different


{


stores(int);





system("pause");


return 0;


}





void stores(int)


{


int store_choice;


cout %26lt;%26lt; "Welcom to my shop! What would you like to buy?" %26lt;%26lt; endl;


cout %26lt;%26lt; "1. weapons... 2. shields... 3. guns... 4. boats..." %26lt;%26lt; endl;


cin %26gt;%26gt; store_choice;

















}
Reply:I'm weak in functions so this will help but not all the way.





Shouldn't this be at the bottom?





System pause is so the screen won't close on you but there should be a return 0 at the very bottom. Is there a return 0 necessary within the function too?





system("pause");


return 0;


No comments:

Post a Comment