how to make a default message for wrong input

Pages: 12
i dont get it when i enter a character it just gives me a long number,
why wont it go to the else part?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int main()
{
	int a(0);
	int b;
	
	while(a==0)
	{
		cout << "As long as your number is under 100 it will loop" << endl;
		cin >> b;
		
		if(b<100)
		{
			cout << "Still not over 100, only " << b << endl;
			b=0;
		}
		
		else if(b>=100)
		{
			cout << "Good you number was " << b << endl;
			a=1;
			break;
		}
		
		else
		{
			cout << "Wrong" << endl;
			b=0;
		}
		
	}
}
AFAIK, there aren't any values of an integer that are not less than 100 and not greater than or equal to 100. That's probably why when cin >> b puts whatever value into b, no matter what value b holds now, your else part will never run.

-Albatross
Last edited on
maybe he meant using else to find letters,words...
yes sorry bout that i'm kinda tired but what i meant was
when i enter a letter into the program it say some thing like "Good you number was 199855737"
and i wonder why does it do that and how do i get it to go the else part of the code
anyone got an idea what's wrong?
Albatross already told you. According to you, what kind of integer doesn't fulfill either of the conditions?
try this [#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;



int main()
{
int a(0);
int b;

while(a==0)
{
cout << "As long as your number is under 100 it will loop" << endl;
cin >> b;

if(b>0 && b < 100)
{
cout << "Still not over 100, only " << b << endl;
b=0;
}

else if(b>=100)
{
cout << "Good you number was " << b << endl;
a=1;
break;
}

else
{
cout << "Wrong" << endl;
b=0;
}

}

system ("pause");

return 0;
}]
you did not pass the argument very well for the for loops so instead of using
[if(b<100)]
you are suppose to use [if(b>0 && b<100)] cos cin do not the inter that is greater or less than b.
so i think that solve your probs now... :D
it still wont work if i do it the way you said
maybe it's cause i dont have the header file thats included there but when i enter a letter it goes into an infinite loop,the program worked fine as long as you entered numbers but it just would not work with letter
my idea was that when you entered a letter it would go to the
1
2
3
4
5
else
{
cout << "Wrong" << endl;
b=0;
}

part
Last edited on
but u did not declare any such to your function here. all what you did to me is just declaring integer and looping it. nothing like declaring or passing [string here].
if you put any letter there it will just keep looping and wont stop which give u error. i dont understand you well. can u just put example?
1
2
3
4
5
6
7
8
switch (<#expression#>) {
        case <#constant#>:
            <#statements#>
            break;
            
        default:
            break;
    }
i think u should try using 'switch' instead of if-else cos it the only 1 that can return the default message for that cin.
is there really no way?

it seems logical enough

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(b<100)//if b is less than 100 do this code
{
       code
}

else if(b>=100) //if b is more than or equal to do this code
{
        code
}

else // if none of the other statement are true do this code
{
        code
}

is that not correct?
And i don't think i can make a switch because there are almost limitless possibilities
of numbers and i want it to work even if the number is 10000000000 or 1

but in a switch i would have to do

1
2
3
4
5
6
7
8
9
10
11
switch(b)
{
     case 100:
     code

     case 0:
     code
     
     default:
     code
}

and there would be no other possibilities
if i entered something else than 0 or 100 it would just take me to default
or is there some way to make a switch that could handle other numbers without having to make a ton of cases?
if there is please inform me
Last edited on
is that not correct?

No. A variable of type int can only store integers and there aren't any integers that are neither smaller than 100 nor greater or equal to 100. Therefore the else-part cannot ever execute.

If the input operations fail because no valid number was entered, the stream's failbit is set, which you can test:
1
2
3
4
5
6
7
8
9
10
#include <limits>
[...]
cin >> b;
if (cin.fail())
{
  cout << "Wrong" << endl;
  cin.clear(); //clears failbit, so subsequent read operations don't fail
  cin.ignore(numeric_limits<streamsize>::max(),'\n'); //get rid of junk in input buffer
}
else ... //a valid number was entered 
Last edited on
I have to call cin.sync(), too. (As well as cin.clear();)

This variation uses cin.good() rather than cin.bad(). It also has some other tweaks... ("OCD perfectionist"-esque?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main()
{
    bool carry_on_looping = true;
    
    while(carry_on_looping)
    {
        int number = 0;
        cout << "As long as your number is under 100 it will loop" << endl;
        cin >> number;

        if(cin.good()) // we got a number
        {
            if(number < 100)
            {
                cout << "Still not over 100, only " << number << endl;
            }
            else // i.e. number >= 100
            {
                cout << "Good your number was " << number << endl;
                carry_on_looping = false;
            }
        }
        else // we got something other than a number
        {
            cout << "Wrong! Please enter a number" << endl;

            cin.clear(); // clear error bit
            cin.sync(); // re-synchronize the input buffer
        }
    }

    return 0;
}


As long as your number is under 100 it will loop
weee!
Wrong! Please enter a number
As long as your number is under 100 it will loop
1
Still not over 100, only 1
As long as your number is under 100 it will loop
2
Still not over 100, only 2
As long as your number is under 100 it will loop
333
Good your number was 333
Press any key to continue . . .


Last edited on
when i tried andywestkens code it did not take me to the
1
2
3
4
5
6
7
8
   
else // we got something other than a number
{
cout << "Wrong! Please enter a number" << endl;

 cin.clear(); // clear error bit
 cin.sync(); // re-synchronize the input buffer
}

part when i entered a letter, it just looped
is this just happening to me?
Try adding cin.ignore(...) as shown in Athar's post above.

Andy

PS I've just checked that gcc is happy with clear/sync as was VC++. I did post the code with sync copied out (done to check the behaviour) but did correct my mistake only a minute or so later. These are both on a Windows machine.
Last edited on
my brain hurts, i feel that this is a bit over my head.
I still don't really get what was wrong with my code, could i switch the int on b to something that could hold both letters and numbers?
You could use a char buffer or a std::string. The former needs to be used with cin.get(), giving the buffer's address and size.

Then you need to check the string manually (using isdigit() or an equivalent custom routine), and then convert it (here I'd prob. just use atol(); not the slickest or safest way, but should be ok here).

istream::get(char* s, streamsize n)
http://www.cplusplus.com/reference/iostream/istream/get/

isdigit()
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

atol()
http://www.cplusplus.com/reference/clibrary/cstdlib/atol/
Last edited on
i'm really sorry but i don't understand anything.
Just replace the sync() call by ignore() for now. You can leave the more complicated parts of standard C++ iostreams for when you prepare for job interviews (arguably their only good use).
Pages: 12