| | |
help with functions please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
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.
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!
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)
#include <iostream> #include <string> using namespace std; void getHours(int, string); void getMinutes(int, string); void calcTotalTime (/* complete */); bool isValidMinutes (/* complete */); bool isValidHours(/* complete */); const int MAX_MINS = 59; const int MAX_HOURS = 23; const int MIN_MINS = 0; const int MIN_HOURS = 0; int main () { int hours, minutes, addedHours, addedMinutes; getHours(hours, "Enter the number of hours for the starting time: "); getMinutes(minutes, "Enter the number of minutes for the starting time: "); getHours (addedHours, "Enter the number of hours to be added to the starting time: "); getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: "); calcTotalTime (hours, minutes, addedHours, addedMinutes); cout << "\nThe total time is " << hours << " hours and " << minutes << " minutes." << endl; system("pause"); return 0; } [B]void getHours(int &input1, string &hours) { cout << hours; cin >> input1; } void getMinutes(int &input2, string &minutes) { cout << minutes; cin >> input2; }[/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
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.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
hmm still confused
am i close?
help me atleast on this one then i can get an idea and try to do the rest
am i close?
C++ Syntax (Toggle Plain Text)
void getHours (string& getHours, int& hours) { cout << getHours; cin >> hours; }
help me atleast on this one then i can get an idea and try to do the rest
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.
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.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
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
i did what u told me (i think) and still get 2 errors
code so far
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void getHours(int &hours, string &getHours); void getMinutes(int &minutes, string& getMinutes); //void calcTotalTime (/* complete */); //bool isValidMinutes (/* complete */); //bool isValidHours(/* complete */); const int MAX_MINS = 59; const int MAX_HOURS = 23; const int MIN_MINS = 0; const int MIN_HOURS = 0; int main () { int hours, minutes, addedHours, addedMinutes; getHours(hours, "Enter the number of hours for the starting time: "); getMinutes(minutes, "Enter the number of minutes for the starting time: "); //getHours (addedHours, "Enter the number of hours to be added to the starting time: "); //getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: "); //calcTotalTime (hours, minutes, addedHours, addedMinutes); cout << "\nThe total time is " << hours << " hours and " << minutes << " minutes." << endl; system("pause"); return 0; } void getHours (int& hours, string& getHours) { cout << getHours; cin >> hours; } void getMinutes (int& minutes, string& getMinutes) { cout << getMinutes; cin >> minutes; }
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
the second example would work, assuming that you declare:
at the top and the definition is:
C++ Syntax (Toggle Plain Text)
void getHours(int& input1, string& s);
C++ Syntax (Toggle Plain Text)
void getHours(int& input1, string& s) { cout << s; cin >> input1; }
•
•
•
•
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
alright i did that still get the same errors
cannot convert parameter 2 from const char to string
my code
cannot convert parameter 2 from const char to string
my code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void getHours(int& input1, string& s); void getMinutes(int& input2, string& s); //void calcTotalTime (/* complete */); //bool isValidMinutes (/* complete */); //bool isValidHours(/* complete */); const int MAX_MINS = 59; const int MAX_HOURS = 23; const int MIN_MINS = 0; const int MIN_HOURS = 0; int main () { int hours, minutes, addedHours, addedMinutes; getHours(hours, "Enter the number of hours for the starting time: "); getMinutes(minutes, "Enter the number of minutes for the starting time: "); //getHours (addedHours, "Enter the number of hours to be added to the starting time: "); //getMinutes (addedMinutes, "Enter the number of minutes to be added to the starting time: "); //calcTotalTime (hours, minutes, addedHours, addedMinutes); cout << "\nThe total time is " << hours << " hours and " << minutes << " minutes." << endl; system("pause"); return 0; } void getHours (int& input1, string& s) { cout << s; cin >> input1; } void getMinutes (int& input2, string& s) { cout << s; cin >> input2; }
•
•
•
•
alright i did that still get the same errors
cannot convert parameter 2 from const char to string
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)
std::string prompt; prompt = Enter the number of minutes for the starting time."; getMinutes(minutes, prompt);
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.
![]() |
Similar Threads
- VB's Left, Right, Mid Functions in C++? (C++)
- User defined functions (C++)
- Double Linked Lists and Functions required (C++)
- How to write FNVAL functions (Java)
- I dont see any difference between these 2 functions, DO YOU? (C)
- access Digital Camera Functions (C++)
Other Threads in the C++ Forum
- Previous Thread: Algos or dev?
- Next Thread: helpp!!
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






