help with functions please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

help with functions please

 
0
  #1
Feb 24th, 2009
so im starting the second half of my c++ class and we are getting into functions. are first assignment and im already stuck -__-


the teacher gives us the code for above main so we just need to write the prototypes.

heres my code so far (prototypes probably wrong) it builds but doesnt run.
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void getHours(int, string);
  6. void getMinutes(int, string);
  7. void calcTotalTime (/* complete */);
  8. bool isValidMinutes (/* complete */);
  9. bool isValidHours(/* complete */);
  10. const int MAX_MINS = 59;
  11. const int MAX_HOURS = 23;
  12. const int MIN_MINS = 0;
  13. const int MIN_HOURS = 0;
  14.  
  15. int main ()
  16. {
  17. int hours, minutes, addedHours, addedMinutes;
  18. getHours(hours, "Enter the number of hours for the starting time: ");
  19. getMinutes(minutes, "Enter the number of minutes for the starting time: ");
  20. getHours (addedHours, "Enter the number of hours to be added to the starting time: ");
  21. getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: ");
  22. calcTotalTime (hours, minutes, addedHours, addedMinutes);
  23. cout << "\nThe total time is " << hours << " hours and "
  24. << minutes << " minutes." << endl;
  25. system("pause");
  26. return 0;
  27. }
  28.  
  29.  
  30. [B]void getHours(int &input1, string &hours)
  31. {
  32.  
  33. cout << hours;
  34. cin >> input1;
  35. }
  36.  
  37. void getMinutes(int &input2, string &minutes)
  38. {
  39. cout << minutes;
  40. cin >> input2;
  41. }[/B]


need help with the bold parts etc.. im really confuse on writing the prototypes

heres how its supposed to run
Enter the number of hours for the starting time: 12
Enter the number of minutes for the starting time: 44
Enter the number of hours to be added to the starting time: 3
Enter the number of minutes to be added to the starting time: 18

The total time is 16 hours and 2 minutes.

help!
Last edited by Ancient Dragon; Feb 25th, 2009 at 8:11 pm. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with functions please

 
0
  #2
Feb 24th, 2009
The prototypes do not match the actual functions. Compare them and you will see what's wrong. After that you should realize that you can't pass string literals as you did to those functions -- must pass a reference to std::string object.
Last edited by Ancient Dragon; Feb 24th, 2009 at 9:18 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: help with functions please

 
0
  #3
Feb 25th, 2009
hmm still confused

am i close?

  1. void getHours (string& getHours, int& hours)
  2. {
  3. cout << getHours;
  4. cin >> hours;
  5. }

help me atleast on this one then i can get an idea and try to do the rest
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with functions please

 
0
  #4
Feb 25th, 2009
What you just posted is incorrect. You can not have the name of a parameter the same as the name of the function. You will get a compiler error on that.


Look at line 5 of the code you originally posted. Notice the two parameters are an int and a string. Now look at the parameters in on line 30 -- the parameters are a reference to an int, and a reference to a string. Change the function prototype on line 5 to look like the function header located on line 30. All you have to do is copy from line 30 and paste on line 5.
Last edited by Ancient Dragon; Feb 25th, 2009 at 8:15 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: help with functions please

 
0
  #5
Feb 25th, 2009
okay right now all i want is to get a successful build just on getHours and getMinutes.

i did what u told me (i think) and still get 2 errors

code so far

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. void getHours(int &hours, string &getHours);
  8. void getMinutes(int &minutes, string& getMinutes);
  9. //void calcTotalTime (/* complete */);
  10. //bool isValidMinutes (/* complete */);
  11. //bool isValidHours(/* complete */);
  12. const int MAX_MINS = 59;
  13. const int MAX_HOURS = 23;
  14. const int MIN_MINS = 0;
  15. const int MIN_HOURS = 0;
  16.  
  17. int main ()
  18. {
  19. int hours, minutes, addedHours, addedMinutes;
  20. getHours(hours, "Enter the number of hours for the starting time: ");
  21. getMinutes(minutes, "Enter the number of minutes for the starting time: ");
  22. //getHours (addedHours, "Enter the number of hours to be added to the starting time: ");
  23. //getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: ");
  24. //calcTotalTime (hours, minutes, addedHours, addedMinutes);
  25. cout << "\nThe total time is " << hours << " hours and "
  26. << minutes << " minutes." << endl;
  27. system("pause");
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33. void getHours (int& hours, string& getHours)
  34. {
  35. cout << getHours;
  36. cin >> hours;
  37. }
  38.  
  39. void getMinutes (int& minutes, string& getMinutes)
  40. {
  41. cout << getMinutes;
  42. cin >> minutes;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: help with functions please

 
0
  #6
Feb 25th, 2009
so i have to change the names on these to something else


void getHours (int& hours, string& getHours)
{
cout << getHours;
cin >> hours;
}

would something like (int& input1, string& s)
{
cout << s;
cin >> input1;
}

work?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: help with functions please

 
0
  #7
Feb 25th, 2009
the second example would work, assuming that you declare:
  1. void getHours(int& input1, string& s);
at the top and the definition is:
  1. void getHours(int& input1, string& s)
  2. {
  3. cout << s;
  4. cin >> input1;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: help with functions please

 
0
  #8
Feb 25th, 2009
Originally Posted by Ancient Dragon View Post
What you just posted is incorrect. You can not have the name of a parameter the same as the name of the function. You will get a compiler error on that.
Actually, you can have the same name for the function and one of its parameters. The scope of the parameter is local to the function, and it masks the function name.

Now this would be awkward if trying to write a recursive function....
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: help with functions please

 
0
  #9
Feb 25th, 2009
alright i did that still get the same errors

cannot convert parameter 2 from const char to string

my code

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. void getHours(int& input1, string& s);
  8. void getMinutes(int& input2, string& s);
  9. //void calcTotalTime (/* complete */);
  10. //bool isValidMinutes (/* complete */);
  11. //bool isValidHours(/* complete */);
  12. const int MAX_MINS = 59;
  13. const int MAX_HOURS = 23;
  14. const int MIN_MINS = 0;
  15. const int MIN_HOURS = 0;
  16.  
  17. int main ()
  18. {
  19. int hours, minutes, addedHours, addedMinutes;
  20. getHours(hours, "Enter the number of hours for the starting time: ");
  21. getMinutes(minutes, "Enter the number of minutes for the starting time: ");
  22. //getHours (addedHours, "Enter the number of hours to be added to the starting time: ");
  23. //getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: ");
  24. //calcTotalTime (hours, minutes, addedHours, addedMinutes);
  25. cout << "\nThe total time is " << hours << " hours and "
  26. << minutes << " minutes." << endl;
  27. system("pause");
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33. void getHours (int& input1, string& s)
  34. {
  35. cout << s;
  36. cin >> input1;
  37. }
  38.  
  39. void getMinutes (int& input2, string& s)
  40. {
  41. cout << s;
  42. cin >> input2;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with functions please

 
0
  #10
Feb 25th, 2009
Originally Posted by anbuninja View Post
alright i did that still get the same errors

cannot convert parameter 2 from const char to string
Your function prototype is correct now. Next you need to realize that you can not pass a string literal to those functions because the functions expect a reference to a std::string object.

There are a couple ways to fix that:
1) remove the reference operator from the function prototype and the actual function. I see no reason to pass the string by reference anyway. But I understand your instructor wants it that way, so maybe that is not an option for you.

2) Typecase the string leterals
getHours(hours, "Enter the number of hours for the starting time: ");
    getMinutes(minutes, (string)("Enter the number of minutes for the starting time"));

3) split the string literals out
  1. std::string prompt;
  2. prompt = Enter the number of minutes for the starting time.";
  3. getMinutes(minutes, prompt);
  4.  
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC