Small game "I think number" code error
Hi guys, I am beginner so I need help or some hint with this program.
I need program where program think specific number between 0 and 4 200 000 000 I chose 2 121 993. When i scanf some number for example 90 000 000 program have to write "seeking number is smaller" or "Seeking number is bigger". In the next write number program have range between 0 and 90 000 000. I have some code and I think that is "good" code but I have problem with "Biggest from right" I have in program -1 and I dont know why.
Sorry for my english, If you dont understand write and I try explain it some another way
I have 2 problems :
1)I dont understand why I have " -1 " in biggest from right insted I want max number and it is 4 200 000 000.
2) I write some number in program and nothing happend, any printf " seeking number is smaller / bigger"
Re: Small game "I think number" code error
You declared it to be a "long int." That is a signed variable.
On my system (and, apparently, on yours) a long int is a 32-bit quantity. The largest 32-bit signed int has a decimal value of 2147483647. (That's 0x7FFFFFFF.)
The 32-bit signed integer representation of -1 is 0xFFFFFFFF. When you try to assign the value 4294967295 it will be stored as 0xFFFFFFFF. When you print it out with %d (the format specifier for a signed integer) it reads back -1.
Now, the value stored is not the problem; the problem occurs when you try to compare that signed integer value with, say, 12345. It is less than 12345. (-1 is less than 12345), which does not agree with the intent of your program.
So, you might start by declaring it to be an unsigned rather than the (default) signed quantity.
(Better yet, use the current standard integer types defined in <stdint.h>, but I will stick with yours as much as possible)
Also, since you declared them to be long integers, strict compliance with standard C requires that printf and scanf use %l as part of the format specifier.
I'll get you started with what I have covered so far.
Note that when doing something new, I always (yes, always) print out some values to show what the program is working on:
CPP / C++ / C Code:
Output after compiling with GNU gcc version 4.4.7 on my Centos 6.7 Linux workstation:
sizeof(biggest_from_right) = 4
biggest_from_right = 4294967295 decimal = 0xFFFFFFFF hex
Maybe that will help get you started. When you are ready to do the scanf, use %lu as a format specifier, and for debugging purposes, print out the value so that you can be sure to know what the computer is working on when it does the comparisons.
If you have further questions, post again.
Sometimes I just can't help myself...
|Thread Tools||Search this Thread|
|Rate This Thread|
|Thread||Thread Starter||Forum||Replies||Last Post|
|Problem passing variables to base constructor||rola||C++ Forum||3||04-Oct-2012 13:51|
|Including DDK headers trouble.||Sarge||C Programming Language||1||02-Feb-2012 18:07|
|error C2064: term does not evaluate to a function taking 1 arguments||AndyCharles||C++ Forum||1||11-Sep-2011 10:45|
|[Tutorial] GUI programming with FLTK||dsmith||FLTK Forum||10||03-Oct-2005 15:41|
|Segmentation error on executing socket code||nkhambal||C Programming Language||3||30-Aug-2004 12:53|
All times are GMT -6. The time now is 00:49.