Reputation: 25
i am try to solve ex7 from chapter 2 (Prata's c++ primer plus)
The task is:
Write a program that asks the user to enter an hour value and a minute value.The main() function should then pass these two values to a type void function that displays the two values in the format shown in the following sample run: Enter the number of hours: 9 Enter the number of minutes: 28 Time: 9:28
My Code is :
#include <iostream>
void hours(int);
void minutes(int);
int main()
{
using namespace std;
int hhrs;
int mnts;
cout << "Enter hours: "; cin >> hhrs; cout << endl;
cout << "Enter minutes: "; cin >> mnts; cout << endl;
hours(hhrs);
minutes(mnts);
cout << "Time is: " << hours << ":" << minutes << endl;
cin.get();
cin.get();
return 0;
}
void hours(int n)
{
using namespace std;
cout << n;
}
void miutes(int m)
{
using namespace std;
cout << m;
}
Errors are :
1) Error 1 error LNK2019: unresolved external symbol "void __cdecl minutes(int)" (?minutes@@YAXH@Z) referenced in function _main G:\~DEV#c++\he\he\Source.obj
2) Error 2 error LNK1120: 1 unresolved externals G:\~DEV#c++\he\Debug\he.exe 1
Upvotes: 0
Views: 289
Reputation: 677
Apart from the spelling mistake in void miutes(int m)
, the logic does not require two seperate functions to call.
Also if you check the below line:
cout << "Time is: " << hours << ":" << minutes << endl;
Here "hours" and "minutes" will always evaluate as True
. So eventually it will show 1:1 in output, not 9:28 (as per your input).
Upvotes: 0
Reputation: 15872
In addition to your misspelling (miutes
instead of minutes
), you also are not doing what the problem is asking:
Write a program that asks the user to enter an hour value and a minute value.The main() function should then pass these two values to a type void function that displays the two values in the format shown in the following sample run: Enter the number of hours: 9 Enter the number of minutes: 28 Time: 9:28
That is asking you to input 2 values, and pass those values to a single function that will print them in the desired format.
Upvotes: 4
Reputation: 110658
You misspelt minutes
in the function definition.
void miutes(int m)
^^^^^^
Note that your hours
and minutes
functions do exactly the same thing. There's no point having both of them. You could have a single function called print
. However, since all they do is call another single function (cout.operator<<
), I would just get rid of them all together. Just do cout << hhrs
and cout << mnts
when you need it. Your problem does require you to move the printing of both the hours and minutes into a function though.
Upvotes: 2