Thread: Conversion Specifier

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    Conversion Specifier

    As soon as this program gets to the scanf line that has a precision specification, it jumps to the end of the program. I assume that a character is somehow getting into the subsequent scanf and getchar, but I don't see how that would happen. Can anybody tell me what is happening here? Also if I put a space between %lf and %*c it takes 2 enter presses to proceed and I don't know why that is either.

    Thanks


    Code:
    #include <stdio.h>
    
    #define STORE_NAME "Sierra Sporting Goods"
    
    
    int main(void){
        int intNumber, intType, intQuantity;
        double dblCost, dblPrice;
        
        printf("%s\n", STORE_NAME);                          /*prompt for input*/
        printf("\n\nEnter the product number: ");
        scanf("%04d%*c", &intNumber);
        printf("\nEnter the product type: ");
        scanf("%d%*c", &intType);
        printf("\nEnter the quantity: ");
        scanf("%d%*c", &intQuantity);
        printf("\nEnter the cost: ");
        scanf("%.2lf%*c", &dblCost);              //<==== the .2 is my problem
        printf("\nEnter the price: ");
        scanf("%.2lf%*c", &dblPrice);             //<==== the .2 is my problem
    
    
        printf("\n\nThe product number is %d", intNumber);   /*Display values*/
        printf("\nThe product type is %d", intType);
        printf("\nThe quantity type is %d", intQuantity);
        printf("\nThe cost is %lf", dblCost);
        printf("\nThe price is %lf", dblPrice);
        
        printf("\nPress enter to continue\n");
        
        getchar();
      
       
     return 0;
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %.2lf is not a valid conversion specifier for scanf. There is no precision on input, only output.

    As to the other, a space in an input format string matches any and every whitespace, hence the first enter key would match the space and not the %*c.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Thanks.

    Is that also to say that there is no width on input either(i have %04d in a scanf)?

    and

    Does "%04d" do something different than "%4d"?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can have field width, sure. 04 is octal, and 4 is decimal, but in this case they both equal the same number. (0 doesn't mean anything special in scanf like it does in printf.)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    On output only. Let's say x = 12;

    printf("%d",x) produces 12;
    printf("%04d",x) produces 0012;

    The leading 0 will pad the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM