help with functions please
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.
#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;
}
<strong>void getHours(int &input1, string &hours)
{
cout << hours;
cin >> input1;
}
void getMinutes(int &input2, string &minutes)
{
cout << minutes;
cin >> input2;
}</strong>
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!
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
hmm still confused
am i close?
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 :D
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
#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;
}
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
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?
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
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....
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
alright i did that still get the same errors
cannot convert parameter 2 from const char to string
my code
#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;
}
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
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
std::string prompt;
prompt = Enter the number of minutes for the starting time.";
getMinutes(minutes, prompt);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
yay finally my protypes are correct.
I used the Typecase the string leterals method, works fine now. finally!
thank you :D and to everyone who is helping out. now ima work on the rest of the code. wont be too long till i get stuck again hehe.
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
stuck again.
hours cant be less than 0 or greater than 23
minutes cant be less than 0 or greater than 59
why isnt this working?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void getHours(int& input1, string& s);
void getMinutes(int& input2, string& s);
//void calcTotalTime(int, int,int,int);
bool isValidMinutes(int);
bool isValidHours(int);
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, (string)("Enter the number of hours for the starting time: "));
getMinutes(minutes, (string)("Enter the number of minutes for the starting time: "));
getHours (addedHours, (string)("Enter the number of hours to be added to the starting time: "));
getMinutes (addedMinutes, (string)("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)
{
bool result;
do {
cout << s;
cin >> input1;
result = isValidHours(input1);
} while (input1 == false);
}
void getMinutes (int& input2, string& s)
{
bool result2;
do {
cout << s;
cin >> input2;
result2 = isValidMinutes(input2);
} while (input2 == false);
}
bool isValidHours (int x)
{
if ( x >= 1 && x <= 23)
return true;
else return false;
}
bool isValidMinutes (int xx)
{
if ( xx >= 1 && xx <=59)
return true;
else return false;
}
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
The do loop is incorrect. See correction below. Same problem with getMinutes().
void getHours (int& input1, string& s)
{
bool result;
do {
cout << s;
cin >> input1;
result = isValidHours(input1);
} while (result == false);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thanks AD, simple mistake i made. im almost done with this program all i have to do now is add the hours with added hours and minutes wiith added minutes but i just dont know how to start it.
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
not 15 hours and 62 minutes
i think im going to be using the mudulus % operation?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void getHours(int& input1, string& s);
void getMinutes(int& input2, string& s);
void calcTotalTime(int& h, int& ah, int& m, int& am);
bool isValidMinutes(int);
bool isValidHours(int);
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, (string)("Enter the number of hours for the starting time: "));
getMinutes(minutes, (string)("Enter the number of minutes for the starting time: "));
getHours (addedHours, (string)("Enter the number of hours to be added to the starting time: "));
getMinutes (addedMinutes, (string)("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)
{
bool result;
do {
cout << s;
cin >> input1;
result = isValidHours(input1);
} while (result == false);
}
void getMinutes (int& input2, string& s)
{
bool result2;
do {
cout << s;
cin >> input2;
result2 = isValidMinutes(input2);
} while (result2 == false);
}
bool isValidHours (int x)
{
if ( x >= 0 && x <= 23)
return true;
else return false;
}
bool isValidMinutes (int xx)
{
if ( xx >= 0 && xx <=59)
return true;
else return false;
}
//h=hours ah= addedhours, m=minutes, am=addedminutes
void calcTotalTime(int& h, int& ah, int& m, int& am)
{
//-------
}
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
void calcTotalTime(int& h, int& ah, int& m, int& am)
Do the added hours/minutes need to be passed by reference? You're probably not changing them here, only the base hours/minutes gets changed and used back at in main.
Yes, modulus operator is your friend for this, as is division. Just divide the total minutes to find any number of additional hours, then use modulus to find the minutes that remain.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
hmmm so
void calctotaltime needs 4 ints or 2 ints?
and i dont even know how to use modulus.
do i need to declare a new variable ex total?
ill try but damn i wont get too far hehe
anbuninja
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Your function still need the four inputs. Only two of them need be by reference, as they will convey information back to the caller.
Just add the hours. Add the minutes.
Minutes / 60 will be any number of full hours that the minutes add up to. Add that result to hours.
Minutes % 60 will be the remaining minutes. Assign that value back to minutes.
Modulus operator gives you the remainder left over from integer division. Just like in grade school, when we learn long division we might give the answer to a problem like 17 / 3 as 5 with remainder 2. That remainder is what modulus operator gives.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228