***Why Won't += Work Here?***

Why won't += work here to complete the loop properly?

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 <iostream>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
int hh;
int mm;
int ss;
int pick;
int ss_choice;
int mm_choice;
int hh_choice;
int ss_hh;
int ss_mm;
int ss_ss;
int mm_hh;
int mm_mm;
int mm_ss;
int hh_hh;
int hh_mm; 
int hh_ss;

cout << "Give Me A Start Time Please:\n";
char sep;
cin  >> hh >> sep >> mm >> sep >> ss;
cout << "Choose An Increment Type Please:\n";
cout << "1. Seconds:\n";
cout << "2. Minutes:\n";
cout << "3. Hours:\n";
cin  >> pick;

if (pick==1) {
char sep;
cout << "How Many Seconds?:\n";
cin  >> ss_choice;
cout << "What's Your End Time?:\n";
cin  >> ss_hh >> sep >> ss_mm >> sep >> ss_ss;
cout << hh << ":" << mm << ":" << ss << "\n";
do {cout <<hh << ":" << mm << ":" << ss += ss_choice; // why won't this work!?
if (ss <= 60){mm + 1, ss=00;}
if (mm <= 60){hh + 1, mm=00, ss=00;} 
}
while (hh <= ss_hh);
return 0;
}
else if 
(pick==2)
{
char sep;
cout << "How Many Minutes?:\n";
cin  >> mm_choice;
cout << "What's Your End Time?:\n";
cin  >> mm_hh >> sep >> mm_mm >> sep >> mm_ss; // loop will be repeated once fixed.
return 0;
}

else if 
(pick==3)
{
char sep;
cout << "How Many Hours?:\n";
cin  >> mm_choice;
cout << "What's Your End Time?:\n";
cin  >> hh_hh >> sep >> hh_mm >> sep >> hh_ss; //loop will be repeated once fixed.
return 0;
}

return 0;
}
You might be confusing your compiler. When math is happening, the << operator can become a bitwise operator, and you're compiler has no idea how to bitwise an equation in this manner... That's a guess at this point, but I think you can use parenthesis to solve this.

cout <<hh << ":" << mm << ":" << (ss += ss_choice);

Lines 44 and 45 should be expanded into multiple lines, using semicolon instead of comma's. And += needs to be used instead of just +.

When taking information from cin using the >> operator, you don't need to take a "sep" or dummy-value between inputs, white-space is the separator by default. (Unless you're expecting the user to enter a colon between their time increments, in which case I guess that makes sense. You might want to google "parsing time from a string in c++")

Your prompts to the user could use a little more verbosity; how is someone who can't look at your code going to know that they need to enter 5 values in a specific order before the program continues? They might enter 30 thinking 30 seconds, and it looks like the program just hangs when in fact they just entered 30 into the hour variable and it's waiting for them to put in the separator...

I think your biggest problem is trying to do too much in a single line, break things up when you can and take it step-by-step. It will save you a lot of bugs in the long-run.

Keep at it and good luck!
Last edited on
Topic archived. No new replies allowed.