-
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;
}
-
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.
-
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.
-
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.