Question:
Help with c++ program please!?
justsilly
2010-04-22 15:07:24 UTC
My compiler keeps telling me that I'm doing something wrong, I really don't know what it is. All help is welcome.

#include
#include

using namespace std;
//Function, input # of apples, output price
int Apple_Price(int num_apples)
{
int apple_price;
apple_price = num_apples * 2;
return apple_price;
}
int main()//Main line of code for apples, input and output here
{
int num_apples;
cout << "Insert the number of apples you want to buy." << endl;
cin >> num_apples >> endl;
cout << Apple_Price(int num_apples) << endl;
}
Six answers:
ʃοχειλ
2010-04-22 15:15:23 UTC
// Corrected code:



#include

#include



using namespace std;

//Function, input # of apples, output price

int Apple_Price(int num_apples)

{

int apple_price;

apple_price = num_apples * 2;

return apple_price;

}



int main() //Main line of code for apples, input and output here

{

int num_apples;

cout << "Insert the number of apples you want to buy." << endl;

cin >> num_apples;

cout << Apple_Price(num_apples) << endl;



return 0;

}
?
2010-04-22 15:32:37 UTC
Hah, don't listen to the people who tell you you need to return something from int main(), that's not true (see ISO 14882:2003 paragraph 3.6.1/5 if you don't believe me, that's one of the several ways in which main() is different from all other functions).



The only two actual errors were the cin >> endl, and the call to "Apple_Price(int num_apples)" and you got them.



Plus there are two examples of bad style: the use of C library's math.h, which is very different from C++'s cmath (although removing that line works too, you're not using anything from cmath) and declaration of variable apple_price only to be overwritten in the next line (should be int apple_price = num_apples * 2; ). Even though for an integer variable it's not a big deal, In C++ we don't instantiate and overwrite variables of class type unnecessarily, and it makes sense to treat integer variables with the same respect.
jplatt39
2010-04-22 15:16:50 UTC
To be an int main() it has to return something, and if you are using Unix or Linux and don't return 0 you are being very mean to your OS so stop it and feed your OS a zero.



Second, are you trying to REDECLARE num_apples in line seventeen?



cout << Apple_Price(int num_apples) << endl;

No. Make it:

cout << Apple_Price(num_apples) << endl;



Finally above that lose the endl and the arrows to it in the line above. cin doesn't do endl; And you shouldn't be feeding anything to it anyhow.
Frecklefoot
2010-04-22 15:13:05 UTC
This is your problem:



cout << Apple_Price(int num_apples) << endl;



You've already declared num_apples as an int. You just need to do this:



cout << Apple_Price(num_apples) << endl;



You may also want to change this:



cin >> num_apples >> endl;



To this:



cin >> num_apples; // don't need the endl



Next time include the error(s) your compiler gives you. Good luck! HTH
anonymous
2010-04-22 15:11:06 UTC
From what I can see there.. you're not returning anything from main although you've declared it to return an int ..



Also, don't re-declare num_apples in that Apple_Price call..



What compilation errors are you getting?
Nick M
2010-04-22 15:11:19 UTC
Use "#include " not "#include ", and do that any time you want to use C libraries in C++.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...