combine inputs

How to combine the if statements?
cout<<"Enter a sale number"<<endl;
cin>>rec.sale;

if(cin.fail())
{
cout<<"Enter a number"<<endl;
cin>>rec.sale;
}
if(rec.sale<0)
{
cout<<"Re-enter a number: \n";
cin>>rec.sale;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout<<"Enter a sale number"<<endl;
bool valid_input = false;
while ( !( valid_input ) ) {
  if ( !( cin >> rec.sale ) ) {
    cout << "Please enter a number: ";
    cin.clear();
    cin.ignore( 256, '\n' );
  }
  else {
    if ( rec.sale < 0 )
      cout << "Number should be >= 0, please re-enter: ";
    else
      valid_input = true;
  }
}

Last edited on
1
2
3
4
5
6
7
8
9
#include <limits>
#include <iostream>
std::cout<<"\nEnter a number: ";
if ( !(std::cin >> rec.sale) || rec.sale < 0 )
{
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   std::cout << "\nRe-enter the number: ";
   std::cin >> rec.sale;
}


EDIT:
Corrections
Last edited on
what does cin>>rec mean??? I am not familiar with the >>sign.
>> is the extraction operator. When we write std::cin >> rec ; , the value from the input stream buffer std::cin is extracted and stored in the variable rec.

Similarly, << is the insertion operator. When we write std::cout << rec ; , the value of rec is inserted to the output stream buffer std::cout.
@Smac89

IMHO !(std::cin >> rec.sale) and (rec.sale < 0) are two different conditions of bad input and need to be handled differently.

For the former case cin flags need to be cleared and bad input needs to be ignored before reading further input.
This does not need to be done for the latter case.
IMHO !(std::cin >> rec.sale) and (rec.sale < 0) are two different conditions of bad input and need to be handled differently.


Except that they don't need to be handled differently. They could be, of course, but you lose nothing by handling them in exactly the same manner. And, of course, there's nothing stopping one from differentiating between the two within the body of the loop and handling them differently.

(And, by treating them the same way, we handle the input "-1asdfasdf" in only one iteration of the loop.)
Last edited on
My doubt is that if the user enters a negative number, then the value would be extracted to the variable and cin buffer would be empty.

Now if we add the ignore statement, then wouldn't the next input get ignored?

EDIT: Oh I see, the newline character shall still remain in the buffer.
Last edited on
@abhishekm71

is there anything that can substitute if ( !( cin >> rec.sale ) )???

Also, where do I put the cin?
Last edited on
if ( !( cin >> rec.sale ) ) does two things:

First it executes cin >> rec.sale; (read from the input buffer)
If there is no error in the read operation, it outputs "true" otherwise it outputs "false". This output is then caught by our if statement.

If you want, you can separate these two statements.

>Also, where do I put the cin?
(I don't understand your question, you already have cin in the if statement)
My brain is not working well at night. Sorry for the second stupid question.
How to debug this?
It says redeclaration of "bool valid".
Because if I take out the "bool valid" on the second one. It will not allow me to input anything when I run it.

cout<<"Enter the sale";
bool valid=false;
while(!(valid))
{
if(!(cin>>rec.sale))
{
cout<<"Enter numbers \n";
cin.clear();
cin.ignore();
}
else
{
if(rec.sale<0)
cout<<"Enter positive number";
else
valid=true;
}
}
cout<<"Enter the cost: \n";
bool valid=false;
while(!(valid))
{
if(!(cin>>rec.cost))
{
cout<<" enter numbers \n";
cin.clear();
cin.ignore();
}
else
{
if(rec.cost<0)
cout<<"ente positive number";
else
valid=true;
}
}
Last edited on
You are declaring valid twice in your program.

instead of the second bool valid=false;, just remove the bool and write valid = false;.

Also it will help you and others to debug if you indent your code.
Thank you!!!!
Last edited on
Topic archived. No new replies allowed.