Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Thread Tools Search this Thread Rate Thread
Old 27-Oct-2015, 11:24
Jamesss Jamesss is offline
New Member
Join Date: Oct 2015
Posts: 1
Jamesss is on a distinguished road

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"

int main() { long int number; long int biggest_from_left=0; long int biggest_from_right= 4294967295; int i_think_number=2121993; while (number!=i_think_number) { printf("Quess,Quess my number\n"); scanf("%d%",&number); printf("Biggest from left:%d\n",biggest_from_left); printf("Biggest from right:%d\n",biggest_from_right); if((number < i_think_number) && (number > biggest_from_left)) { printf("Seeking number is bigger\n"); } else if((number > i_think_number) && (number < biggest_from_right)) { printf("Seeking number is smaller\n"); } continue; }printf("Seeking number is 2121993\n"); return 0; }
Old 27-Oct-2015, 15:26
davekw7x davekw7x is offline
Outstanding Member
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Small game "I think number" code error

Originally Posted by Jamesss

1)I dont understand why I have " -1 " in biggest from right insted I want max number and it is 4 200 000 000.

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:
#include <stdio.h>

int main()
    /* Declare it as an unsigned long, and make sure the literal
     * constant will be interpreted as unsigned long
    unsigned long int biggest_from_right = 4294967295UL;

    /* Make sure you understand that it's a 4-byte variable */
    printf("sizeof(biggest_from_right) = %d\n", sizeof(biggest_from_right));
    printf("biggest_from_right = %lu decimal = 0x%lX hex\n",
            biggest_from_right, biggest_from_right);

    return 0;

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.

Originally Posted by Jamesss
Sorry for my english...
Your English is fine. You have an excellent start with the code. Just a little tweak (or two) and you are in business!


Sometimes I just can't help myself...

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
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

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:36.

vBulletin, Copyright © 2000 - 2018, Jelsoft Enterprises Ltd.