Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get_item_price

Reply
Thread Tools

get_item_price

 
 
tomakated
Guest
Posts: n/a
 
      11-16-2004
Hi, i'm new here and I can't get my get_item_price() function to work I
need it to take in 4 items

int main()

{

double item1, item2, item3, item4, lowest=0, total;

char response;



instructions();

item1 = get_item_price();

item2 = get_item_price();

item3 = get_item_price();

item4 = get_item_price();

total = item1 + item2 + item3 + item4;

cout << “Do you have a coupon for this purchase? \n" ;

response = getresponse();

if (response == 'y')

{ lowest = findcheapest(item1, item2, item3, item4);

total = total – lowest;

}

printresults (response, item1, item2, item3, item4, total, lowest);

}

and so far all I have is this;
#include <iostream>
#include <iomanip>

double findcheapest(double, double, double, double);
void printresults (double, double, double, double, double, double,
double);


int main()
{
double item1, item2, item3, item4, lowest=0, total;
char response;

instructions();
item1 = get_item_price();
item2 = get_item_price();
item3 = get_item_price();
item4 = get_item_price();
total = item1 + item2 + item3 + item4;
cout << "Do you have a coupon for this purchase? \n" ;
response = getresponse() ;
if (response == 'y')
{ lowest = findcheapest(item1, item2, item3,
item4);
total = total - lowest;
}
printresults (response, item1, item2, item3, item4,
total, lowest);
}


void get_item_price()
{
double i;
cout << "Enter the four item prices you want to buy.\n"
cout << "Item1: ";
cin >> i;
while (i<0)
{
cout << "Please enter a positive number: ";
cin >> i;
}

int getresponse()
{
while ((response == 'y' ) || (response == 'Y')
{
cout << "Do you have a coupon for this pruchase? (y or n)
\n";
cin >> response;
}
double findcheapest(double item1, item2, item3, item4)
{
if (((item1 <= item2) && (item1 <= item3) && (item1 <=
item4)))
{
lowest = item1;
}
if (((item2 <= item1) && (item2 <= item3) && (item2 <=
item4)))
{
lowest = item2;
}

if (((item3 <= item1) && (item3 <= item2) && (item3 <=
item4)))
{
lowest = item3;
}

if (((item4 <= item1) && (item4 <= item2) && (item4 <=
item3)))
{
lowest = item4;
}

}
void printresults(double response, double item1, double item2,
double item3, double item4, double total, double lowest);

{
cout << "Item 1 $" << setw(10) << item1 << endl;
cout << "Item 2 $" << setw(10) << item2 << endl;
cout << "Item 3 $" << setw(10) << item3 << endl;
cout << "Item 4 $" << setw(10) << item4 << endl;
cout << "Coupon Discount $(" << setw(10) <<
lowest<<")"<< endl;
cout << "----------------------------" << endl;
cout << "Total $" << setw(10) << total << endl;
}

}
"fourth.cpp" 84 lines, 2069 characters
$ g++ fourth.cpp -o fourth.out
fourth.cpp: In function `int main()':
fourth.cpp:13: implicit declaration of function `int instructions(...)'
fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
fourth.cpp:20: implicit declaration of function `int getresponse(...)'
fourth.cpp: In function `void get_item_price()':
fourth.cpp:34: parse error before `<'
fourth.cpp:43: parse error before `{'
fourth.cpp:47: `response' undeclared (first use this function)
fourth.cpp:47: (Each undeclared identifier is reported only once
fourth.cpp:47: for each function it appears in.)
fourth.cpp: At top level:
fourth.cpp:49: type specifier omitted for parameter
fourth.cpp:49: type specifier omitted for parameter
fourth.cpp:49: type specifier omitted for parameter
fourth.cpp: In function `double findcheapest(double)':
fourth.cpp:51: `item2' undeclared (first use this function)
fourth.cpp:51: `item3' undeclared (first use this function)
fourth.cpp:51: `item4' undeclared (first use this function)
fourth.cpp:53: `lowest' undeclared (first use this function)
fourth.cpp: At top level:
fourth.cpp:74: parse error before `{'
fourth.cpp:76: syntax error before `<'
fourth.cpp:77: syntax error before `<'
fourth.cpp:78: syntax error before `<'
fourth.cpp:79: syntax error before `<'
fourth.cpp:80: syntax error before `<'
fourth.cpp:81: syntax error before `<'


and I can't figure out what to do<!!!.
Can anyone help out<?.

 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      11-16-2004
In article <(E-Mail Removed) outprogramming.com>,
tomakated <(E-Mail Removed)> wrote:
>
> int main()
> {
> double item1, item2, item3, item4, lowest=0, total;
> char response;
>
> instructions();
> item1 = get_item_price();
> item2 = get_item_price();
> item3 = get_item_price();
> item4 = get_item_price();

[snip]

It appears that main() expects get_item_price() to return a double as the
function value.

However, you've actually declared get_item_price() below as 'void', that
means it doesn't return a function value. Nor does the function have a
'return' statement that actually performs the return.

> void get_item_price()


double get_item_price()

> {
> double i;


If a variable is supposed to contain a price, why not *name* it "price"?


> cout << "Enter the four item prices you want to buy.\n"
> cout << "Item1: ";


Hmmm. In main(), you call this function four times, once for each price.
Do you really want to display the message above, four times?

> cin >> i;
> while (i<0)
> {
> cout << "Please enter a positive number: ";
> cin >> i;
> }


return i;
} // you're missing a curly brace, too.

I strongly urge you to get in the habit of indenting your code and
aligning your curly braces consistently. That makes it easier to spot
missing curly braces and some kinds of logic problems. There are a few
different ways of doing this, and some programmers love to argue about
which is the best way. Just study your textbook and copy the style that
it uses. Your instructor will surely appreciate it!

There are probably other mistakes, too, but I'll let others help you find
those.

--
Jon Bell <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
 
Stephan Br?nnimann
Guest
Posts: n/a
 
      11-16-2004
"tomakated" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) alkaboutprogramming.com>...
> Hi, i'm new here and I can't get my get_item_price() function to work I
> need it to take in 4 items


[code snipped]

> "fourth.cpp" 84 lines, 2069 characters
> $ g++ fourth.cpp -o fourth.out
> fourth.cpp: In function `int main()':
> fourth.cpp:13: implicit declaration of function `int instructions(...)'
> fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
> fourth.cpp:20: implicit declaration of function `int getresponse(...)'
> fourth.cpp: In function `void get_item_price()':
> fourth.cpp:34: parse error before `<'
> fourth.cpp:43: parse error before `{'
> fourth.cpp:47: `response' undeclared (first use this function)
> fourth.cpp:47: (Each undeclared identifier is reported only once
> fourth.cpp:47: for each function it appears in.)
> fourth.cpp: At top level:
> fourth.cpp:49: type specifier omitted for parameter
> fourth.cpp:49: type specifier omitted for parameter
> fourth.cpp:49: type specifier omitted for parameter
> fourth.cpp: In function `double findcheapest(double)':
> fourth.cpp:51: `item2' undeclared (first use this function)
> fourth.cpp:51: `item3' undeclared (first use this function)
> fourth.cpp:51: `item4' undeclared (first use this function)
> fourth.cpp:53: `lowest' undeclared (first use this function)
> fourth.cpp: At top level:
> fourth.cpp:74: parse error before `{'
> fourth.cpp:76: syntax error before `<'
> fourth.cpp:77: syntax error before `<'
> fourth.cpp:78: syntax error before `<'
> fourth.cpp:79: syntax error before `<'
> fourth.cpp:80: syntax error before `<'
> fourth.cpp:81: syntax error before `<'
>
>
> and I can't figure out what to do<!!!.
> Can anyone help out<?.


Fix the errors one by one. Remember that everything you are using
must be declared first.

BTW: consider using std::vector instead of the 4 doubles,
it might come in handy if you need to compare 1, 2, 3, 5, ... values.

Stephan Brönnimann
(E-Mail Removed)
Open source rating and billing engine for communication networks.
 
Reply With Quote
 
tomakated
Guest
Posts: n/a
 
      11-17-2004
Not LIKING FUNCTIONS right now. Been at it for about 4 hours now..
What am I missing, I know it's right in front of me<?.

P.S. Have to work off main can't change it<!!!.

Now I get different errors......
fourth.cpp: In function `int main()':
fourth.cpp:13: implicit declaration of function `int instructions(...)'
fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
fourth.cpp:20: implicit declaration of function `int getresponse(...)'
fourth.cpp: In function `int getresponse()':
fourth.cpp:48: parse error before `>'
fourth.cpp:51: `response' undeclared (first use this function)
fourth.cpp:51: (Each undeclared identifier is reported only once
fourth.cpp:51: for each function it appears in.)
fourth.cpp:54: parse error before `,'
fourth.cpp:55: `total' undeclared (first use this function)
fourth.cpp:55: `lowest' undeclared (first use this function)
fourth.cpp:62: type specifier omitted for parameter
fourth.cpp:62: type specifier omitted for parameter
fourth.cpp:62: type specifier omitted for parameter
fourth.cpp:64: parse error before `{'
fourth.cpp: At top level:
fourth.cpp:89: parse error before `{'
fourth.cpp:92: syntax error before `<'
fourth.cpp:93: syntax error before `<'
fourth.cpp:94: syntax error before `<'
fourth.cpp:95: syntax error before `<'
fourth.cpp:96: syntax error before `<'
fourth.cpp:97: syntax error before `<'


with this code....
#include <iostream>
#include <iomanip>

double findcheapest(double, double, double, double);
void printresults (double, double, double, double, double, double,
double);


int main()
{
double item1, item2, item3, item4, lowest=0, total;
char response;

instructions();
item1 = get_item_price();
item2 = get_item_price();
item3 = get_item_price();
item4 = get_item_price();
total = item1 + item2 + item3 + item4;
cout << "Do you have a coupon for this purchase? \n" ;
response = getresponse() ;
if (response == 'y')
{ lowest = findcheapest(item1, item2, item3,
item4);
total = total - lowest;
}
printresults (response, item1, item2, item3, item4,
total, lowest);
}


double get_item_price()
{
double i;
cout << "Enter the four item prices you want to buy.";
cout << "Item1: ";
cin >> i;
while (i<0)
{
cout << "Please enter a positive number: ";
cin >> i;
}

return i;
}

int getresponse()
{
cout << "Do you have a coupon for this purchase? (y or n):
\n"
cin >> response;


if ((response == 'y') || (response == 'Y'))

{
findcheapest(double item1, double item2, double item3,
double item4);
total = total - lowest;

if ((response == 'n') || (response == 'N'))
total = total;

}

double findcheapest(double item1, item2, item3, item4)

{
if (((item1 <= item2) && (item1 <= item3) && (item1 <=
item4)))
{
lowest = item1;
}
if (((item2 <= item1) && (item2 <= item3) && (item2 <=
item4)))
{
lowest = item2;
}

if (((item3 <= item1) && (item3 <= item2) && (item3 <=
item4)))
{
lowest = item3;
}

if (((item4 <= item1) && (item4 <= item2) && (item4 <=
item3)))
{
lowest = item4;
}

}

void printresults(double response, double item1, double item2,
double item3, double item4, double total, double lowest);

{

cout << "Item 1 $" << setw(10) << item1 << endl;
cout << "Item 2 $" << setw(10) << item2 << endl;
cout << "Item 3 $" << setw(10) << item3 << endl;
cout << "Item 4 $" << setw(10) << item4 << endl;
cout << "Coupon Discount $(" << setw(10) <<
lowest<<")"<< endl;
cout << "----------------------------" << endl;
cout << "Total $" << setw(10) << total << endl;
}


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-17-2004

"tomakated" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) lkaboutprogramming.com...
> Not LIKING FUNCTIONS right now. Been at it for about 4 hours now..
> What am I missing, I know it's right in front of me<?.
>
> P.S. Have to work off main can't change it<!!!.
>
> Now I get different errors......
> fourth.cpp: In function `int main()':
> fourth.cpp:13: implicit declaration of function `int instructions(...)'


This is a lack of a prototype. You have prototypes for findcheapest and
printresults, you need them for all your functions.

> fourth.cpp:14: implicit declaration of function `int get_item_price(...)'


Ditto

> fourth.cpp:20: implicit declaration of function `int getresponse(...)'


Ditto

> fourth.cpp: In function `int getresponse()':
> fourth.cpp:48: parse error before `>'


This is mismatched curly brackets, as has already been explained to you by
Jon Bell. You need an extra } before the function get_item_price. The way to
avoid this sort of error is to INDENT YOUR CODE PROPERLY, as already stated
by Jon Bell.

Everything after here should be ignored until you fix these problems. Always
fix the first errors first, because the later errors might just be the
compiler getting confused by your earlier errors.

john


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-17-2004
tomakated wrote:
>
> Not LIKING FUNCTIONS right now. Been at it for about 4 hours now..
> What am I missing, I know it's right in front of me<?.
>


2 things:

* your code style is terrible. No wonder you can't see any errors in it.

* you made a typical newbie mistake: You wrote to much code in one big
rush and now unable to sort things out. Start small! Compile it, fix
any errors you have, then add new things.

Start eg with

int main()
{
}

compile it.
Then add things. But add *small* amounts of code

You could eg. continue with

int main()
{
double item1;

item1 = get_item_price();
cout << "You entered " << item1 << '\n';
}

double get_item_price()
{
return 0.0;
}

and make that compileable
(The above will already trigger the firt error in your program. The error
messages will be obvious and you should have no troubles fixing that)

Only then start to add the next thing. You could eg. add the inner workings
of get_item_price.

This way you usually will not be left with lots of error messages and don't
have an idea where the error could be. The error is always related to the
last code section you added. If you add only small amounts of code, you
only have to search in small code sections for errors.


PS: You can apply this scheme even now.
Save what you have right now into a new file (the backup). Remove everything
and start with a fresh, empty int main(). Then start copying things
from the backup into the new source code. But add small code sections
and recompile after each step. Do it in the spirit of above. Don't add
the complete main() from the backup. Just copy/paste enough from the
backup that you still have a complete main() which is compilable.



--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      11-17-2004
"tomakated" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) alkaboutprogramming.com>...

> Now I get different errors......
> fourth.cpp: In function `int main()':
> fourth.cpp:13: implicit declaration of function `int instructions(...)'
> fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
> fourth.cpp:20: implicit declaration of function `int getresponse(...)'


You need to tell the compiler what these three functions look like.
main() calls them, so you must declare them before main. The 'int'
return type is just a guess by the compiler, so you might need to
change that.

> fourth.cpp: In function `int getresponse()':
> fourth.cpp:48: parse error before `>'


It's std::cout, std::cin, etcetera (if your compiler isn't obsolete)

Further errors occur because the compiler is lost.

HTH,
Michiel Salters

> with this code....
> #include <iostream>
> #include <iomanip>
>
> double findcheapest(double, double, double, double);
> void printresults (double, double, double, double, double, double,
> double);
>
>
> int main()
> {
> double item1, item2, item3, item4, lowest=0, total;
> char response;
>
> instructions();
> item1 = get_item_price();
> item2 = get_item_price();
> item3 = get_item_price();
> item4 = get_item_price();
> total = item1 + item2 + item3 + item4;
> cout << "Do you have a coupon for this purchase? \n" ;
> response = getresponse() ;
> if (response == 'y')
> { lowest = findcheapest(item1, item2, item3,
> item4);
> total = total - lowest;
> }
> printresults (response, item1, item2, item3, item4,
> total, lowest);
> }
>
>
> double get_item_price()
> {
> double i;
> cout << "Enter the four item prices you want to buy.";
> cout << "Item1: ";
> cin >> i;
> while (i<0)
> {
> cout << "Please enter a positive number: ";
> cin >> i;
> }
>
> return i;
> }
>
> int getresponse()
> {
> cout << "Do you have a coupon for this purchase? (y or n):
> \n"
> cin >> response;
>
>
> if ((response == 'y') || (response == 'Y'))
>
> {
> findcheapest(double item1, double item2, double item3,
> double item4);
> total = total - lowest;
>
> if ((response == 'n') || (response == 'N'))
> total = total;
>
> }
>
> double findcheapest(double item1, item2, item3, item4)
>
> {
> if (((item1 <= item2) && (item1 <= item3) && (item1 <=
> item4)))
> {
> lowest = item1;
> }
> if (((item2 <= item1) && (item2 <= item3) && (item2 <=
> item4)))
> {
> lowest = item2;
> }
>
> if (((item3 <= item1) && (item3 <= item2) && (item3 <=
> item4)))
> {
> lowest = item3;
> }
>
> if (((item4 <= item1) && (item4 <= item2) && (item4 <=
> item3)))
> {
> lowest = item4;
> }
>
> }
>
> void printresults(double response, double item1, double item2,
> double item3, double item4, double total, double lowest);
>
> {
>
> cout << "Item 1 $" << setw(10) << item1 << endl;
> cout << "Item 2 $" << setw(10) << item2 << endl;
> cout << "Item 3 $" << setw(10) << item3 << endl;
> cout << "Item 4 $" << setw(10) << item4 << endl;
> cout << "Coupon Discount $(" << setw(10) <<
> lowest<<")"<< endl;
> cout << "----------------------------" << endl;
> cout << "Total $" << setw(10) << total << endl;
> }

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-17-2004

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> tomakated wrote:
>>
>> Not LIKING FUNCTIONS right now. Been at it for about 4 hours now..
>> What am I missing, I know it's right in front of me<?.
>>

>
> 2 things:
>
> * your code style is terrible. No wonder you can't see any errors in it.
>
> * you made a typical newbie mistake: You wrote to much code in one big
> rush and now unable to sort things out. Start small! Compile it, fix
> any errors you have, then add new things.
>


[more good advice snipped]

It's undoubtedly good advice, but have you ever known a newbie follow advice
like that? They have to learn the hard way.

OP, most programmers work like this, no matter how expert. The difference
when you get to be expert is that you add code in bigger chunks. As a newbie
you should add code in very small chunks and not be afraid to get to your
final goal gradually.

john


 
Reply With Quote
 
Carl Muller
Guest
Posts: n/a
 
      11-17-2004
"tomakated" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) alkaboutprogramming.com>...
> Not LIKING FUNCTIONS right now. Been at it for about 4 hours now..
> What am I missing, I know it's right in front of me<?.


Learn to read the error messages the compiler is telling you.

> P.S. Have to work off main can't change it<!!!.
>
> Now I get different errors......
> fourth.cpp: In function `int main()':
> fourth.cpp:13: implicit declaration of function `int instructions(...)'
> fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
> fourth.cpp:20: implicit declaration of function `int getresponse(...)'


These are warnings that you are using a function that you haven't
told the compiler about yet.
Look up "prototypes", or declare the functions before you use them.
For example you could put the main function at the bottom of the file.

> fourth.cpp: In function `int getresponse()':
> fourth.cpp:48: parse error before `>'


This is telling you to not forget the semicolon at the end of the cout line.
I would not pay much attention to subsequent error message until you fix that
one.

> fourth.cpp:51: `response' undeclared (first use this function)
> fourth.cpp:51: (Each undeclared identifier is reported only once
> fourth.cpp:51: for each function it appears in.)
> fourth.cpp:54: parse error before `,'

[etc.]

> int getresponse()
> {
> cout << "Do you have a coupon for this purchase? (y or n):
> \n"
> cin >> response;

 
Reply With Quote
 
velthuijsen
Guest
Posts: n/a
 
      11-17-2004
> Now I get different errors......
> fourth.cpp: In function `int main()':
> fourth.cpp:13: implicit declaration of function `int instructions(...)'
> fourth.cpp:14: implicit declaration of function `int get_item_price(...)'
> fourth.cpp:20: implicit declaration of function `int getresponse(...)'

For these 3 do a declaration like findcheapest
The compiler can only work with what it knows exists, so you got to
tell it that yes there exists a function like getresponse and that you
are going to detail how it actually works later.

> fourth.cpp: In function `int getresponse()':
> fourth.cpp:48: parse error before `>'
> fourth.cpp:51: `response' undeclared (first use this function)
> fourth.cpp:51: (Each undeclared identifier is reported only once
> fourth.cpp:51: for each function it appears in.)
> fourth.cpp:54: parse error before `,'
> fourth.cpp:55: `total' undeclared (first use this function)
> fourth.cpp:55: `lowest' undeclared (first use this function)
> fourth.cpp:62: type specifier omitted for parameter
> fourth.cpp:62: type specifier omitted for parameter
> fourth.cpp:62: type specifier omitted for parameter
> fourth.cpp:64: parse error before `{'
> fourth.cpp: At top level:
> fourth.cpp:89: parse error before `{'
> fourth.cpp:92: syntax error before `<'
> fourth.cpp:93: syntax error before `<'
> fourth.cpp:94: syntax error before `<'
> fourth.cpp:95: syntax error before `<'
> fourth.cpp:96: syntax error before `<'
> fourth.cpp:97: syntax error before `<'


These are all errors because you got small things wrong, like :
Missing a ; or having a ; at the wrong place.
Forgetting that variables used in one function (this includes main)
are not necessarily known in other functions. Generally unless a
variable is declared global it only exists in the { } pair it is
declared in (there are quite a few exceptions to this but to get
started it'll do).
The use of type specfiers at the wrong place (in getresponse).

Question: Is this homework where you have to learn how to move parts
of the main to seperate functions?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments