Is there a way to detect garbage value?

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?
You need to give more details. What type of data is this, e.g. string, char, int, double, float etc? And what values would you consider to be garbage?
I'm using a dynamic array of int type.
Last edited on
That is still not enough information. What dataype is the array? And what are the valid values?
int type. What do you mean by valid values?
User can enter any integer.
I'm talking about, the value -345468667 something. How to find this value without running program. This is the garbage value.
1
2
3
4
5
6
7
int input;
while (true)
{
    cin >> input;
    if (input == -345468667)
        break;
}


or maybe:

1
2
3
4
5
int value;
while (value != -345468667)
{

}
Last edited on
My question is, what if we don't know the garbage value, then can we find out the garbage value without running and printing through program.
Dude, you need to explicity define what you consider as valuable information.

What information do you really consider to be valid, so that you can discard the rest as garbage?

For example, if I want my user to specify the no. of humans living on earth, then I can assume for a fact that it will be between 5 billion to 10 billion for this year, taking into account something drastic also happens (nuke, sudden overgrowth).

So, any other value for me is garbage, which I can discard.

On a side note,
I think you're using the wrong language here wiseRehan.
what if we don't know the garbage value

If you don't know the exact value, then you could specify some rules which define what is garbage, for example a negative number might be considered garbage - but that decision is up to you.

can we find out the garbage value without running and printing through program
Sorry, I don't understand the question, if the program is not running, then no values exist at all, there is nothing to look at.

Last edited on
Thanks, nice way to some extent.

What do you mean by wrong language?
I'm talking politely, I think.
The problem you're facing implies that you're doing something very odd with your code. For example, overflow: when I use a very long loop and increase the value of an integer i beyond INT_MAX then it automatically becomes ...-INT_MAX-1. C++ doesn't detect this automatically, you need to take this into account explicitly.

It's not really a problem, because if this happens then that means something in your code is not functioning properly. There are other high level languages available that detect this automatically. I believe Ada is one example.
Last edited on
Indeed, I've been asking "What is the garbage?" but maybe the better question is "Why is there garbage?"

Lots of reasons. Uninitialised variable. Uninitialised pointer. Array index out of bounds. Numeric overflow. User input incorrect, etc.

You need to look at the source code to see what is the root cause. Or post the code which gives the problem here in the forum.
Last edited on
Topic archived. No new replies allowed.