std::cin not resetting in loop

Feb 26, 2019 at 11:15pm
Whenever I enter an incorrect answer (main()) then it keeps looping and having this output without stopping to std::cin. Usually std::getline would fix this but I can't do that sense it's integers and not strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <math.h>

int Check();
void PrintOut(int Quotient, int Remainder);

double Minutes;

int main(){
	while(true){
		std::cout << "How many minutes?: ";
		std::cin >> Minutes;
		if(std::cin.fail()){
			std::cout << "Enter a number, try again.\n\n";
		}
		else{
			break;
		}
	}
	Check();
	return 0;
}


Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
How many minutes?
Enter a Number try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.
How many minutes? Enter a number, try again.


This will keep looping

Last edited on Feb 26, 2019 at 11:21pm
Feb 26, 2019 at 11:27pm
How are you clearing the error flag? Once the stream enters an error state it will stay in the error state until the error is cleared and any offending data is removed from the input buffer.

Feb 26, 2019 at 11:28pm
Maybe something like this?

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

int Check();
void PrintOut(int Quotient, int Remainder);

double Minutes;

int main(){
	while(true)
	{
	std::cout << "How many minutes?: ";
	std::cin >> Minutes;

	    if(std::cin.fail())
	    {
	    std::cout << "Enter a number, try again.\n\n";
	    std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
	    }
	    else
            {
	    break;
	    }
	}

	// Check();

return 0;
}


Not sure what the "Check()" function is meant to do as you haven't defined it, so commented out for now so it compiles.
Last edited on Feb 26, 2019 at 11:30pm
Feb 26, 2019 at 11:49pm
My full code is here (I finished thanks to your 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
#include <iostream>
#include <math.h>
#include <limits>

int Check();
void PrintOut(int Quotient, int Remainder);

double Minutes;

int main(){
	while(true)
	{
	std::cout << "How many minutes?: ";
	std::cin >> Minutes;
	    if(std::cin.fail())
	    {
	     std::cout << "Enter a number, try again.\n\n";
	     std::cin.clear();
             std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
	    }
	    else
            {
	    break;
	    }
	}
Check();
return 0;
}

int Check(){
	int Remainder;
	double Quotient;
	Remainder = remainder(-Minutes, -60);
	Quotient = Minutes / 60;
	PrintOut(Quotient, Remainder);
	return 0;
}

void PrintOut(int Quotient, int Remainder){
	if(Quotient == 1){
		std::cout << Quotient << " hour and " << Remainder << " minutes.";
	}
	else if(Quotient > 1){
		std::cout << Quotient << " hours and " << Remainder << " minutes.";
	}
	return;
}
Topic archived. No new replies allowed.