Beginner: questions about flushing cin

I don't know a whole lot of clearing the cin buffer so it could be a noob misstake, anyway:

I'm doing an assignment to learn C++ so if you wonder why you'd wanna input
the time of day in seconds and not use strings to prevent faulty input, I'm
not supposed to.

anyway to prevent the program going in to a constant loop if something wrong is
entered I tried to flush cin with cin.clear and cin.ignore.
But after I've entered a wrong input all inputs are flagged as wrong

(also if I try cin.fail() or try something like "char check; IF input>>check" then all inputs will be flagged as wrong probably because I'm a noob)

So why isn't cin being cleared?

please teach me the ways!

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
 #include <iostream>
#include <limits>

//convert seconds in to HH/MM/SS format
double SecToH(double sec, double &hours, double &minutes, double &seconds){

    int temp;

    hours = sec/3600;

    temp= hours;
    minutes = (hours-temp)*60;

    temp = minutes;
    seconds = ((minutes-temp)*60)+0.5;

}


int main(){

    double h, m, s;
    const int daySec = 86400;
    int startNumber, winner=daySec, raceTime, winNr;
    bool inputCorrect;


    //Program starts
    std::cout<<"Welcome.\nEnter the Start number of each participant and their Start and End Times."<<std::endl;
    std::cout<<"To END program ENTER 0 as Start number."<<std::endl;

        do{

            //ENTER start number
            std::cout<< "\n"<< "ENTER Start number: "<<std::endl;
            std::cin>>startNumber;


            //End program at "0" input
            if(startNumber != 0){

                //ENTER start time
                long startTime;
                long endTime = endTime + startTime;

                std::cout<<"ENTER Start time and End time "<<std::endl;
                std::cin>>startTime;

                //check if Start time is within realistic values
                if(startTime < daySec ){

                    //Print start Time
                    SecToH(startTime,h,m,s);
                    std::cout<< "Start time: " << int(h) << ":" << int(m) << ":" << int(s) <<std::endl;

                    //Input end time
                    std::cin>>endTime;

                    //check for wrong input
                    if(endTime > daySec*2 || endTime < 1 || endTime < startTime){
                        inputCorrect = false;
                    }

                    //length of race, store fastest time
                    raceTime = endTime-startTime;
                    if(raceTime<winner){
                        winner=raceTime;
                    }

                    //save Start number of fastest time if 0 has not been input
                    if(raceTime<winner || startNumber != 0){
                        winNr=startNumber;
                    }

                    // check if lap passes 24:00 hours
                    if(endTime > daySec){

                        endTime = endTime-daySec;
                    }

                    //if input correct
                    if(inputCorrect != false ){

                        //Print end time
                        SecToH(endTime,h,m,s);
                        std::cout<< "Finnish time: " << int(h) << ":" << int(m) << ":" << int(s) << std::endl;


                        //if End time input is wrong
                    }   else{
                            std::cin.clear();
                            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                            std::cout<< "Invalid Input try again"<< std::endl;


                        }
                }   else{
                        //if start input is wrong
                        std::cout<< "ENTER a realistic Start time"<<std::endl;

                   }
                }   else{
                        //winners time and Start number
                        SecToH(winner,h,m,s);
                        std::cout<< "Winner is! "<<winNr<<std::endl;
                        std::cout<< "Time: "<< int(h) << ":" << int(m) << ":" << int(s) <<std::endl;
                    }

        }while(startNumber!=0);

    //keep program from closing once 0 is input
    std::cin.get();
  return 0;
}
closed account (48T7M4Gy)
1
2
3
4
5
std::cin >> x;
...
   std::cin.clear();
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   std::cout<< "Invalid Input try again"<< std::endl;


You need the above as a cycle for each input of hours, minutes then seconds. ie on 3 occasions, not just once.

You'll also find it's easier to separate out input stage from output results. ie input data -> process it -> output results, don't mix them.
Last edited on
Well there's only one input and that is in seconds which then converts in to a HH/MM/SS format
but should I do it for all inputs as in: startNumber, startTime, endTime?

also not sure what you meant by the last thing I'm very new!
closed account (48T7M4Gy)
OK, I see what you mean. But there are actually 2 inputs - one for start and another for end.

So that means, just concentarte on getting input right:

INPUT STAGE

cin >> start
clear
ignore
until it's acceptable

then

cin >> end
clear
ignore
until that's acceptable


PROCESS STAGE
Now, once that's been done, go ahead and process the two times.

OUTPUT STAGE
Then output the results
:)
thanks! I tried this before but it didn't work but it made me realize what was wrong!

if input was wrong I set bool inputCorrect = false but I never reset it

so even though you were right and I tried that before the flag would still be there

cheers I think I've got it now!
Topic archived. No new replies allowed.