944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 864
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 24th, 2009
0

help with functions please

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Feb 24th, 2009
0

Re: help with functions please

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 25th, 2009
0

Re: help with functions please

hmm still confused

am i close?

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Feb 25th, 2009
0

Re: help with functions please

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 25th, 2009
0

Re: help with functions please

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Feb 25th, 2009
0

Re: help with functions please

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Feb 25th, 2009
0

Re: help with functions please

the second example would work, assuming that you declare:
C++ Syntax (Toggle Plain Text)
  1. void getHours(int& input1, string& s);
at the top and the definition is:
C++ Syntax (Toggle Plain Text)
  1. void getHours(int& input1, string& s)
  2. {
  3. cout << s;
  4. cin >> input1;
  5. }
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Feb 25th, 2009
0

Re: help with functions please

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....
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 25th, 2009
0

Re: help with functions please

alright i did that still get the same errors

cannot convert parameter 2 from const char to string

my code

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Feb 25th, 2009
0

Re: help with functions please

Click to Expand / Collapse  Quote originally posted by anbuninja ...
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
C++ Syntax (Toggle Plain Text)
  1. std::string prompt;
  2. prompt = Enter the number of minutes for the starting time.";
  3. getMinutes(minutes, prompt);
  4.  
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

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: Algos or dev?
Next Thread in C++ Forum Timeline: helpp!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC