Trouble with input

so I've been trying to enter in hours and minutes in a HH:MM format, but crashes and I've been trying to find out why this is the case. Here's my code for reference if you need it. Any help is much appreciated.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 #include <iostream>
#include <string>
#include <cmath>
using namespace std;

void input(int & min, int & hours, int & wait_hours, int & wait_min);
void calc(int & total_hours, int & hours, int & min, int & wait_hours, int & wait_min, int & total_min);
void output(int & total_hours, int & total_min, char & anwser);



int main()
{
	int min, hours, wait_hours, wait_min, total_hours, total_min;
	char anwser;
	do {
		input(min, hours, wait_hours, wait_min);
		calc(total_hours, hours, min, wait_hours, wait_min, total_min);
		output(total_min, total_hours, anwser);
	} while (toupper(anwser) == 'Y');
}

void input(int & min, int & hours, int & wait_hours, int & wait_min)
{
		cout << "Compute completion time from current time and waiting period" << endl;
		cout << "Enter 24 hour time in the format of HH : MM" << endl;
		cin >> hours >> min;
		cout << "Enter in a waiting time in the format of HH : MM" << endl;
		cin >> wait_hours >> wait_min;
	
}

void calc(int & total_hours, int & hours, int & min, int & wait_hours, int & wait_min, int & total_min)
{
	
		total_hours = hours + wait_hours;
		total_min = min + wait_min;
		if (total_hours > 24)
		{
			total_hours = total_hours - 24;
		}
		else if (total_min > 60)
		{
			total_min = total_min - 60;
		}
		if (hours > 24 || min > 60)
		{
			cout << "Error!" << endl;
			abort();
		}
	

}

void output(int & total_hours, int & total_min, char & anwser)
{
	
		cout << "Completion time in 24 hour format is: " << total_hours << " : " << total_min << endl;
		cout << "would you like to do this this again? Press Y for yes or N for no" << endl;
		cin >> anwser;
		
}
Last edited on
Instead of using std::cin you could use <string>'s std::getline().

http://www.cplusplus.com/reference/string/string/getline/

Once you get your "MM:HH" format you can parse the string for the hours and minutes, converting the substrings into integers using <string>'s std::stoi() function.

http://www.cplusplus.com/reference/string/stoi/
I feel like I'm on to something,but I run into more problems then before this way.
The main problem with using std::cin to input your time format into two ints is when the user enters something like "12 : 24," as you requested they do so.

Your hours variable is properly filled with 12. Now std::cin "sees" the colon as the next thing to extract. ":" is not an int, so trying to stuff it into an int variable causes std::cin to enter an error state. Until you clear the input stream's buffer AND clear the error bit std::cin will keep on failing.

I wish your teacher will introduce structs soon!
To solve your issue, you could also try adding a char which get rid of the ‘:’ character.
Anyway, I think there’s something wrong in your output: shouldn’t “Completion time in 24 hour format is: 28:4” in the output below become: “Completion time in 24 hour format is: 25:58”?
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cmath>
#include <iostream>
#include <string>


void input(int& min, int& hours, int& wait_hours, int& wait_min);

void calc(int& total_hours,
          int& hours,
          int& min,
          int& wait_hours,
          int& wait_min,
          int& total_min);

void output(int& total_hours, int & total_min, char & anwser);



int main()
{
    char anwser;
    do {
        int min, hours, wait_hours, wait_min, total_hours, total_min;
        input(min, hours, wait_hours, wait_min);
        calc(total_hours, hours, min, wait_hours, wait_min, total_min);
        output(total_min, total_hours, anwser);
    } while (toupper(anwser) == 'Y');
}


void input(int& min, int& hours, int& wait_hours, int& wait_min)
{
    std::cout << "Compute completion time from current time and waiting period"
                 "\nEnter 24 hour time in the format of HH:MM: ";
    char c;
    std::cin >> hours >> c >> min;
    std::cout << "Enter in a waiting time in the format of HH:MM: ";
    std::cin >> wait_hours >> c >> wait_min;
}


void calc(int& total_hours,
          int& hours,
          int& min,
          int& wait_hours,
          int& wait_min,
          int& total_min)
{
    total_hours = hours + wait_hours;
    total_min = min + wait_min;
    if (total_hours > 24)
    {
        total_hours -= 24;
    }
    else if (total_min > 60)
    {
        total_min -= 60;
    }
    if (hours > 24 || min > 60)
    {
        std::cout << "Error!\n";
        std::abort();   // std::exit() ?
    }
}

void output(int& total_hours, int& total_min, char& anwser)
{
    std::cout << "Completion time in 24 hour format is: "
              << total_hours << ":" << total_min
              << "\nWould you like to do this this again? Press Y for yes or "
                 "N for no: ";
    std::cin >> anwser;
}


Output:
Compute completion time from current time and waiting period
Enter 24 hour time in the format of HH:MM: 13:15
Enter in a waiting time in the format of HH:MM: 15:13
Completion time in 24 hour format is: 28:4
Would you like to do this this again? Press Y for yes or N for no: n

Calc isn't quite right. When total_min > 60 you subtract 60 from total_min, but you also need to add 1 to total_hours. And since the total_min calculation affects the total_hours, total_min should be computed first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void calc(int& total_hours,
          int& hours,
          int& min,
          int& wait_hours,
          int& wait_min,
          int& total_min)
{
    total_hours = hours + wait_hours;
    total_min = min + wait_min;
    if (total_min > 60) {
        total_min -= 60;
        ++total_hours;
    }
    if (total_hours > 24) {
        total_hours -= 24;
    }
    if (hours > 24 || min > 60) {
        std::cout << "Error!\n";
        std::abort();   // std::exit() ?
    }

}
Topic archived. No new replies allowed.