Thread: Can anyone explain what I'm doing wrong? Runs but doesn't let the user input

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Angry Can anyone explain what I'm doing wrong? Runs but doesn't let the user input

    I'm new to programming and very confused.

    Code:
    #include <stdio.h>
    
    
    #define INCHES_IN_CUBIC_FOOT 1728
    
    
    int main () {
        double length_in_inches; 
        double width_in_inches;
        double height_in_inches;
        
        printf("Length: ");
        scanf("%lf", &length_in_inches);
        
        printf("Width: ");
        scanf("%.2lf", &width_in_inches);
        
        printf("Height: ");
        scanf("%d", &height_in_inches);
        
        float num_1 = length_in_inches * width_in_inches* height_in_inches;
        printf("Volume in Cubic Inches: %.2lf", num_1);
        
        float num_2 = (length_in_inches * width_in_inches*height_in_inches) /INCHES_IN_CUBIC_FOOT;
        printf("Volume in Cubic feet:%.2lf", num_2);
    
    
        return 0;
        }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    If it lets you enter the length but not width it is probably because of those "extra" characters in your scanf() format string.

    If you're compiler was properly configured it should be able to warn you about these issues:

    main.c||In function ‘main’:|
    main.c|16|warning: unknown conversion type character ‘.’ in format [-Wformat=]|
    main.c|16|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|19|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat=]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|

    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,560
    By the way, is this supposed to be C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    101
    C

    or maybe he is using C syntax and compiling C++. I know this very well.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Ok lad, I think I have your code but is still in C syntax:

    There were two mistakes: the scanf of height had a format %d while with double you should put %lf. In scanf please don't put any precision, only to read the result with printf, so I removed the .2 in width scanf. I also added some \n in printf to end line so the output is prettier.

    Code:
    #include <stdio.h>
    
    #define INCHES_IN_CUBIC_FOOT 1728
    
        int main () {
        double length_in_inches;
        double width_in_inches;
        double height_in_inches;
    
        printf("Length: ");
        scanf("%lf", &length_in_inches);
    
        printf("Width: ");
        scanf("%lf", &width_in_inches);
    
        printf("Height: ");
        scanf("%lf", &height_in_inches);
    
        float num_1 = length_in_inches * width_in_inches* height_in_inches;
        printf("Volume in Cubic Inches: %.2lf\n", num_1);
    
        float num_2 = (length_in_inches * width_in_inches*height_in_inches) /INCHES_IN_CUBIC_FOOT;
        printf("Volume in Cubic feet:%.2lf\n", num_2);
    
        return 0;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  2. Program doesn't wait for user input!
    By gaurav_13191 in forum C Programming
    Replies: 8
    Last Post: 07-13-2011, 08:25 AM
  3. Program runs and calculates before all integers are input
    By thewizardalbany in forum C Programming
    Replies: 7
    Last Post: 07-30-2006, 05:01 AM
  4. receiving notification when user runs a program
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 11:23 PM
  5. Replies: 6
    Last Post: 10-24-2002, 08:58 AM

Tags for this Thread