Reputation: 847
The problem is when the user input is = 9999999999 then the console is returning all the values. I want to limit this input by using simple else if statement if someone tried to put the value for a, b = 9999999999 then user must get the warning as I defined in my code. I can control it by using double instead of int but this is not a solution.
#include <iostream>
using namespace std;
main()
{
int a, b, c, d;
cout << "Enter Value for A: ";
cin >> a;
cout << "Enter Value for B: ";
cin >> b;
if (a > b)
{
cout << "A is Greater than B " << endl; //This will be printed only if the statement is True
}
else if ( a < b )
{
cout << "A is Less than B " << endl; //This will be printed if the statement is False
}
else if ( a, b > 999999999 )
{
cout << " The Value is filtered by autobot !not allowed " << endl; //This will be printed if the statement is going against the rules
}
else
{
cout << "Values are not given or Unknown " << endl; //This will be printed if the both statements are unknown
}
cout << "Enter Value for C: ";
cin >> c;
cout << "Enter Value for D: ";
cin >> d;
if (c < d)
{
cout << c << " < " << d << endl; //This will be printed if the statement is True
}
else if ( c > d )
{
cout << "C is Greater than D " << endl; //This will be printed if the statement is False
}
else
{
cout << c << " unknown " << d << endl; //This will be printed if the statement is Unknown
}
}
Upvotes: 5
Views: 2029
Reputation: 629
The maximum value of int is 2147483647 which is less than 9999999999. If you want to input 9999999999, you have to use a different data type.
Edit: I guess I have to give a more in depth explanation on why you have to use a different data type.
C++ is different from Java. In Java if you try to input 9999999999 to an int, it would throw an exception saying the number is too larger, and the program quits.
However, in C++, when you cin a number that is too large, cin would go into failure mode. In failure mode, cin would take the closest in-range value and silently ignores any further cin and leaving them as the initial value. Most of the time the initial value is 0; however, depending on your heap space, the variable may be assigned a garbage value. Hence your a would have an value of 2147484647 and b is left as its initialized value 0 or a random value. Thus your first if statement would evaluate to true and cout "A is Greater than B".
In short, the maximum value of int is 2147483647, so if you want to input 9999999999, you have to use a different data type.
Upvotes: 2
Reputation: 10020
Here's an example function which ensures valid input. I've made it take in a min and max value too, but you don't necessarily need them, since 999999999 is going to be out of the range of int
anyway, which causes cin
to fail. It will wait for valid input though.
int getNum( int min, int max ) {
int val;
while ( !( cin >> val ) || val < min || val > max ) {
if ( !cin ) {
cin.clear();
cin.ignore( 1000, '\n' );
}
cout << "You entered an invalid number\n";
}
return val;
}
int main() {
int a = getNum( 0, 12321 );
}
EDIT: Well, now you just modified your question to have using simple else if statement, changing the answers. The answer then, is simply no. Since cin
would fail, you can't realistically do a check. Also, it would never be true since 999999999
is greater than INT_MAX
on any implementation I can think of.
Upvotes: 7
Reputation: 234635
if (std::min(a, b) > 999999999)
is one fix, although if (a > 999999999)
would work in your case due to the way you've arrange your if
block. Take care though to check that 999999999
is within the range of an int
- the C++ standard does not require it to be. The expression
if (a, b > 999999999)
is equivalent to
if (b > 999999999)
with, formally, a
being evaluated but the result discarded.
Upvotes: 4
Reputation: 532
You can check the input value and if it exceeds the predefined limit request for another input
int limit = 99999;
int a, b;
cout << "Input a : ";
cin >> a;
while(a>limit){
cout << "Input exceeds limit reinput a : ";
cin >> a;
}
cout << "Input b : ";
cin >> b;
while(b>limit){
cout << "Input exceeds limit reinput b : ";
cin >> b;
}
Upvotes: 2