Thread: How can I enter item price?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    How can I enter item price?

    Hi,

    I've managed to write the following program that displays total purchase price amounts for places with different sales tax rates. As it is, it assumes a value of $125, but I need to now modify it so a user can input any item price. I'm banging my head against a wall with this one.

    Thanks for any help!

    Code:
    #include <stdio.h>
    main()
    {
    char city1[] = "Lajolla"; 
    char city2[] = "Encintas";
    char city3[] = "Delmar";
    float tax1;
    float tax2;
    float tax3;
    int pur1;
    tax1 =7.75;
    tax2 =7.5;
    tax3 =7.25;
    pur1 =125;
    printf("\nItem price is %d\n", pur1);
    getch();
    printf("Tax at %s is: $%.2f\n", city1,pur1 * (tax1 / 100) ); 
    printf("Tax at %s is: $%.2f\n", city2,pur1 * (tax2 / 100) );
    printf("Tax at %s is: $%.2f\n", city3,pur1 * (tax3 / 100) );
    getch();
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read up on scanf, and make main return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Eg.
    Code:
    int x;
    scanf("%d%*c",&x);
    The red part makes sure the newline at the end of the input is discarded, since what you enter is

    56\n

    '\n' being the product of the enter key.

    Do not use floating point numbers for money, read this:

    Beginner: What is wrong with my code? It seems fine!

    My trick for printing dollars and cents there needs a slight tweak tho:
    Code:
    int x=500;
    printf("$%d.%02d\n",x/100,x%100);
    The "0" will fill in the two digit placeholder with zeros, so you get "$5.00" and not "$5. 0".
    Last edited by MK27; 08-07-2009 at 01:34 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also make sure that you use indentation in the code that you write. It looks neat and readable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  2. multi file project error
    By dantestwin in forum C++ Programming
    Replies: 14
    Last Post: 07-22-2004, 12:10 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM