Error in the ouput for quantity of 0

First of all, thank you for reading this. This code is for a project in my college class. For every scenario this program works just fine. The only issue I am having is when the user enters a value that is 0 or negative. The program is supposed to just print "The number of units must be 1 or more" and exit the program in the while loop for the getQuantity function, but for some reason it also prints "The total sale for the customer is $0.00" which is in the displaySale function. Any help with this would be greatly appreciated. I just can't figure out where the issue is coming from.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

// constants used by the program

// functions needed for the application
int getQuantity();
double getDiscount(int quantity);
bool isPreferred(int quantity);
double calculateSale(int quantity, double discount);
void displaySale(int quantity, double discount, double revenue);
void displayPreferred();

const double RETAIL_PRICE = 99.0; 
const int PREFERRED_CUSTOMER_LEVEL = 250;

int _tmain(int argc, _TCHAR* argv[])
{
	int quantity= 0;
	double discount = 0.0;
	double revenue = 0.0;
	
	quantity = getQuantity();
	discount = getDiscount(quantity);
	revenue = calculateSale(quantity, discount);
	displaySale(quantity, discount, revenue);
	isPreferred(quantity);

	return 0;
}

int getQuantity()	
{
	int quantity;

	cout << "Enter the number of units sold: ";
	cin >> quantity;

	while (quantity <= 0)
	{
		cout << "The number of units must be 1 or more\n";
		
		return 0;
	}

	return quantity;
}

double getDiscount(int quantity_units)
{
	double discount = 0;

	if (quantity_units >= 1)
	{
		discount = 0;
	}

	if (quantity_units >= 10)
	{
		discount = .1;
	}

	if (quantity_units >= 20)
	{
		discount = .15;
	}

	if (quantity_units >= 50)
	{
		discount = .2;
	}
	
	if (quantity_units >= 100)
	{
		discount = .25;
	}

	return discount;
}

double calculateSale(int quantity_units, double discount_percent)
{
	double revenue;

	revenue = (RETAIL_PRICE * quantity_units) - (discount_percent * (RETAIL_PRICE * quantity_units));

	return revenue;
}

void displaySale(int quantity_units, double discount_percent, double revenue_total)
{
	if (quantity_units >= 1 && quantity_units <= 9)
	{
		cout << "The customer purchased " << quantity_units << " units and does not qualify for a discount\n";
	}
	if (quantity_units > 9)
	{
		cout << "The customer purchased " << quantity_units << " units and qualified for a " << (discount_percent) * 100 << "% discount\n";
	}

	cout << fixed << setprecision(2);
	cout << "The total sale for the customer is $" << revenue_total;
	cout << endl;
}

bool isPreferred(int quantity_units)
{
	bool preferred;
	
	if (quantity_units >= PREFERRED_CUSTOMER_LEVEL)
	{
		preferred = true;
		displayPreferred();
	}
	else
	{
		preferred = false;
	}
	
	return preferred;
}

void displayPreferred()
{
	cout << "The customer is a preferred customer" << endl;
}
Last edited on
closed account (13bSLyTq)
Hi,

I did not test however I think the error lies in getQuantity() function in specific the loop\while. I would recommend you change the function into:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int getQuantity()	
{
	int quantity;

	cout << "Enter the number of units sold: ";
	cin >> quantity;

         if(quantity < 1) // If user inputs anything smaller than 1.
	{
		cout << "The number of units must be 1 or more\n";
		
		return 0;
	}

	return quantity;
}
The problem is that you never actually exit the program if the quantity is less than 1.

Easiest way to fix that would probably be
25
26
27
quantity = getQuantity();
if (quantity < 1)
    return 0;
Thank you so much for replying. I've changed the code but I am still receiving the same output message.

This is what the output looks like...

Enter the number of units sold: 0
The number of units must be 1 or more
The total sale for the customer is $0.00

I'm racking my brain as to why the total sale would print, especially considering that the rest of the function displaySale does not
You probably didn't change the code correctly.

What I meant was to change your main function so that it reads
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int _tmain(int argc, _TCHAR* argv[])
{
    int quantity= 0;
    double discount = 0.0;
    double revenue = 0.0;
	
    quantity = getQuantity();
    if (quantity < 1) // Add this
        return 0;
    discount = getDiscount(quantity);
    revenue = calculateSale(quantity, discount);
    displaySale(quantity, discount, revenue);
    isPreferred(quantity);

    return 0;
}

Another option would be to #include <cstdlib> and change your getQuantity function to
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int getQuantity()	
{
    int quantity;

    cout << "Enter the number of units sold: ";
    cin >> quantity;

    if (quantity <= 0)
    {
        cout << "The number of units must be 1 or more\n";

        exit(0); // <-- Here
    }

    return quantity;
}
Thank you so much! I added the if statement in the main function and now it works just fine. You saved my sanity. I'm still not 100% sure why I would need two return 0; calls, but I'll dig around in my book and figure it all out. Thanks again.
Last edited on
The return 0; in getQuantity doesn't exit the program.
It simply returns 0 to the function that called it -- namely, the main function, where it gets stored into the quantity variable.
Ahhh. Now I understand.
Topic archived. No new replies allowed.