Hey guys as part of my c++ course, I have been asked to do the following:
The code i have written works, although if the user desides to input a floating point number, the program will go into a loop. I was just wandering could some kind soul please explain this to me.
The code below contains the question as well:
//14. Write a program to allow the user to enter an integer value, but ensure
//that only an even number is entered.
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter an even integer value.";
int number, temp;
do
{
cin >> temp;
if (temp%2 == 0)
number = temp;
else
cout << "I am sorry but you have to enter an even integer number. Please enter an integer number.";
}
while (temp%2 !=0);
return 0;
}
The code i have written works, although if the user desides to input a floating point number, the program will go into a loop. I was just wandering could some kind soul please explain this to me.
The code below contains the question as well:
//14. Write a program to allow the user to enter an integer value, but ensure
//that only an even number is entered.
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter an even integer value.";
int number, temp;
do
{
cin >> temp;
if (temp%2 == 0)
number = temp;
else
cout << "I am sorry but you have to enter an even integer number. Please enter an integer number.";
}
while (temp%2 !=0);
return 0;
}