my code has got some bugs and dont know how to repair!

Hello there! I was creating a timer and i ran into some problems (well my code has two bugs)
heres the code:

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
#include <iostream>
#include <ctime>
#include "windows.h"
using namespace std;

int Checker(){ /*retry or end program function that checks that user is only using 1 and 2 when */
int o=0;  // user input int
int d=0;  // int that gets returned after user enters 1 or 2
for(;;){
    system("CLS");
    cout << "would you like to try again?"<< endl;
    cout << "1.yes!" << endl;
    cout << "2.no!" << endl;
    cout << "please answer using 1 and 2! << endl;
    cin >> o;
    switch(o){
    case 1:
    return d;
    case 2:
    d=1;
    return d;
    }
    cout << "please use only numbers 1 and 2" << endl;
Sleep(1000);
}

}


int main()
{

for(;;)
{
system("CLS");
cout << "press enter to start the timer!" << endl;
cin.get();  // start the timer!
    int x = time(0);  // gets the starting time using time();
    system("CLS");
    cout << "press enter to stop the timer!" << endl;  //stop the timer!
cin.get();
    int y = time(0);  //stopping time using time();
    system("CLS");
    int c = y-x;  // time that has passed on seconds...
    int a=c/60;   // minutes that have passed....
    int b= a/60;  // hours that have passed...

    if(a>0&&b==0){   // if timer has been running full minute
        c=c-60*a;    // seconds minus 60* how many minutes have passed
    cout << "Time that has passed 00:" << a << ":" << c << endl;

    }

    else if(a==0&&b==0){  // if only seconds have passed..
    cout << "Time that has passed 00:00:" << c << endl;
    }

    else if(b>0){ // if hours  have passed....
        a=a-60*b;  // minutes minus 60* how many hours have passed...
        c=c-60*a;  // you got the idea already....
    cout << "Time that has passed" << b << ":" << a << ":" << c << endl;
    }

    Sleep(1000);
    system("CLS");
    int e=Checker(); /* calls the function that takes user input and checks that it is 1 or 2!*/
    if(e == 1) // if user wants to quit the program
    {
     break;
    }


}
} 



bug 1== when at the end of the program program ask the user if he wants to continue and user input is 1 and then the user presses enter the program will start the timer even if user diden't press enter second time to start the timer


bug 2== using a letter at the menu that ask you if you want to quit the program will cause a endless loop


if you could tell me how to fix these bug i would be happy!
thank you !
bug 2 is probably because you don't account for letters. you're expecting 1 or 2, but do you have an else (or default if you're using switch statements) to account for all other input? If not, this could do all sorts of weird stuff.
thanks NerdTastic I have never heard about switch statement defaults (or then i have forgotten them I need to check my c++ guide....) I will mayby solve bug 1 by using system("PAUSE >NUL"); for now...
When you cin a number, and type in a non-number, you break the cin object.

You could declare o as a char, and then have your switch case be '1' instead of 1.

Or you could fix the cin object. We do:
1
2
3
4
5
6
7
8
9
int i;

while (!(cin >> i)){  // extract integer at the same time as checking to see if it broke
  cin.clear();  // clear the contents of the cin object
  cin.ignore(80, '\n');  // ignore the rest of the data in the buffer.
                          // People use more scientific numbers than 80, but this should suffice
  cout << "Numbers only please, try again. ";
}
cin.ignore(80, '\n');  // make sure to clear the buffer if the cin didn't break 


We make a function out of this, and then also functions to ensure that the number is an integer or double, or even within a range we want.
Last edited on
what does the 80, \n of ignore do? I've always just left it empty and its worked fine for me.
Specifies the maximum number of chars to consume if '\n' is not reached.
Topic archived. No new replies allowed.