Thread: N00b @ C: Code is failing at line 12 which is a basic scanf

  1. #1
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59

    Question N00b @ C: Code is failing at line 12 which is a basic scanf

    Code:
    #include <stdio.h>
    #include <matlib.h>
    
    #define YARDS_PER_HAT 220; YARDS_PER_SWEATER 460;
    
    int main(void){
        
        int  yards_per_ball, num_balls;
        int  total_h, total_s;
        
        printf("How many balls of yarn do you have?\n")
        scanf("%d", &num_balls);
       
        
        if (num_balls < 0)
           printf("You are a liar! That's impossible. Restart the program and tell the truth.\n");
        
        else
            printf("How many yards are in each of your balls of yarn?\n");
            scanf("%d" , &yards_per_ball);
            
        if (num_balls < 0)
           printf("You are a liar! That's impossible. Restart the program and tell the truth.\n");
           
        total_h = yards_per_ball*num_balls/(YARDS_PER_HAT);
        total_s = yards_per_ball*num_balls/(YARDS_PER_SWEATER);
          
           
        printf("You can make %d hats or you can make %d sweaters.\n" , total_h; total_s);
        
        getchar();
        return 0        
    }

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    What's up with your title? That's not how you should properly ask a question. Also, you didn't specify the error in the code like you should have.

    You need to look up processor directives in C, you can't just separate them with a semicolon and call it done. I'll leave it up to look it up in this site's tutorials.

    You really like abusing the semicolon terminator and place it in spots that it shouldn't have gone in. You need to look up exactly what you use the semicolon in C for, and what you use the comma for.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  3. #3
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Thank you, kind sir. That was a good direction

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    your indentation does not match real flow of the code, your defines are wrong
    This is how defines should look
    Code:
    #define YARDS_PER_HAT 220
    #define YARDS_PER_SWEATER 460

    This is how your if/else is really working
    Code:
        if (num_balls < 0)
           printf("You are a liar! That's impossible. Restart the program and tell the truth.\n");
        else
            printf("How many yards are in each of your balls of yarn?\n");
    
        scanf("%d" , &yards_per_ball);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very Basic Telnet/Mud Client - coding n00b
    By Cebb in forum C Programming
    Replies: 19
    Last Post: 10-11-2011, 04:16 PM
  2. Basic Scanf() help
    By DooberXL in forum C Programming
    Replies: 7
    Last Post: 10-11-2004, 11:02 PM
  3. n00b Code Error.
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2004, 05:15 PM
  4. Basic C++ Help for n00b.
    By cheesestickman in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2004, 04:23 AM
  5. ISO someone daring to look at some n00b code!
    By Rev. Jack Ed in forum Game Programming
    Replies: 4
    Last Post: 10-17-2003, 08:45 AM