Thread: There's something wrong with my code...

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    2

    There's something wrong with my code...

    Everytime I type a character, the number 1 appears in the next line. And i just keep getting the message "Wrong! I have more than that." even when I type a number bigger than 1023
    Do you know what's wrong. I've spent hours on this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    int main(void)
    {
        srand(time(NULL));
        
        int candies = rand() % 1024;
        
        int c;
        
        printf("Guess how many candies I have. I have less than 1024.\nGo on! Type a number!\n");
        
        while(1) 
        {
            c = scanf("%d", &c);
            
            if(c == candies)
            {    
                printf("%d\n", c);
                printf("That's correct! How did you know that?");
                return 0;
            }
                else if(c > candies)
                {
                    printf("%d\n", c);
                    printf("No. I wish I had that much...\n");
                }
                else if(c < candies)
                {
                    printf("%d\n", c);
                    printf("Wrong! I have more than that!\n");
                }
        }
    }

  2. #2
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Where do you record your value for c?
    Sure, you have a prompt to take a value for c, but what does it actually do anything?

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Code:
    c = scanf("%d", &c);
    The problem is here (I think). Scanf() is returning the value of successfully copied arguments (1 in this case), while at the (same time? before? ) trying to copy the user input into the variable.

  4. #4
    Registered User
    Join Date
    May 2014
    Posts
    2
    but what exactly should i do? what part of the code should i change? i'm new to this so...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alpo View Post
    Code:
    c = scanf("%d", &c);
    The problem is here (I think). Scanf() is returning the value of successfully copied arguments (1 in this case), while at the (same time? before? ) trying to copy the user input into the variable.
    I haven't checked closely enough to determine if it is the only problem.

    Just to clarify, scanf() potentially modifies c first (processing the %d specifier) and returns a value. That return value is then stored in c, and any value read is lost. There is nothing inside scanf() which would change that order (returning a value follows the attempt to process the %d format). If an error occurs reading, the error is detected before scanf() returns.

    Quote Originally Posted by jane.d View Post
    but what exactly should i do? what part of the code should i change? i'm new to this so...
    Store the value returned by scanf() in a variable other than c. That way you capture both the value read into c (if any), and the return value from scanf() (which, among other things, will separately indicate if a value was successfully read and stored in c).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by jane.d View Post
    but what exactly should i do? what part of the code should i change? i'm new to this so...
    Sorry, basically you just need to separate checking the return value of scanf (scanf() is a function defined in the stdio.h header, it returns an int) from the variable you are using with scanf() to store into. You could do it like this

    Code:
       if(scanf("%d", &c) != 1)
         return 0;
    This would evaluate to false if scanf performs propery, skipping the return. Or you could do like grumpy says

    Code:
      newvariable = scanf("%d" , &c);

    You would then have a variable you could check to see if scanf() stored correctly, newvariable should be 1 in that case.

    Or you could just not check the return value (the YOLO approach to programming :P)

    Code:
      scanf("%d", &c);

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    the YOLO approach to programming
    o_O

    Gross.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by phantomotap View Post
    o_O

    Gross.

    Soma

    Sorry, I admit to being a whippersnapper and will kindly get off your lawn lol.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Sorry, I admit to being a whippersnapper and will kindly get off your lawn lol.
    O_o

    I don't care if you play chicken with an empty trail screaming "YOLO" all the time. So long as your stupid behavior doesn't infringe on the lives of others, your behavior is not my business.

    I do care if you give people stupid advice because you are a lazy or incompetent programmer.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Those who apply the YOLO approach to programming are more likely to die at the hands of fellow developers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by phantomotap View Post
    I do care if you give people stupid advice because you are a lazy or incompetent programmer.

    Look at this site's tutorials Functions in C - Cprogramming.com . You may say (correctly) that best practice isn't a democracy and giving another example of bad habit doesn't make what I supplied any more correct, but I did supply correct answers as well, and if the OP was learning from the tutorials he would be doing the worst of my examples. Also saying "you can do it this way" isn't advocating a thing, just admitting it's existence.

    Edit: Also, giving the example without the check for return value might have given an important clue and to what "checking the return value" even means. If I kept doing everything at once in my examples it might be harder for a beginner to separate the check from what scanf() is actually doing with the pointer (as was the original problem).
    Last edited by Alpo; 05-17-2014 at 11:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with my code ??
    By Spock1701 in forum C Programming
    Replies: 2
    Last Post: 04-08-2013, 08:59 PM
  2. What is wrong with this code? please help..
    By thebridge in forum C Programming
    Replies: 2
    Last Post: 10-07-2010, 02:06 PM
  3. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  4. What is wrong with the following code?
    By steals10304 in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 02:06 PM
  5. Is there something wrong with this code?
    By lime in forum C Programming
    Replies: 6
    Last Post: 07-28-2003, 06:17 AM

Tags for this Thread