I have an issue with loop when I enter double valuse

#include <iostream>
#include <iomanip>
#include <windows.h>
#include<fstream>
using namespace std;

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void showmassage()
{
cin.clear();
fflush(stdin);

SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << endl << "\t\t\t" << "ERROR! Terrorist Attacted! Reeenter." << endl;

SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << "\t\t\t\t" << "Enter the correct value: ";
}
int main()
{
int P;
cout << "This program calculate poppulation change." << endl;

cout << "The starting size of the population: ";


while (!(cin >> P) || P <= 2)
{

showmassage();

}


cin.get();
return 0;
}


This grogram I try to enter non interger value in 2 or 4 time on value P.
it just keep escape the window. I dont know waht happen to it
any help please
thank you very much
Last edited on
Maybe this can help:
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
#include <iostream>
#include <iomanip>
#include <windows.h>
#include<fstream>
using namespace std;

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void showmassage()
{
	cin.clear();
	fflush(stdin);

	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
	cout << endl << "\t\t\t" << "ERROR! Terrorist Attacted! Reeenter." << endl;

	SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
	cout << "\t\t\t\t" << "Enter the correct value: ";
}
int main()
{
	int P = 0;
	cout << "This program calculate population change." << endl;

	cout << "The starting size of the population: ";

	//some changes, simple and not to elegant but it works
	while (P <= 2)
	{
		cin >> P;
		if(P<=2)
			showmassage();

	}
	

	cin.get();
	return 0;
}

If You need something more sophisticated, i can try.
See ya!
Topic archived. No new replies allowed.