integer validation
Write a program that asks the user to enter two integers. Declare variables to store the integers entered by the user. Your program will perform the arithmetic operations addition, subtraction, multiplication and division on the integers entered. In addition you will use the modulus operator to determine the remainder of integer division. The results of each arithmetic operation will be output to the screen.
Typical output may look as follows:
Enter two integers: 5 3
5 + 3 = 8
5 3 = 2
5 * 3 = 15
5 / 3 = 1
5 % 3 = 2
and I want to take it further. I have allowed the program to display results if 0 is entered for the integer. However, I'm trying to create validation of the input. I want to validate that the input is integer and if it is a character or is NULL to display a message and prompt for a number to be entered. I hope that makes sense. I keep running into problems with the if statement though. no matter how I do it, it still allows any input to be enter. A hint would be appreciated.
#include <iostream> // include library
#include <string>
#if UNIX
#include "curs.h"
#else
#include "Wincurs.h"
#endif
int main()
{
// declaration of variables
int num1, num2;
char exit;
// Note to user about program
std::cout<<"This program will evaluate two numbers of your choice\n";
std::cout<<"in the form of addition, subtraction, multiplication and division\n\n";
do // do while exit is equal to (y)es
{
std::cout<<"Please enter your first number\n\n"; //prompt user
std::cin>>num1; //read first number
if(!(std::cout >> num1 || (std::cout ==NULL){
clr();
std::cout<<"You must enter numeric characters to continue";
std::cout<<"Please try again";
std::cin>>num1;
}
else{
std::cout<<"\nThank You\n";
}
std::cout<<"\nPlease enter your second number\n\n"; //prompt user
std::cin>>num2; // read second number
clr();
std::cout<<"Here are the results\n";
// do equations and print results to the screen
std::cout<<num1<<" + "<<num2<<" = "<<num1 + num2<<"\n";
std::cout<<num1<<" - "<<num2<<" = "<<num1 - num2<<"\n";
std::cout<<num1<<" * "<<num2<<" = "<<num1 * num2<<"\n";
// if statement to determine if 0 is entered because a
// number is not divisible by 0 therefore is undefined
if(num2==0)
{
std::cout<<num1<<" / "<<num2<<" = "<<"Undefined\n";
}
else
{
std::cout<<num1<<" / "<<num2<<" = "<<num1 / num2<<" with a remainder of "<<num1%num2<<"\n";
}
std::cout<<"\n";
std::cout<<"Do you want to enter new numbers? (y/n)\n"; //prompt user
std::cin>>exit; // get input
clr(); // clear screen
}while(exit=='y'); // end do while loop
clr(); // clear screen
std::cout<<"\nHave a Great Day\n\n";
return 0; // end program
} // end main()

