Banging My Head Against The Wall

Hello,

New to C++ and doing well in a class, but can't seem to understand where I'm going wrong here. Essentially I'm trying to calculate credit limits for different customers, and have an overall set of counters and accumulators to count the number of customers processed and accumulate the total available credit. Any ideas where I'm going wrong? It seems as though the program is working but the calculations are all wrong. Also, my if statement doesn't seem to working, and neither does my totals function.

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
129
130
131
/* 
VARIABLE DICTIONARY:

		VARIABLE							TYPE						REPRESENTS

		credit_limit						double						credit limit
		customer_balance					double						customer balance
		customer_credit						double						customer credit
		customer_purchases					double						customer purchases
		exceeded_credit						double						exceeded credit
		increment							double						increment
		number_of_customers					double						number of customers
		remaining_credit					double						remaining credit
		total_balance						double						total balance
		total_purchases						double						total purchases
		total_available_credit				double						total available credit
		account_number						int							account number
		first								string						first
		first_name							string						first name
		reply								string						reply
		last_name							string						last name

*/

#include <iostream>
#include <string>
using namespace std;

double credit_limit, customer_balance, customer_credit, customer_purchases,  exceeded_credit, increment, number_of_customers, remaining_credit, total_balance, total_purchases, total_available_credit;
int account_number;
string first, first_name, reply, last_name;

void setup();
void input();
void processing();
void totals();

int main()
		{
			system ("cls");
			cout << "Do you want to run a credit report (y/n)?" << endl;
			cin >> reply;

			while (reply != "y" && reply != "n")
				{
				cout << "User Error" << endl;
				cout << "User must answer y for yes or n for no" << endl;
				cout << "Please enter y or n" << endl;
				cin >> reply;
				}
			while (reply == "y")
				{
				if (first == "y")
					{
						setup();
						first = "n";
					}
				system ("cls");
				input();
				processing();
				cout << "Would you like to run the report again?" << endl;
				cin >> reply;

				while (reply != "y" && reply != "n")
					{
					cout << "User Error" << endl;
					cout << "User must answer y for yes or n for no" << endl;
					cout << "Please enter y or n" << endl;
					cin >> reply;
					}
				}
			if (first == "n")
				{
					totals();
				}
			system ("pause");
			return 0;
		}

void setup()
		{
			credit_limit = 1500;
			increment = 0;
			number_of_customers = 0;
			total_balance = 0;
			total_purchases = 0;
			total_available_credit = 0;
		}
void input()
		{
			cout << "Enter Account Number" << endl;
			cin >> account_number;
			cout << "Enter Last Name" << endl;
			cin >> last_name;
			cout << "Enter First Name" << endl;
			cin >> first_name;
			cout << "Enter Customer Balance" << endl;
			cin >> customer_balance;
			cout << "Enter Customer Purchases" << endl;
			cin >> customer_purchases;
		}
void processing()
		{
			system ("cls");
			number_of_customers = number_of_customers + increment;
			total_balance = total_balance + customer_balance;
			total_purchases = total_purchases + customer_purchases;
			customer_credit = customer_balance + customer_purchases;
			exceeded_credit = customer_credit - credit_limit;
			remaining_credit = credit_limit - customer_credit;
			total_available_credit = remaining_credit + total_available_credit;

			if (customer_credit < credit_limit)
				{
					cout << "The following credit report is prepared for " << first_name << " " << last_name << "." << endl;
					cout << "You have $" << remaining_credit << " of available credit in account " << account_number << "." << endl;
				}
			else
				{
					cout << "The following credit report is prepared for " << first_name << " " << last_name << "." << endl;
					cout << "You have exceeded your credit limit by $" << exceeded_credit << " in account " << account_number << "." << endl;
				}
			
		}
void totals()
		{
			cout << "Total number of customers processed             " << number_of_customers << endl;
			cout << "Total balance due for all customers is          " << total_balance << endl;
			cout << "Total purchases for all customers is            " << total_purchases << endl;
			cout << "Total available credit for all customers is     " << total_available_credit << endl;
		}
Having that many uninitialized global variables tend always to cause problems.

In your case: line 53 is never true and hence setup() is never called, so that you calculate with random 0 values


[EDIT]
Ok: global variables are set to 0 or the constructor is called.
Last edited on
At line 105,
 
    number_of_customers = number_of_customers + increment;

the global variable increment (which has the value zero) is added to the double representing the number of customers.

number_of_customers should be an integer (unless you anticipate there being a fraction of a customer) and line 105 should simply read:
 
    number_of_customers++;

Your doing it wrong
Banging My Head Against The Wall
You get more code if you bang your head on Keyboard....

I don't see the problem, can you post the input and output ?
If you could point out which calculations were wrong, that might help.

One thing that I see is your not declaring your variables. That fixes a lot of problems.
int account_number;
should be
int account_number=0;
So I made a change in a few places and the calculations now work fine, but my new problem is the output should display the number of customers processed, balance due for all customers, total purchases for all customers and total credit available for all customers. Here's my new code, what do you think?

VARIABLE DICTIONARY:

VARIABLE TYPE REPRESENTS

credit_limit double credit limit
customer_balance double customer balance
customer_credit double customer credit
customer_purchases double customer purchases
exceeded_credit double exceeded credit
increment double increment
number_of_customers double number of customers
remaining_credit double remaining credit
total_balance double total balance
total_purchases double total purchases
total_available_credit double total available credit
account_number int account number
first_name string first name
last_name string last name
reply string reply

*/

#include <iostream>
#include <string>
using namespace std;

double credit_limit, customer_balance, customer_credit, customer_purchases, exceeded_credit, increment, number_of_customers, remaining_credit, total_balance, total_purchases, total_available_credit;
int account_number;
string first_name, last_name, reply;

void setup();
void input();
void processing();
void totals();

int main()
{
system ("cls");
cout << "Do you want to run a credit report (y/n)?" << endl;
cin >> reply;

while (reply != "y" && reply != "n")
{
cout << "User Error" << endl;
cout << "User must answer y for yes or n for no" << endl;
cout << "Please enter y or n" << endl;
cin >> reply;
}
while (reply == "y")
{
if (reply == "y")
{
setup();
reply = "n";
}
system ("cls");
input();
processing();
cout << "Would you like to run the report again?" << endl;
cin >> reply;

while (reply != "y" && reply != "n")
{
cout << "User Error" << endl;
cout << "User must answer y for yes or n for no" << endl;
cout << "Please enter y or n" << endl;
cin >> reply;
}

}
if (reply == "n")
{
totals();
}
system ("pause");
return 0;
}

void setup()
{
credit_limit = 1500;
increment = 1;
number_of_customers = 0;
total_balance = 0;
total_purchases = 0;
total_available_credit = 0;
}
void input()
{
cout << "Enter Account Number" << endl;
cin >> account_number;
cout << "Enter Last Name" << endl;
cin >> last_name;
cout << "Enter First Name" << endl;
cin >> first_name;
cout << "Enter Customer Balance" << endl;
cin >> customer_balance;
cout << "Enter Customer Purchases" << endl;
cin >> customer_purchases;
}
void processing()
{
system ("cls");
number_of_customers = number_of_customers + increment;
total_balance = total_balance + customer_balance;
total_purchases = total_purchases + customer_purchases;
customer_credit = customer_balance + customer_purchases;
exceeded_credit = customer_credit - credit_limit;
remaining_credit = credit_limit - customer_credit;
total_available_credit = remaining_credit + total_available_credit;

if (customer_credit < credit_limit)
{
cout << "The following credit report is prepared for " << first_name << " " << last_name << "." << endl;
cout << "You have $" << remaining_credit << " of available credit in account " << account_number << "." << endl;
}
else
{
cout << "The following credit report is prepared for " << first_name << " " << last_name << "." << endl;
cout << "You have exceeded your credit limit by $" << exceeded_credit << " in account " << account_number << "." << endl;
}

}
void totals()
{
cout << "Total number of customers processed " << number_of_customers << endl;
cout << "Total balance due for all customers is " << total_balance << endl;
cout << "Total purchases for all customers is " << total_purchases << endl;
cout << "Total available credit for all customers is " << total_available_credit << endl;
}
It's a lot of code for us to dig through. Can you provide an example of your input, the results you're getting, and the results that you expect? This will make it easier for people to understand what's wrong and find the problem.
Will do, soon as I get in front of my home computer.
OK, so essentially what I'm trying to get this program to do is take the information and accumulate the total balance, total purchases, and total available credit, as well as count the number of customers that I've run through the program. It's doing none of the accumulation end. All the calculation is working correctly, but I cannot get the counters/accumulators to work, mostly because I can't wrap my head around how the coding breaks down for an accumulator. If someone could help me not only understand the accumulation code but implement it in the correct place, I would be very grateful.

I do understand conceptually how a "while" loop should work, but I can't seem to understand how it will apply to this program.
I really wish you would have provided an example of the inputs and results like I asked. It would have made the problem easier to find and someone probably would have jumped in with the answer by now.

In any case, I compiled your code and ran it myself. Your problem is very simple: you're calling setup() each time you generate a credit report and setup() sets your total values back to zero. Move the call to setup() to the top of main() and the program will work.
That doesn't fix my problem. Here's some of the input:

Account Number 3434
Last Name L
First Name F
Balance 343
Purchases 1800

"The following credit report is prepared for f l.
You have exceeded your credit limit by $643 in account 3434.
Would you like to run the report again?"

I ran the same information through twice after answering "y" to the last question and my output looks like this:

"Total number of customers processed - 1 (should be 2)
Total balance due for all customers is 343 (should be 686)
Total purchases for all customers is 1800 (should be 3600)
Total available credit for all customers is -643 (should be 1286)"

My accumulators aren't working still. Any thoughts?
I only want my numbers to initialize in setup once at the beginning so that my counters don't reset.
I only want my numbers to initialize in setup once at the beginning so that my counters don't reset.

Yes, and in order to achieve that, dhayden recommended that you "Move the call to setup() to the top of main()". Did you do that?
Yes, but is there an alternate way to do this? Part of the instructions were only to initialize the numbers IF the user wanted to run the program, not automatically initializing when the program is booted.
Then place the call to setup() after the user has replied "y" near the start of main(). But make sure that it is not inside the main while loop.
I understand, thank you for the help it's working fine now.
use static variables.
Topic archived. No new replies allowed.