I'm new to C++ and this is my first real program writing on my own. It keeps crashing right after you ask the cost of the product. It won't even let me input anything in there. I ran the debug and I get an error on line 23, I'm guessing it's the way I set up my math but, I'm not completely sure, it seems like it's definitely a syntax error. I'm guessing the same thing will happen on line 32 and possibly 35 as well. Help would be greatly appreciated. (Please don't be too harsh just getting started)
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Welcome to Profit Percentage and Discount Price Calculator." << endl;
int nProductName;
cout << "Enter the name of the product: ";
cin >> nProductName;
char cProductName = (char)nProductName;
int nProductCost;
cout << "Enter the cost of the product: ";
cin >> nProductCost;
int nOriginalSalePrice;
cout << "Enter the original sale price of the product: ";
cin >> nOriginalSalePrice;
int nProfitPercentage = ((nOriginalSalePrice-nProductCost)/nOriginalSalePrice)*100;
char cProfitPercentage = (char)nProfitPercentage;
cout << "The profit percentage is $" << cProfitPercentage << endl;
int nDiscountPercentage;
cout << "Enter the discount percentage: ";
cin >> nDiscountPercentage;
int nSalePrice = nOriginalSalePrice-nOriginalSalePrice*(nDiscountPercentage/100);
char cSalePrice = char(nSalePrice);
int nAmountSaved = nOriginalSalePrice - nSalePrice;
char cAmountSaved = char(nAmountSaved);
cout << "The sale price of " << cProductName << " is $" << cSalePrice
<< " your amount saved is $" << cAmountSaved << endl;
system("PAUSE");
return 0;
}
Why do you cast some of the int values to char? it does not convert an int into its character representation! It does only tell the compiler to interpret the unmodified integer value as character.
F.e.: An integer value of 33 would be printed as letter "!" (have a look at the ASCII table).
Line 12 expects an integer number, like "33", as input. Any non number like input will result in a read error. You better should as cin if everything went well. if (! cin) //... .
Try:
1 2 3 4
static size_t NAME_SIZE = 64; // An example value
char productName[NAME_SIZE];
cin >> productName;
or better use standard strings to avoid buffer overflow and have a more powerful string object.
@beko @tcs
Originally, I had tried it without the char and cXXX values, but it would just randomly auto-complete the rest of he program with random integers and such. So, I thought If I moved it to a character it wouldn't do that. I have changed the back to what you were suggesting and I still can't get it to run correctly for me.
"Line 12 expects an integer number, like "33", as input. Any non number like input will result in a read error. You better should as cin if everything went well. if (! cin) //... ."
I guess my concept of the cin command is a little flawed. I was under the interpretation that when used after cout << it saves the next entry has a variable. If anyone could explain this really clearly that'd be great. I read the whole book "C++ for dummies" and obviously it wasn't dumbed down enough lol.
- I have added #include <string> , but it keeps giving me the same problem of auto completing.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
std::string productName;
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Welcome to Profit Percentage and Discount Price Calculator." << endl;
int nProductName;
cout << "Enter the name of the product: ";
cin >> nProductName;
int nProductCost;
cout << "Enter the cost of the product: ";
cin >> nProductCost;
int nOriginalSalePrice;
cout << "Enter the original sale price of the product: ";
cin >> nOriginalSalePrice;
int nProfitPercentage = ((nOriginalSalePrice-nProductCost)/nOriginalSalePrice)*100;
cout << "The profit percentage is $" << nProfitPercentage << endl;
int nDiscountPercentage;
cout << "Enter the discount percentage: ";
cin >> nDiscountPercentage;
int nSalePrice = nOriginalSalePrice-nOriginalSalePrice*(nDiscountPercentage/100);
int nAmountSaved = nOriginalSalePrice - nSalePrice;
cout << "The sale price of " << nProductName << "is $" << nSalePrice
<< " your amount saved is $" << nAmountSaved << endl;
system("PAUSE");
return 0;
}
I just can't figure out why the program doesn't stop and ask me what the cost of the product is.. and wait for an input. I think that's all I'm trying to say
"Enter the original sale price of the product:"
Example: 2:50
So you I want it to be able to let me type these in and remember them under the variable they're being set to.
So I think ProductName should be the only one saved as a char variable.
All the others should be a float or a double opposed to the int if what I'm reading is correct.
(Sorry about the 'n' and 'c' the book had taught me to put them in front of the name so you remember what type it was.)
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Welcome to Profit Percentage and Discount Price Calculator." << endl;
char ProductName;
cout << "Enter the name of the product: " << endl;
cin >> ProductName;
double ProductCost;
cout << "Enter the cost of the product: " << endl;
cin >> ProductCost;
double OriginalSalePrice;
cout << "Enter the original sale price of the product: " << endl;
cin >> OriginalSalePrice;
double ProfitPercentage = ((OriginalSalePrice-ProductCost)/OriginalSalePrice)*100;
cout << "The profit percentage is $" << ProfitPercentage << endl;
double DiscountPercentage;
cout << "Enter the discount percentage: " << endl;
cin >> DiscountPercentage;
double SalePrice = OriginalSalePrice-OriginalSalePrice*(DiscountPercentage/100);
double AmountSaved = OriginalSalePrice - SalePrice;
cout << "The sale price of " << ProductName << " is $" << SalePrice
<< " your amount saved is $" << AmountSaved << endl;
system("PAUSE");
return 0;
}
It doesn't give me a whole lot of number that I don't understand where they are coming from any more, but I'm still having the problem of the program not stopping and letting me type in the rest of the rest of what needs input
Yes you're correct: int values are a sequence of decimal digits only, double and float value must contain exactly on dot ('.'). You should check whether input failed. Otherwise following input statements may also fail. Have a look at Cplusplus reference at this side for IOstream.
1 2 3 4
if (! (cin >> nProductCost))
{
// Failes if operator>>() couldn't read a number from cin.
}
Results may also become curious when giving a double value where an int will be expected and vice versa.
I understand that, but that's not solving my problem of the program not allowing me to type in anything. Like after I am prompted, "Enter the name of the product", I type for example: "Apple".
Then, the whole program just skips ahead and starts spitting everything out. Without waiting for me to input to the next answer for the following question.
- I'm just going to take your advice and try to right this with strings, Ill be back once I'm done.
Please: You really have to check input operations for success!
You've defined ProductName as a one character variable. It can take only the 'A' from "Apple". Your process will now try to take the remaining character, namely "pple" as input for the following numbers. This indeed fails!