problems w/ a c program that needs to format product information entered by the user

This is a discussion on problems w/ a c program that needs to format product information entered by the user within the C Programming forums, part of the General Programming Boards category; I'm having trouble writing this program: 5. Write a program that formats product information entered by the user. A session ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    problems w/ a c program that needs to format product information entered by the user

    I'm having trouble writing this program:

    5. Write a program that formats product information entered by the user. A session with the program should look like this:

    Enter item number: 583
    Enter unit price: 13.5
    Enter purchase date (mm/dd/yy): 10/24/95

    Item Unit Purchase
    Price Date
    583 $13.50 10/24/95

    The item number and date should be left justified; the unit price should be right justified. Allow dollar amounts up to $9999.99. HINT: Use tabs to line up columns.

    This is what I've written:
    #include <stdio.h>

    int main()
    {
    int item, mm, dd, yy;
    float unit;

    printf("Enter an item number: ");
    scanf("%d", &item);
    printf("Enter a purchase date (mm/dd/yy): ");
    scanf("%d/%d/%d", &mm, &dd, &yy);
    printf("Enter a unit price: ");
    scanf("%.2f", &unit);
    printf("Item Unit Purchase\n");
    printf(" Price Date\n");
    printf("%-d $%.2f %-17d/%17d/%17d\n", item, unit, mm/dd/yy);

    return 0;
    }

    (i'm also having trouble when i write out 'Enter purchase date: ' last... so that's why it's in the middle)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf("Enter an item number: ");
    scanf("%d", &item);
    printf("Enter a purchase date (mm/dd/yy): ");
    scanf("%d/%d/%d", &mm, &dd, &yy);

    printf("Enter a unit price: ");
    scanf("%.2f", &unit);
    printf("Item Unit Purchase\n");
    printf(" Price Date\n");
    printf("%-d $%.2f %-17d/%17d/%17d\n", item, unit, mm/dd/yy);

    return 0;
    }

    (i'm also having trouble when i write out 'Enter purchase date: ' last... so that's why it's in the middle)
    What's the problem? It either works or it doesn't. If it works in one spot, it'll work in another.

    Put tabs in this line, between words:

    printf("Item Unit Purchase\n");


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    thanx Quzah~!! now everything works EXCEPT for the unit price...

    #include <stdio.h>

    int main()
    {
    int item, mm, dd, yy;
    float unit;

    printf("Enter an item number: ");
    scanf("%d", &item);

    printf("Enter a purchase date (mm/dd/yy): ");
    scanf("%d/%d/%d", &mm, &dd, &yy);

    printf("Enter a unit price: ");
    scanf("%.2f", &unit);

    printf("Item Unit Purchase\n");
    printf(" Price Date\n");
    printf("%-d $%.2f %-02d/%02d/%02d\n", item, unit, mm, dd, yy);

    return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("%.2f", &unit);
    Change this to

    scanf("%f", &unit);

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    problems w/ a c program that needs to format product information entered by the user

    thanx Prelude~!! that did the trick

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    thanx Prelude~!! that did the trick

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796


    >thanx Prelude~!!
    You're quite welcome.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21