need help :(

Hey guys, I'm kinda new to C++ and I keep having trouble getting this code to work when I enter yes for programmer_status. It works fine when I enter no. This is an unfinished code that is still missing alot but I wanted to fix this problem first before I moved on. What do I need to add or change to fix this?

This is the error that pops up.

Run-Time Check Failure #3 - The variable 'price' is being used without being initialized. (refers to line 54)

And this is the unfinished code

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
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	// Amount of items
	const double ITEM_COUNT_ONE = 2;
	const double ITEM_COUNT_TWO = 5;

	// Price of items
	const double rubberDuck = 2.00;

	// Discount rates
	const double programmer_COBOL = 0.00;
	const double language_COBOL = 0.00;
	const double programmer_NONCOBOL = 0.10;
	
	const double ITEMS_TWO = 0.15;		// More than two items
	const double ITEMS_FIVE = 0.10;		// More than five items

	double itemsCount;
	double price;


	int purchase_order;	// Number of purchase order to be processed

	cout << "Are you a programmer? (Please enter 'yes' or 'no')" << endl;
	string programmer_status;
	cin >> programmer_status;	

	cout << "How many items are there? (Please type a number)" << endl;
	cin >> itemsCount;

	
	if (programmer_status == "yes")
	{
		cout << "What programming language do you use?\n";
		string programmer_language;
		cin >> programmer_language;

		if (itemsCount <= ITEM_COUNT_ONE)
			 price = rubberDuck - (rubberDuck * itemsCount) - (rubberDuck * ITEMS_TWO) - (rubberDuck * programmer_NONCOBOL);
	}
	

	else if (programmer_status == "no")
	{
		if (itemsCount <= ITEM_COUNT_TWO)
			price = rubberDuck * itemsCount;
	}
	cout << programmer_status << ", " << price << endl;

	system ("pause");
	return 0;
}
Last edited on
Line 25 double price; should be double price = 0;
closed account (zybCM4Gy)
Better yet...line 44.

How many items are you entering and is it less than or equal to 2 items? If you enter 3 or more items, then price isn't going to be initialized.

Edit..

Actually it works for me. It just gives me a very very small number if I enter more than the number allowed and even if I only enter...

yes
1

I get -0.5. >_>;;;

yes, 3 gets me: -8.0939e-42

Edit 4

Perhaps this is a compiler issue... as gcc is just dandy with the code.
Last edited on
I did what Yanson said but any input that's 3 or bigger gives me 0 :(

Edit.

Nevermind, I see what I'm doing wrong. It's because I'm limiting the amount to less then 2. Got to fix that. :)

Thanks for the help!
Last edited on
That's because you never set the price if programmer_status is yes and itemsCount is bigger than ITEM_COUNT_ONE or if programmer_status is no and itemsCount is bigger than ITEM_COUNT_TWO.

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
if (programmer_status == "yes")
{
    cout << "What programming language do you use?\n";
    string programmer_language;
    cin >> programmer_language;

    if (itemsCount <= ITEM_COUNT_ONE)
         price = rubberDuck - (rubberDuck * itemsCount) - (rubberDuck * ITEMS_TWO) - (rubberDuck * programmer_NONCOBOL);
    // What if itemsCount > ITEM_COUNT_ONE ?
}


else if (programmer_status == "no")
{
    if (itemsCount <= ITEM_COUNT_TWO)
        price = rubberDuck * itemsCount;
    // What if itemsCount > ITEM_COUNT_TWO ?
}
Yea I noticed that when bigger numbers didn't work.

Edit.

This is the new code and I couldn't have solved it without you guys! Thanks!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	// Amount of items
	const double ITEM_COUNT_ONE = 2;
	const double ITEM_COUNT_TWO = 5;

	// Price of items
	const double rubberDuck = 2.00;

	// Discount rates
	const double programmer_NONCOBOL = 0.10;
	const double ITEMS_TWO = 0.15;		// More than two items
	const double ITEMS_FIVE = 0.10;		// More than five items

	double itemsCount;
	double price = 0;


	int purchase_order;	// Number of purchase order to be processed

	cout << "Are you a programmer? (Please enter 'yes' or 'no')" << endl;
	string programmer_status;
	cin >> programmer_status;	

	cout << "How many items are there? (Please type a number)" << endl;
	cin >> itemsCount;

	
	if (programmer_status == "yes")
	{
		cout << "What programming language do you use?\n";
		string programmer_language;
		cin >> programmer_language;

		if (programmer_language == "cobol")
		{
			if (itemsCount == itemsCount)
			price = rubberDuck * itemsCount;
		}
		else if (programmer_language == programmer_language)

			if (itemsCount < ITEM_COUNT_ONE)
				price = (rubberDuck * itemsCount) - ((rubberDuck * itemsCount) * programmer_NONCOBOL);
			else if (itemsCount >= ITEM_COUNT_ONE)
				price = (rubberDuck * itemsCount) - ((rubberDuck * itemsCount) * ITEMS_TWO) - (((rubberDuck * itemsCount) * ITEMS_TWO) * programmer_NONCOBOL);
			else if (itemsCount >= ITEM_COUNT_TWO)
				price = (rubberDuck * itemsCount) - ((rubberDuck * itemsCount) * ITEMS_TWO) - (((rubberDuck * itemsCount) * ITEMS_TWO) * ITEMS_FIVE) - ((((rubberDuck * itemsCount) * ITEMS_TWO) * ITEMS_FIVE) * programmer_NONCOBOL);
	}

	else if (programmer_status == "no")
	{
		if (itemsCount == itemsCount)
			price = rubberDuck * itemsCount;
	}
	cout << programmer_status << ", " << price << endl;

	system ("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.