Question:
I need help on this C++ problem?
Peachy Keen
2012-10-17 07:57:00 UTC
so the question asks to write a program that inputs prices for three book and outputs the highest price. basically i have to write a program to compare the prices. my prof gave us a skeleton of the program and i basically had to fill in he function definition for the function declaration. however, my program doesnt work. i know something is wrong but i don't know what.
thanks!


#include
using namespace std;

double comp (double bk1_par, double bk2_par, double bk3_par);

int main()
{
double bk1, bk2, bk3, max;

cout << "Please input the price for the first book:";
cin >> bk1;
cout << "Please input the price for the second book:";
cin >> bk2;
cout << "Please input the price for the third book:";
cin >> bk3;

max = comp(bk1, bk2, bk3);

cout << "The highest price is " << max << endl;

return 0;
}

double comp( double bk1_par, double bk2_par, double bk3_par)
{
double bk1_par, bk2_par, bk3_par;

if ((bk1_par > bk2_par) && (bk1_par > bk3_par))
return bk1_par;
else if ((bk2_par > bk1_par) && (bk2_par > bk3_par))
return bk2_par;
else if ((bk3_par > bk1_par) && (bk3_par > bk2_par))
return bk3_par;
}
Three answers:
δοτζο
2012-10-17 08:09:37 UTC
It's been a while since I've done C++, but I believe the problem lies here:

double comp( double bk1_par, double bk2_par, double bk3_par)

{

double bk1_par, bk2_par, bk3_par;



You're redeclaring the arguments of the function inside the function, effectively wiping their memory. Inside a function the arguments are already defined (they're really just dummy names anyway), so just delete that line of declarations and the program should world perfectly.





Edit: Tested it... yep it works if that line disappears.
scott edwards
2012-10-17 15:08:58 UTC
try removing the line in your function definition :



double bk1_par, bk2_par, bk3_par;



the second declaration might be clearing the values passed to your function
Moon
2012-10-17 15:04:51 UTC
Better place for your question is :::



Home > All Categories > Computers & Internet > Programming & Design



...........


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