hey guys, im working with c++ primer 5th edition, and one of the exercises it gives is to write a program to take 2 inputs from the user(hours and minuets) and then pass it to a void function in the program to display the output of those two inputs in the format "Hours:minuets", i keep getting C2062 int unexpected on line 28 of the code(the definition for int hourmin(void)), anyways, i was just wondering if im doing this correctly and how i could correctly display those in the proper format(without doing separate hour/min, ie cout<<"Time"<<hour<<":"<<min;) i understand i could do it this way but im trying not to cheat and challenge myself a bit more....... thanks in advance boyos!
//program to take user input and display inputs(hour/min) in standard output form
#include<iostream>
int hourmin(void)
int main()
{
using namespace std;
int hour;
cout<<"Enter the number of hours:"<< hour << endl;
cin>>hour;
int min;
cout<<"Enter the number of minuets:" << min << endl;
cin>>min;
int hourmin;
cout<<"Time:"<< hourmin;
cin.get();
cin.get();
return 0;
}
int hourmin(void)
{
int hour, int min;//not sure whether compilers referring to first or second int, or even if this is correct way to display in hour:min format
removed the int on min, however it keeps coming up with C4700 and C4101 for hour and min, also i switched the void and int on my definition of hourmin...... derrrp on my part
Firstly, the only way to have hour:min printed it cout << hour << ":" << min; That's no cheating. It's good that you try to use functions but you're doing it wrong.
The correct function declaration would be int hourmin(int h, int m); as the output depends on those variables.
The way to call a function is funtion_name ( arguments );. What you do there is declaring an integer.
In your hourmin function you will have to manipulate the h and m parameters to get the output (you know how). note that even if you name your variables in one function (hourmin) the same as in another (main) they are not the same ones. They can be copies if you pass the right parameters when you call hourmin though.
ahhhh ok, well another quick question, i know the function format(just gettin a bit confused) but dont you put the return type in front of the function name then the parameters inside the parentheses, so would i just do the format of "void function name(parameters/arguments)", also im assuming that return 0 denotes that the main function returns nothing, so is that the same as stating that the functions within the main function are void or return nothing? And im assuming in the case of the hour:min question, that when i put a comma in between "int hour,min" in the function definition outside of the main function, that the compiler understands those are 2 different variables? And again, thanks for the help guys!
whoops. forgot to write the return type. return 0; returns a 0 - an integer value. that's not nothing. if you want to return from a void function before you've reached '}', you can write return;.
yes, in int hour, min; hour and min are independent from each other. my point was that when you have
1 2 3 4 5 6 7
void f(){
int i;
}
int main(){
int i;//this i is not related to the one in f()
return 0;
}