The following is my code. I am just getting started in C++ so there are few concepts I am still stuck on.

This program works well except for one issue – the data type is integer but it is not catching decimal numbers. In my other programs this worked but for some reason it is not working here and it’s frustrating because I don’t know why. Any advice / help would be appreciated.

// 2 integers - determine which of 2 INTEGERS is the largest or if they are equal

#include <iostream>							

using std::cout;							
using std::cin;										

int main ()								

{													
int integer1;							
int integer2;										

int remainder1;
int remainder2;

bool flag1 = false;						
bool flag2 = false;						


cout << "Enter the first integer: ";			
cin >> integer1;							


while ( flag1 == false)						
{
  if (!cin.fail())						
  {
     remainder1 = (integer1 % 1);

      if (remainder1 != 0)
     {
        cout << "\nError reading the number.";			
         cin.clear();
         cin.ignore(50, '\n');
         cout << "\nError cleared.";					

         cout << "\nRe-enter the first integer: ";				     
         cin >> integer1;						
        }
         else
         {
	flag1 = true;												}
          }
         else 
        {
		cout << "\nError reading the number.";					cin.clear();
		cin.ignore(50, '\n');
		cout << "\nError cleared.";							
		cout << "\nRe-enter the first integer: ";					cin >> integer1;					

		flag1 = false;										
          }
}
	
// check input for the second number to make sure it also does not fail (check if not a string) 

cout << "Enter the second integer: ";					
cin >> integer2;										

while ( flag2 == false)						
{
if (!cin.fail())							
{
           remainder2 = (integer2 % 1);

	if (remainder2 != 0)
	{
		cout << "\nError reading the number.";			
		cin.clear();
		cin.ignore(50, '\n');
		cout << "\nError cleared.";						
		cout << "\nRe-enter the second integer: ";					cin >> integer2;						}
	else
	{
		flag2 = true;						}
}
else 
               {
	cout << "\nError reading the number.";					cin.clear();
	cin.ignore(50, '\n');

                 cout << "\nError cleared.";					 
	cout << "\nRe-enter the second integer: ";			
	cin >> integer2;							flag2 = false;							}
}
	

// now calculate which integer is largest or whether they are equal

if (integer1 > integer2)
	cout << integer1 << " is larger.\n";

if (integer2 > integer1)
	cout << integer2 << " is larger.\n";

if (integer1 == integer2)
	cout << " These numbers are equal.\n";



return 0;

}

> the data type is integer but it is not catching decimal numbers
Can you give an example run of the program and point out where the actual results don't coincide with your expected results?

the data type is integer but it is not catching decimal numbers.

Unless I'm missing something, I think you answered your own question here...after all, integers are not real numbers or rational numbers (i.e. no decimals allowed). They are simply all the numbers in the set: {...-3, -2, -1, 0, 1, 2, 3...}. I.e. positive and negative whole numbers, including zero...

So you could change the data type from integer to float or double or what have you. But be careful....in your code you have "integer1 %1"...modulus works fine for integers, but is not defined for decimal numbers.

Also, please use code tags when posting code...

yes exactly so I don't understand how its allowing decimals.

See if I enter 2.5 the following displays:
Enter the second integer:
Error reading the number.
Erro cleared.
Re-enter the second integer:


To make matters worse, if the user then enters 2 in the second integer, it actually calculates and for some reason is truncating the 2.5 and saying that they are equal.

This is exactly what you should expect. C++ implicitly converts (or rather truncates) the decimal portion to the integer. It is equivalent to using a floor() function..

So if you try to stick 2.999999 into an integer variable, the variable will instead hold 2 -- all the decimal information is simply thrown away and lost.

In fact, you can explicitly tell C++ to do this via "casting". For example, if you had a decimal number, for example 3.14, and you wanted to store it into an integer variable i, you could do this:

i = (int)3.14

In fact, you can explicity tell C++ to convert any type using this technique.

In any case, your program won't throw an error if you enter a decimal number into an integer variable...you will just get unexpected results (if you don't realize what you are doing).

So instead, you will have to change the data type (from integer) to one that will store decimals (like I said above). And again, be wary of modulo...

This article has been dead for over six months. Start a new discussion instead.