Help With Code Keeps Crashing

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace 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;   
}
Last edited on
Works fine for me, it runs at least. But the output is probably not what you want.

Line 26 and 38/39 you shouldn't use cXXX, but nXXX if you want numbers printed.
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.

1
2
3
4
5
6
7
#include <string>

// ...

std::string productName;

cin >> productName;
@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.

I guess this is where I'm at now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace 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
Give an example of your input.

And when defining productName you should use it!

(Btw: Avoid those curious prefixes garbage like 'n' and 'c'. It may only confuse the reader of this program).
Okay,

So

"Enter the name of the product: "
Example: Apple

"Enter the cost of the product:"
Example: 1.50

"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.)

This is what I'm at:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace 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
Last edited on
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.




EDIT: Added code-tags.
Last edited on
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.
Last edited on
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!

Do declare ProductName as std::string.
Where are you wanting me to put ProductName as std::string exactly?

I tried writing this using strings - I failed terribly.. I'm ashamed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <string>
#include <sstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    string mystr;
    float ProductCost=0;
    float OriginalSalePrice=0;
    float SalePrice=0;
    float OriginalSalePrice=0;
    float ProfitPercentage=0;
    float DiscountPercentaage=0;
    float AmountSaved=0;
    char ProductName;

    cout << "Hello, welcome to the Profit Percentage and Discount Price"
         << " calculator." << endl;

    cout << "Enter the name of the product: ";
    getline (cin,mystr);
    stringstream(mystr) >> ProductName;

    cout << "Enter the cost of the product: ";
    getline (cin,mystr);
    stringstream(mystr) >> ProductCost;

    cout << "Enter the original sale price of the product: ";
    getline (cin,mystr);
    stringstream(mystr) >> OriginalSaleprice;

    cout << "The profit percentage of " << ProductName << " is "
         << ((OriginalSalePrice-ProductCost)/OriginalSalePrice)*100 << endl;

    cout << "Enter the discount percentage: ";
    getline (cin,mystr);
    stringstream(mystr) >> DiscountPercentage;

    cout << "The sale price of " << ProductName << " is "
         << SalePrice=OriginalSalePrice-OriginalSalePrice*(DiscountPercentage/100)
         << "/nThe amount saved by the shopper is $"
         << OriginalSalePrice-SalePrice << endl;

    system("PAUSE");
    return 0;


}
Please, I think its to late (even here in Europe). Better take a sleep and then have another look at your code.

Why do you now use stringsteams?
You cannot read an entire name of more then one character into one char-variable. Use std::string instead.

But better take a pause or sleep ;-)
Well, I got that Idea from www.cplusplus.com/doc/tutorial/basic_io/

Yeah, I understand I think it's time to quit for the night.

I'm not sure how to use the std::string and where to put it exactly.
Bump - I'm not sure if anyone can help or can rewrite what I'm trying to do, so I can have some source code to reference on what I've done wrong.

Thanks
I'll take a look at some point today, I'm no C++ expert still a beginner but I think I might be able to solve your issue
Can you post your current code, since you've modified it from your original post?
Topic archived. No new replies allowed.