Click to See Complete Forum and Search --> : invalid input check within the function


monkeyboi
02-04-2006, 06:30 PM
Hi,

i'm required to write a function
void print(unsigned int x, unsigned int base =10)

and i have to do an input check for invalid input, how can this be done inside the function?

for example if i enter a negative number for the first argument, inside the function x = some large random number.


thx

JayMan8081
02-04-2006, 07:15 PM
Hmm, could you be a little clearer? According to your prototype a negative number won't be seen inside the print function since the type for x is declared as unsigned.

monkeyboi
02-04-2006, 07:32 PM
Hmm, could you be a little clearer? According to your prototype a negative number won't be seen inside the print function since the type for x is declared as unsigned.


i know it's unsigned, just say i'm making a function with that prototype but i have no control over what pass to it. like if my function is to be used as library, and some idiot use it but doesn't know what he's doing and pass in crazy values.
how can i handle that error?

JayMan8081
02-04-2006, 07:40 PM
Probably just about anyway you want. You could print a message to stderr and not do anything else in that function and simply return.

flukshun
02-04-2006, 08:08 PM
the check HAS to be within the function? and you cant declare the values as signed int's so you can just do a if(x < 0) type of check?

monkeyboi
02-04-2006, 08:54 PM
the check HAS to be within the function? and you cant declare the values as signed int's so you can just do a if(x < 0) type of check?


apparently the check has to be inside the function and has to follow that prototype..

i find it impossible to do, might have to ask my prof later

JayMan8081
02-04-2006, 08:55 PM
According to a test I performed on my Ubuntu install with g++ 3.4 you can do a check for if (x < 0) because the unsigned keyword doesn't give any compilation errors when passing a signed type into the function. In my example I called print(-100) and inside the function x was -100.

flukshun
02-04-2006, 09:20 PM
interesting. and if that wasnt the case, i suppose you could explicitly cast it as a signed int, and do the check on the result. right? actually that wouldnt work...cuz you still wouldnt know when to handle it as signed or unsigned..unless there's some upper bound to the input range that doesnt utilize the highest-order bit

bwkaz
02-05-2006, 03:02 PM
In my example I called print(-100) and inside the function x was -100. That's because you probably used %d for the printf, which takes a signed integer. Had you used %u (unsigned decimal), you would have gotten different output.

gcc should have warned you about the printf; it usually says something about "int format, unsigned int arg" when stuff like this happens. You would probably need -Wall on, though. Perhaps also -pedantic, but I don't think so.

JayMan8081
02-06-2006, 01:34 PM
No, without the extra flags g++ doesn't complain. I tried the following code:
#include <stdio.h>

void print(unsigned int x);

int main()
{
int i = -100;
print(i);

return 0;
}

void print(unsigned int x)
{
printf("Printing with unsigned: %u\n", x);
printf("Printing with signed: %d\n", x);

if (((int)x) < 0)
{
printf("X was less than zero.\n");
}
}

This is the result of my shell session:
jhorsman@jhorsman:~$ vi test.cpp
jhorsman@jhorsman:~$ g++ -o test test.cpp
jhorsman@jhorsman:~$ ./test
Printing with unsigned: 4294967196
Printing with signed: -100
X was less than zero.

So you could cast x as a signed integer and check for negative values. Also compiling with both -Wall and -pedantic didn't produce any warnings on the above code.

truls
02-06-2006, 06:47 PM
Interestingly enough, if you set i to 4294967196 and compile you get the following warning:
warning: this decimal constant is unsigned only in ISO C90
Also, if you check UINT_MAX (defined in limits.h) you can see what the defined maximum value for unsigned integer is. On my machine it is 4294967295 which is a larger number than the negative one (in its unsigned form).

I think the best solution is JayMan8081's, but you should be aware that this is one area which seems to be dependent on your compiler. I think this is one of those assignments where the prof's spesifications are not clear enough, he should have given a clear definition of what he puts in the term "invalid input".

bwkaz
02-06-2006, 08:05 PM
Also compiling with both -Wall and -pedantic didn't produce any warnings on the above code. Maybe that only happens when there's a possible size mismatch then ("int" -> "long int" or "short int" -> "int" or "int" -> "long long int" or any other combination). I'm pretty sure I remember seeing it before with signed/unsigned, but if you aren't getting it with this code, then maybe I'm remembering wrong.