Thread: bad io output

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    44

    bad io output

    The output I get from this program just shows all zeros, I cant figure out what is wrong as this is how the scanf and printf statements are formatted in the book. Any help please?

    Code:
    #include <stdio.h>
    
      double min(double, double);
    
    int main(void)
    {
      double first, second, displaylow;
    
      printf("Please enter 2 floating point numbers: ");
      scanf("%f %f", &first, &second);
    
      displaylow = min(first, second);
    
      printf("%f is the lower of the floats entered", displaylow);
    
      return 0;
    }
    
    double min(double one, double two)
    {
      if (one > two)
        return two;
      else
        return one;
    }

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    change
    Code:
    scanf("%f %f", &first, &second);
    to
    Code:
    scanf("%lf %lf", &first, &second);
    At least that's what gcc says.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    carrotcake1029- Thanks, that fixes the problem on this program. However I tried this fix on my other program that had the same problem and it didnt work. Could I bother for a tad more help?
    Code:
    #include <stdio.h>
    
      void larger_of(double *, double *);
    
    int main(void)
    {
      double first, second;
    
      printf("Please enter 2 floating point numbers: ");
      scanf("%lf %lf", first, second);
    
      larger_of(&first, &second);
    
      printf("The values entered are now %f and %f", first, second);
    
      return 0;
    } 
    
    void larger_of(double * one, double * two)
    {
      if (one > two)
        *two = *one;
      else
        *one = *two;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that:
    Code:
      if (one > two)
    is incorrect. You are comparing the address of one with the address of two.

    Also, if "one" is greater than "two", you probably need to SWAP the two values, which involves storing one value in a temporary variable, the copying the second value into the one value, and then storing the temporary in the second variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    aarrgh *facepalms* thx matsp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Weird read input or bad printf output?
    By ChaoticMachiner in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 03:40 PM
  3. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  4. Bad output!
    By SledMan2002 in forum C Programming
    Replies: 2
    Last Post: 11-09-2002, 06:35 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM