0

Suppose I'm running a loop and the loop should continue unless I get a garbage value.
So what will be the terminal condition. How can garbage value be checked in if condition?

4
Contributors
10
Replies
34
Views
4 Years
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • You can't define what is a garbage value any more than you can write down all the wrong answers to a question. You decide what constitutes a correct value and try to define a pattern that matches all correct values. Anything else is garbage. A pattern for an integer might … Read More

0

Then you need to figure out what the heck a "garbage" value is before you can tell the computer how to recognize one. You can't write code to do something you yourself are unable to do manually.

0

So it's impossible to find out the garbage value as in dynamic arrays on paper?

0

The word "garbage" is meaningless outside the context of your problem domain. You need to define what it means to your program. For example, if the domain of the program is to read numbers from 0 to 10, then anything that's not a number and any number outside the range of 0-10 constitutes garbage.

So...what input does your program expect, and what input falls outside of that expectation? You want to identify and allow only the expected input and treat everything else as garbage.

0

Then checking for garbage values is as simple as this:

int value;

if (!(cin >> value)) {
    /*
        The stream begins with garbage
    */

    // Restore the stream to a good state
    cin.clear();

    // Clear the entire line since you don't know where the garbage ends
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

Formatted input will validate the contents of the stream for you, and probably far more completely than if you tried to validate integers manually.

0

is there any pattern to find out garbage value????????
this is our homework today ,i think no, there is not any possible fixed pattern but our sir clamied it ....

0

is there any pattern to find out garbage value????????

The term "garbage" is meaningless without context. What's garbage for one program could be perfectly valid for another program. So step 1 is to clearly and unambiguously define what garbage means to your program. Once that's done, you can absolutely create a pattern matcher to detect legit values and everything else is garbage.

1

You can't define what is a garbage value any more than you can write down all the wrong answers to a question. You decide what constitutes a correct value and try to define a pattern that matches all correct values. Anything else is garbage. A pattern for an integer might be "an optional leading plus or minus sign followed by any number of digits." If you can't handle an arbitrarily large number of digits then your pattern becomes "an optional leading plus or minus sign followed by (as an example) ont to nine digits."

There are web sites that can help you with the patterns, such as regexpr and regular expression tester.

Votes + Comments
I get it. Only right answers, everything else is garbage (except in politics where all is garbage.)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.