weird results

It's me agian, with another noob problem..=( but I just can't seem to figure it out.
This time I am writing code for a program that is suppose to have 2 functions.

1)is to intake the time in minutes and seconds from the operator called ReadTime()
2)is to rewrite the time in the format of hour:min:second called DisplayTime()

however my time displayed by DisplayTime() will be 124421323:1213245:1, or something ridiculous


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

//---------------------------------------------------
int ReadTime(int min, int sec)
{
    int hour;
    cout << "Enter minutes: ";
    cin >> min;
    cout << endl;
    cout << "Enter seconds: ";
    cin >> sec;
    cout << endl;
    
    hour = (min / 60);
    min = (min % 60) + (sec / 60);
    sec = (sec &60);
    
    return(hour, min, sec);
}
//----------------------------------------------------
int DisplayTime (int hour, int min, int sec)
{
    setfill ('0');
    cout << setw(2) << hour << ":" << setw(2) << min << ":" << setw(2) << sec << endl;
}
//-----------------------------------------------------



int main(int argc, char *argv[])
{
    int hour, min, sec;
    cout << "Enter a time: " << endl;
    ReadTime(min, sec);
    
    cout << "Your time was: ";
    DisplayTime(hour, min, sec);
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



You appear to be trying to return more than one value from a function, which can't be done. Search the site documentation for pointers or references.
I think this is what you want to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ReadTime(int& hour, int& min, int& sec)
{
    int hour;
    cout << "Enter minutes: ";
    cin >> min;
    cout << endl;
    cout << "Enter seconds: ";
    cin >> sec;
    cout << endl;
    
    //These instructions set the values of the original variables passed as arguments in this function, because a reference (&) operator is specified.
    hour = (min / 60);
    min = (min % 60) + (sec / 60);
    sec = (sec &60);
}

Hi there. what Mr helios said is true.. but you can use two or more values if you use them as reference.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void input(int& i, int& k) { /*pass it by reference then you can use two values at one go*/
     cout<<"Enter minutes:";
     cin>>i;
     cout<<endl<<"Enter seconds:";
     cin>>k;
     cout<<endl;
     }


int Display() {
    int min, sec,hour;
    input(min, sec);
    hour= (min/60);
    min=(min % 60) + (sec / 60);
    cout<<"your time was "<<":"<<hour<<"hrs :"<<min<<"mins : & "<<sec<<"secs"<<endl;
    return 0;
}

int main()
{
    Display();
    system("pause");
    return 0;
}


Sorry i did not create a better maths algo for the calculations... you do the rest... ('',).
thx for all of the replys,

but for "magnificence's" reply, if you have three reference parameters in the function, does that mean when calling the function in int main you have to attribute an value to "int &hour?" or can you just leave it blank?''

UPDATE: o nvm, it can just return the results for the display()
Last edited on
Topic archived. No new replies allowed.