My program isnt looping

Hey, I am a 14 year old beginner programmer and while learning about C++, I decided I'll make a program where you have to input:
the hours the program has to count up to,
the minutes the program has to count up to,
the seconds it has to count up to
and finally, you have to write yes to start the while(){ loop.

However, when I type in my hrs, mins, secs and type in yes, the program doesn't do anything. It doesn't even close, it's as if it was at the end and was paused. Nothing comes up, there are no runtime errors and there are no bugs when I compile my code.
This is my code so far:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int second = 60;			//Maximum
int minute = 60 * second;	//Maximum
int hour = 24 * minute;		//Maximum

int hd = 0;				//How many hours passed
int md = 0;				//How many minutes passed
int sd = 0;				//How many seconds passed

int x = 0;				//Current hour
int y = 0;				//Current minute
int z = 0;				//Current second

int a = -1;				//Memory
int b = -1;				//Memory
int c = -1;				//Memory

int dh = 1;                       //Target hours reached
int dm = 1;                      //Target minutes reached
int ds = 1;                       //Target seconds reached

//string reason;		//declaring reason
string yes;				//declaring starting string


int main(){
    
//    string reason = " ";	//reason set to nothing
    string yes = " ";		//starting up string set to nothing
    
    cout << " " << endl;
    cout << "Welcome to Milan's count up program." << endl;
    cout << " " << endl;
    cout << " " << endl;
    cout << "Enter the number of hours to count up to: " << endl;
    cin >> a;
    cout << "You have chosen " << a << " hours." << endl;
    
    cout << "Enter the number of minutes to count up to: " << endl;
    cin >> b;
    cout << "You have chosen " << b << " minutes." << endl;
    
    cout << "Enter the number of sec to count up to: " << endl;
    cin >> c;
    cout << "You have chosen " << c << " seconds." << endl;
    
//    cout << "Enter the message you want to be sent when the time is met" << endl;
//    cin >> reason;
    
    cout << "Are you ready?" << endl;
    cin >> yes;
    

    while(yes == "yes"){
           
           if(a>0){
                   int y = 0;
                   x++;
                   if(x == a){
                        
                        int hd = 1;

                        cout << a << "hours have past." << endl;

                        }
              }
              else{
                   
                   if(hd == 1){
                         if(dh == 1){
                               cout << a << "hours have past." << endl;
                               int dh = 0;
                               }
                         }
                   
                   }
           
           if(b>0){
                   int z = 0;
                   y++;
		  	 if(hd == 1){

                   		if(y == a){
                        
                       		   int md = 1;
                        
				   cout << b << "minutes have past." << endl;

                        	   }
          	                 else{
                   
                                   if(hd == 1){
                                         if(dm == 1){
                                                cout << b << "minutes have past." << endl;
                                                int dm = 0;
                                                }
                                   }
                   
                   }
		           }
              }
           
           if(c>0){
		   Sleep(1000);
                   z++;
		    if(md == 1){

                      if(z == a){
                           
                           int sd = 1;
                           
			   cout << c << "seconds have past." << endl;
			   
			   string yes = "done";

                           }
			}



           }
           


      }
    
//    cout << reason << endl;
    Sleep(5000);
    cout << "Thank you for using Milan's count up program !" << endl;
    cout << " " << endl;
    cout << " " << endl;
    
    system("pause");


return 0;

}


Any help will be appreciated. However, keep in mind that even if this code might not be perfect, etc, I am not planning on using something like arrays until I get it working or if it is a must.

Cheers,
Milan
Last edited on
Oops, I realized that I've left some coding out, I am going to add that now and post an update once its finished :)
There maybe left-over characters in the input buffer from previous input operations. If this truly is the case,
cin >> yes will extract the trailing characters, but not the characters you want. As a result, the loop never starts.

Try clearing the input buffer (after each extraction) with this:

MinGW: std::cin.ignore(std::streamsize, '\n');
MSC: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Note that in MinGW's latest compiler, std::streamsize is equivalent to LONG_MAX.

Wazzak
Last edited on
Hey, in fact, I am using Dev C++, I find that the easiest. I managed to start the loop and in fact, I reckon the loop DID start but didn't print anything because of my newbie coding :P eg:

in the minute paragraph,
1
2
3
           if(b>0){
                   int z = 0;     //<-- this line sets seconds to 0 but doesn't set x to x + 1
                   y++;


and similar mistakes. However, what it does now is, say I say I want it to count to 0 hours, 1 minute and 12 seconds, it keeps printing "0 hours have past." even though there is no reason for it to do so. Let me explain:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

//int second = 0;	        //Maximum
//int minute = 0;	        //Maximum
//int hour = 0;	        //Maximum

int hd = 0;				//Target hours reached
int md = 0;				//Target mins reached
int sd = 0;				//Target secs reached

int x = 0;				//Current hour
int y = 0;				//Current minute
int z = 0;				//Current second

int a = 0;				//Memory
int b = 0;				//Memory
int c = 0;				//Memory

int dh = 1;             //Limits the amount of times the program should output that hours are 
                             //done
int dm = 1;            //Limits the amount of times the program should output that hours are 
                             //done
int ds = 1;             //Limits the amount of times the program should output that hours are 
                             //done

//string reason;		//declaring reason
string yes;				//declaring starting string


int main(){
    
//    string reason = " ";	//reason set to nothing
    string yes = " ";		//starting up string set to nothing
    
    cout << " " << endl;
    cout << "Welcome to Milan's count up program." << endl;
    cout << " " << endl;
    cout << " " << endl;
    cout << "Enter the number of hours to count up to: " << endl;
    cin >> a;
    cout << "You have chosen " << a << " hours." << endl;
    
    cout << "Enter the number of minutes to count up to: " << endl;
    cin >> b;
    cout << "You have chosen " << b << " minutes." << endl;
    
    cout << "Enter the number of sec to count up to: " << endl;
    cin >> c;
    cout << "You have chosen " << c << " seconds." << endl;
    
//    cout << "Enter the message you want to be sent when the time is met" << endl;
//    cin >> reason;
    
    cout << "Are you ready?" << endl;
    cin >> yes;
    while(yes != "yes"){
              cout << "Sorry, I don't understand your answer." << endl;
              cout << "To continue, type in simply 'yes', preferably in lowercase lettering." << endl;
              cin >> yes;
              }
    

    while(yes == "yes"){
           
           if(a>0){

                   if(x == a){
                        
                        int hd = 1;
                        if(dh == 1){                                                       //<-- In the beginning, dh is 1
                               cout << a << " hours have past." << endl;
                               int dh = 0;                                                  //<-- Here, dh is set to 0
                               }                                                                      //therefore the loop should
              }                                                                                       //break.
              }
              else{
                   if(dh == 1){                                                          //<-- Same thing should happen
                         cout << a << " hours have past." << endl;           //here.
                         int dh = 0;
                         int hd = 1;
                   }

           
           if(yes == "yes"){
                   if(y == 60){
                             int x = x + 1;
                             
		  	                 if(hd == 1){

                   		           if(y == b){
                        
                       		        int md = 1;
                                    if(dm == 1)
                                             cout << b << " minutes have past." << endl;
                                          int dm = 0;
                                          }
                        	   }
                            }
                            int y = 0;
		           }
		           if(b == 0){
                                   if(hd == 1){
                                         if(dm == 1){
                                                cout << b << " minutes have past." << endl;
                                                int dm = 0;
                                                int md = 1;
                                                }
                                   }
                   
                   }
              }
           
           if(c>=0){
		   Sleep(1000);
           z++;
           
           if(z == 60){
                 int z = 0;
                 int y = 1 + y;
                 if(md == 1){
                       if(z == c){

                              int sd = 1;

                              cout << c << " seconds have past." << endl;

                              string yes = "done";

                       }
			}



           }
           


      }
      }
   }
//    cout << reason << endl;
//    Sleep(5000);
    cout << "Thank you for using Milan's count up program !" << endl;
    cout << " " << endl;
    cout << " " << endl;
    
    system("pause");


return 0;

}


And also, even if the 1 minute does pass, it doesn't print out it has, same goes for the seconds and no message comes up, nor does the "0 hours" printing stop, even though the while loop should be broken as yes would not be equal to "done", not "yes".
Cheers,
Milan
Last edited on
Welcome to C++!

This is not exactly related to the question, but...

afaik, DevC++ is not longer in maintenance.
http://www.jasonbadams.net/20081218/why-you-shouldnt-use-dev-c/
If you feel up to it, you should eventually migrate to a new IDE :) (Code::Blocks is good... but if you don't mind using the command line, use g++ along with Notepad++)

Also, you should use more descriptive variable name (in future use)... a, b, and c are not very descriptive. Maybe use something like.. input_hours, input_minutes, input_seconds.. and likewise to your other variables :)
Last edited on
I'll keep that in mind but to make them more descriptive I described them after declaring them :)
if you don't understand what they are there for, feel free to OK. However, it would help if you gave a clue as to how to solve my problem as well but thanks anyway :) unless switching to, say, Code::Blocks will help/resolve the problems :P
Last edited on
I talked with my parents for the last half an hour and got them to buy me VS. I hope that will be of some use, it should arrive tomorrow so it shouldn't be too long before I'll explore it :)
You know there is a free version? You could've tried that out, at least until your sure that you actually want to become a programmer, otherwise its wasted money.
Don't worry, I've done a lot of practice with other languages too, I've done some simple 3D game creating, graphic working, I've helped create a realistic 3D version of our school and have got a good enough computer to do just about anything. To top that, I get the highest grades in IT, do extra work, teach my IT teachers, chose IT as my #1 priority in GSCE, I do work for our IT faculty, I am deeply interested in programming and when I discovered about C++(only a couple of days, 1 week max) I was tempted to know atleast a bit after the basics. Plus, who said I am only going to do C++, with VS you can do much more, which is why I really wanted it for a longer period of time. :)
but... you could have got a 2 year old version for free O_O my mind is blown...
Last edited on
Don't worry about it now, it will arrive today or tomorrow at the latest so it doesn't matter. Just a quick check though, is there anyway that I could resolve my problem? Maybe using while loops instea dof if loops ? Not sure, the worst thing is that we don't have a C++ IDE in school therefore I can't actually try and fix it right now.
closed account (S6k9GNh0)
The problem with using cin stream operators is that they naturally leave the 0x0A or '\n' symbol at the end of the stream instead of discarding it. To prevent this, use getline from string. http://cplusplus.com/reference/string/getline/
Do yourself a favour and use meaningful names like

'hours_passed' instead of 'hd'
'current_hour' instead of 'x'

trust me that make things by far much clearer and is better to read

if you can't find a meaningful name then it's a strong hint that you're about to make a mistake
@computerquip thanks for the tip, I will read it as soon as I finish off my classwork :)

@coder777 , I will keep that in mind, I might change it as soon as I get home as well as making sure that the code is laid up more tidily. The reason why I used names like a,b,c, etc was because at the beginning I wasn't expecting 12 variables but only about 6. However, to make the names more descriptive I wrote the //meanings next to the line where I declared the integer.
Apart from the dodgy names, is there any reason as to why my program doesn't work properly? (refer to the post made at
Jan 30, 2012 at 6:38pm
)
Well, sorry without decent names it way to hard to see what's going on/what should happen.

I wasn't expecting 12 variables but only about 6
The amount doesn't matter. Even if it's just 1 variable. Always name them right.

What confuses me is this dh/dm/ds. I would have expected that a/b/c are the limits. So I don't know what your aim is.

Do you just want to count seconds? Then why don't calculate the seconds from the input? Like

sec_limit = a * 60 * 60 + b * 60 + c;
@coder777 -- I said the same thing earlier in the thread :P
The dh and dm and ds are there because of the problem I was having. As I mentioned, this was the program repeatedly outputting the same line. To avoid this, I wanted to limit the amount the program could output, say, " a << "hours have passed" ". To do this, I set the dh to 1 and made a loop so that everytime the program counted the hours AND dh was 1 it would output that the hours have passed and set dh to 0, therefore breaking the requirement.
However, this did not help and the same problem was occuring.
Right now I am at home, therefore I can change the variables' names to appropriate
names, such as the ones you suggested.

As for your second suggestion, all I can say is that the aim of the program was to count seconds, minutes AND hours. However, you gave me a good idea of what I might do to repair my program. At the moment it is just an idea and I haven't planned it out yet, therefore I can't give you the exact details yet, however, I will post an update soon.

So far, I thank you very much for being so very active and giving me ideas on how to improve my C++ skills.

Cheers,
Milan
Topic archived. No new replies allowed.