Thread: What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Question What am I doing wrong?

    I am currently taking a C programming course via the internet and need help. I cannot figure out what I am doing wrong within the coding that I have created. I am supposed to be creating my own functions right now, and thought that I was, but apparently not. When I run this code it ask for the input and then does nothing. Any help would be much appriaciated.

    Code:
    #include <stdio.h>
    
    int i;
    int liquid1 (int);
    int liquid2 (int);
    int liquid3 (int);
    int liquid4 (int);
    int main()
    {
    printf("Enter cups to be converted:");
    scanf ("%d\n",&i);
    printf ("gallons = %d\n",liquid1 (i));
    printf ("quarts = %d\n",liquid2 (i));
    printf ("pints = %d\n",liquid3 (i));
    printf ("cups = %d\n",liquid4 (i));
    
    system ("PAUSE");
    
    return 0;
    }
    
    int liquid1 (i)
    {
    int s,g;
    g = s/16;
    return (g);
    }
    int liquid2 (i)
    {
    int f,q;
    q=(f%16)/4;
    return (q);
    }
    int liquid3 (i)
    {
    int d,p;
    p=((d%16)%4)/2;
    return (p);
    }
    int liquid4 (i)
    {
    int l,c;
    c=((l%16)%4)%2;
    return (c);
    }

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    for example,
    Code:
    int liquid1 (int i)   <- data type for the parameter "i"
    {
    int s,g;        <- these are not assigned a value  i needs to be in here somewhere
    g = s/16;     <- should this be: g=i/16; ?
    return (g);  
    }

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    I have done what you showed and now get a warning for every funciton that says
    "settings\primary\my documents\school\c programming\liq.c:24: warning: declaration of `i' shadows a parameter"

    What does that mean? When I run the code it still only asks for the input.
    Last edited by al_engler; 12-18-2006 at 01:38 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        scanf("%d\n", &i );
    I'm quite shure you don't want that. Only eof or some additional input would stop scanf.
    use
    Code:
        scanf("%d", &i );
    jim mcnamara meant

    Code:
    int liquid1 (int i) {
    int g;
    g = i/16;
    return (g);  
    }
    Kurt

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Thank you, now it works.
    I am trying to see if i understand what was happening. By puttingn after the
    %d, I was basically stopping the program right there?
    Last edited by al_engler; 12-18-2006 at 01:50 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the \n made scanf waiting for some whitespace. but scanf also skips any whitespace after inputting the int so it doesn't see the '\n' after your input and keeps waiting.
    some input like
    Code:
    1000 y\n
    would have stopped the loop
    Kurt

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Thank you for the help, I think I am understanding, hopefully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM