Help with C++ Program

This is a discussion on Help with C++ Program within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that will receive as input an item type and then depending on the ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    Help with C++ Program

    I am trying to write a program that will receive as input an item type and then depending on the input, calculate tax for it, etc. If the user does not enter a valid item type, it is supposed to ask the user to re-enter the correct item type. I am having problems with the condition section of the while loop. Can anyone suggest a hint to help me correct the problem? Thanks.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    int main()
      
    {
        
        double price ;
        double discount ;
        double tax ;
        double total ;
        double discount_amount ;
        double tax_amount ; 
    
    
        char item_type [8] ;
        cout << "Enter the Item Type: food, clothes, toys, or other: " ; 
        cin >> item_type ;
        
        while (item_type == "food" || item_type == "clothes" || item_type == "toys"
        || item_type == "other")
        
    {
                       
        cout << "Enter Item Price : $" ;
        cin >> price ; 
        
        cout << "Enter Discount Percentage (%): " ;
        cin >> discount ;
        
        
        // Checks if item is taxable and calculates tax.
        
        if (item_type == "toys" || item_type == "other")
        
                    {   cout << "Enter Tax Percentage (%): " ;
                        cin >> tax ;
                    }   cout << endl ;
        
        
        cout << "The Item Price is: $" << price << "." <<endl ;
        discount_amount = price - (price * (discount/100));
        
        cout << "The Discount Amount is: $" << discount_amount << endl ;
        tax_amount = (price - (price * (discount/100))) * (tax/100);
        
        cout << "The Tax Amount is : $" << tax_amount << endl ;
        
        cout << "The Final Bill Amount is : $" << discount_amount + tax_amount ; 
        cout << endl ;   
        
    }
        
        cout << "Please Enter a Valid Item Type: " <<endl ;
        cin >> item_type ;
        
        
        system("PAUSE") ;
           
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It looks to me as though you have it the wrong way round, you should just have a do...while loop for the input, so it does it while the input is incorrect and then it continues to do all the calculations.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    char item_type [8] ;
    ...
    cin >> item_type ;
    Very bad idea. As soon as the user types 8 characters (because they will become 9 with a trailing \0), you have a buffer overrun.

    In general, you should NEVER fill a char[] buffer except if the function has an argument to specify the maximum number of characters it's allowed to read.

    Fix this by using a std::string for the item_type. The string will adjust its size to be large enough to store the input.

    That also will solve another major problem, which is you can't use == with C-style strings.

    Code:
    char item_type[8];
    ...
    if (item_type == "toys")
    That if statement will ALWAYS return false, because it's comparing them as pointers. That is, the if statement is actually asking "Is the memory address where item_type is stored the same as the memory address where the constant string literal "toys" is stored?"

    And that will always, always be false. There is a way to fix that (using C-style string compares) but that's the bad fix. The best fix is a std::string, where == does what you think it does, string comparison.
    Last edited by Cat; 09-09-2006 at 07:42 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You don't need to put a space before each semicolon (although there's no harm in doing so).

    You have a problem with your code; an uninitialized variable. What if this if
    Code:
    if (item_type == "toys" || item_type == "other")
    is false? tax won't be set to anything, but you use tax anyway. tax will be a random value (say 2.7847959e-55), and your calculations will be . . . off.

    Code:
        cout << "Please Enter a Valid Item Type: " <<endl ;
        cin >> item_type ;
        
        
        system("PAUSE") ;
           
        return 0;
    }
    I think you need a loop here, because as written, that will prompt you for something and do nothing with the value.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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