944,080 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1599
  • C++ RSS
May 7th, 2007
0

Function help/homework

Expand Post »
Hi,

I'm fairly new to c++ and I'm having some trouble getting this to work as a function.

Here's the error I get:

error C2447: '{' : missing function header (old-style formal list?)

I can work through the minor troubleshooting of basic syntax I just don't get how to call the function properly.

Thanks!!

here's the code:
  1. #include<iostream>// calls I/O library
  2. usingnamespace std;
  3. double sales;//declare sales as a double so decimals can be used
  4. void salesInput()//Sales Input Function
  5. {
  6. const double basePay = 200;//declare base salary as a constant
  7. const double grossSales = .09;//declare gross sales as a constant
  8. double com;//declare commission as double so decimals can be used
  9. com=((sales * 0.09) + 200);//calculation for commission
  10. cout << "Total pay plus 9% commission is " <<com << endl;//output final results to the screen
  11. }
  12. usingnamespace std;
  13. int main();
  14. {
  15. cout << "Enter the Sales for the week, enter a zero to quit"<< endl; //ask user for input
  16. cin >> sales;//get input and store as variable sales
  17. while (sales != 0 )//Start sales loop, executes function sales input if anything other than a zero is entered, if zero then exit
  18. {
  19. salesInput();//calls sales input function
  20. }
  21. cout << “You entered a zero to quit!” << endl;
  22. return0;
  23. }
Last edited by Ancient Dragon; May 7th, 2007 at 9:54 pm. Reason: removed color codes and added code tags to show line numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssgatbliss is offline Offline
9 posts
since May 2007
May 7th, 2007
0

Re: Function help/homework

remove the semicolon at the end of line 13. That's a common mistake for even old-timers -- you just need to learn how to recognize the error.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
May 7th, 2007
0

Re: Function help/homework

remove the semicolon at the end of line 13. That's a common mistake for even old-timers -- you just need to learn how to recognize the error.
OK fixed that (thanks) and I removed line 21 which was causing an error (I'll fix that later). Problem now is I'm in a continuous loop, which is what I had before I tried to make a function out of it. How do I make it execute the function once then return to ask if there is another amount to put in?

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssgatbliss is offline Offline
9 posts
since May 2007
May 7th, 2007
0

Re: Function help/homework

also delete line 12, don't have to declare that only once at the top of the program and after the includes.

>>How do I make it execute the function once then return to ask if there is another amount

move lines 17 and 18 up above line 15. Also, there is no need to make sales a global variable. Move line 3 to be local to main() then pass it as a parameter to salesInput() function.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
May 8th, 2007
0

Re: Function help/homework

also delete line 12, don't have to declare that only once at the top of the program and after the includes.

>>How do I make it execute the function once then return to ask if there is another amount

move lines 17 and 18 up above line 15. Also, there is no need to make sales a global variable. Move line 3 to be local to main() then pass it as a parameter to salesInput() function.
OK, still problems. It compiles but now it goes straight to the "Press any key to continue"

1 #include <iostream>// calls I/O library
2 using namespace std;
3 double sales;//declare sales as a double so decimals can be used
4 double salesInput()//Sales Input Function
5 {
6 const double basePay = 200;//declare base salary as a constant
7 const double grossSales = .09;//declare gross sales as a constant
8 double com;//declare commission as double so decimals can be used
9 com=((sales * 0.09) + 200);//calculation for commission
10 cout << "Total pay plus 9% commission is " <<com << endl;//output final results to the screen
11}
12 int main()
13{
14 while
15 (sales!=0 )//Start sales loop, executes function sales input if anything other than a zero is entered, if zero then exit
16 {
17 cout << "Enter the Sales for the week, enter a zero to quit"<< endl; //ask user for input
18 cin >> sales;//get input and store as variable sales
19 salesInput();//calls sales input function
20 }
21 }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssgatbliss is offline Offline
9 posts
since May 2007
May 8th, 2007
0

Re: Function help/homework

What's the value of sales when the program starts?

And use Code Tags!
Moderator
Reputation Points: 3281
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
May 8th, 2007
0

Re: Function help/homework

Click to Expand / Collapse  Quote originally posted by WaltP ...
What's the value of sales when the program starts?

And use Code Tags!
Sorry about the code tags, my bad. But that was it, I simply added a sales=1 at the beginning and it worked.

Thanks for the help!!

Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssgatbliss is offline Offline
9 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pascal -> C++; Binary, convert?
Next Thread in C++ Forum Timeline: How To Read A Webpage ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC