I am struggling with a while loop please help

Write your question here.
I am making a program that that determines if someone has exceeded their credit limit. I have made a loop but when i execute i am in an infinite loop. Im looking in my book and can't find anything that looks similar to what i'm doing please help.
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
#include <iostream>
using namespace std;
int main()
{

//variables
double beginningbal, totalchargesthismonth,
totalcreditsthismonth, creditlimit, newbalance, accountnumber;
string  name;

//user enters information

cout <<"Enter your name: " << endl;
cin >> name;

cout <<"Enter account number: " << endl;
cin >> accountnumber;

cout <<"Enter beginning balance: " << endl;
cin >> beginningbal;

cout <<"Enter total charges this month: " <<  endl;
cin >> totalchargesthismonth;

cout <<"Enter total credits this month: " <<  endl;
cin >> totalcreditsthismonth;

cout <<"Enter your credit limit: " <<  endl;
cin >> creditlimit;

newbalance = (beginningbal - totalchargesthismonth +
totalcreditsthismonth);

    while (newbalance > creditlimit
{
    cout <<"Credit limit Exceeded" <<  endl;

}

   while (newbalance <= creditlimit)
{
cout <<"There is an available balance " << endl;
}

return 0;
}
Last edited on
Welcome to the forums! Please remember to use code-tags when posting:) You aren't changing the variables (newBalance and creditLimit) inside your loops, so of course they will never exit. Make sure you understand how while-loops work, from reading this code I'm guessing that you actually want to use if-statements rather than while-loops.
Last edited on
what is a code tag
Surround your code with "[ code ]" and "[ /code ]" (without the quotes or spaces). It will make your code look like this:

int a = 42;
thanks for that tip! Now im trying to think of what to change my variables to so that they won't loop but if i did something like newbalnce++ wouldnt it loop and display "Credit limit exeeded" like 1000 times?
Hmm, the program is supposed to ask the user for info, and then tell him/her ONCE whether or not they have an available balance, right? If so it makes sense to just use an if-else-statement:

1
2
3
4
5
if (newbalance > creditlimit) {
	cout <<"Credit limit Exceeded" <<  endl;
} else {
	cout <<"There is an available balance " << endl;
}
Last edited on
That makes alot of since and I thought the same thing but the instructions says that i need to use a while loop sigh...
Hmm, I don't see the point of using a while-loop for this :P Are you sure that's exactly what the program is supposed to do?
these are the instructions

The program should use a while loop to read the input, calculate the new balances, determine
whether the new balance exceeds the credit limit, display a message “Credit Limit Exceeded” in
case the limit is exceed or “There is an available balance” otherwise.
Ah, ok. Then you need to read the input and calculate the new balance inside the second while-loop (get rid of the first one).
Last edited on
where i initialized new balance needs to be in the second while loop instead?
Also i need to remove the first while loop?
First you declare all the variables, like you've already done. Then get all the input from the user which will only be entered once. Then you enter a while-loop which checks if newBalance is less than creditLimit (like your second while-loop). In that loop you get input from the user about new credits and charges and calculate the new balance. After the loop you print the message saying that the credit limit has been exceeded :)
Im missing something you told me to do. i dont know how to go abut this part "In that loop you get input from the user about new credits and charges and calculate the new balance."


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
#include <iostream>
using namespace std;
int main()
{

//variables
double beginningbal, totalchargesthismonth,
totalcreditsthismonth, creditlimit, newbalance, accountnumber;
string  name;

//user enters information

cout <<"Enter your name: " << endl;
cin >> name;

cout <<"Enter account number: " << endl;
cin >> accountnumber;

cout <<"Enter beginning balance: " << endl;
cin >> beginningbal;

cout <<"Enter total charges this month: " <<  endl;
cin >> totalchargesthismonth;

cout <<"Enter total credits this month: " <<  endl;
cin >> totalcreditsthismonth;

cout <<"Enter your credit limit: " <<  endl;
cin >> creditlimit;

newbalance = (beginningbal - totalchargesthismonth +
totalcreditsthismonth);

    while (newbalance < creditlimit)
{
cout  <<"You have an available balance " << endl;
}
cout <<" Credit Limit exeeded " <<  endl;
return 0;
}
Last edited on
You're almost there, just need to move some stuff around :) I haven't tried this, but it should work:

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

#include <iostream>

using namespace std;

int main() {

	//variables
	double beginningbal, totalchargesthismonth,
	totalcreditsthismonth, creditlimit, newbalance, accountnumber;
	string  name;

	//user enters information
	cout <<"Enter your name: " << endl;
	cin >> name;

	cout <<"Enter account number: " << endl;
	cin >> accountnumber;

	cout <<"Enter beginning balance: " << endl;
	cin >> beginningbal;

	cout <<"Enter your credit limit: " <<  endl;
	cin >> creditlimit;

	cout <<"Enter total charges this month: " <<  endl;
	cin >> totalchargesthismonth;

	cout <<"Enter total credits this month: " <<  endl;
	cin >> totalcreditsthismonth;

	newbalance = (beginningbal - totalchargesthismonth + totalcreditsthismonth);

	while (newbalance < creditlimit) {
		cout << "You have an available balance " << endl;

		cout <<"Enter total charges this month: " <<  endl;
		cin >> totalchargesthismonth;

		cout <<"Enter total credits this month: " <<  endl;
		cin >> totalcreditsthismonth;

		newbalance = (beginningbal - totalchargesthismonth + totalcreditsthismonth);
	}

	cout <<" Credit Limit exeeded " <<  endl;

	return 0;
}
Yay it works i have 2 problems left

The first is i thought charge meant someone spent X amount of money
and Credit meant that they paid back X amount of money do i have that mixed up?
Woops, I made a mistake. Line 43 should be

newbalance = (newbalance - totalchargesthismonth + totalcreditsthismonth);
Im still having my charge and credit flip-flopped
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

#include <iostream>
using namespace std;
int main()
{

//variables
double beginningbal, totalchargesthismonth,
totalcreditsthismonth, creditlimit, newbalance, accountnumber;
string  name;

//user enters information

                cout <<"Enter your name: " << endl;
                cin >> name;

                cout <<"Enter account number: " << endl;
                cin >> accountnumber;

                cout <<"Enter beginning balance: " << endl;
                cin >> beginningbal;

                cout <<"Enter total charges this month: " <<  endl;
                cin >> totalchargesthismonth;

                cout <<"Enter total credits this month: " <<  endl;
                cin >> totalcreditsthismonth;

                cout <<"Enter your credit limit: " <<  endl;
                cin >> creditlimit;
newbalance = (beginningbal - totalchargesthismonth + totalcreditsthismonth);

while (newbalance < creditlimit) {
                cout << "You have an available balance " << endl;

                cout <<"Enter total charges this month: " <<  endl;
                cin >> totalchargesthismonth;

                cout <<"Enter total credits this month: " <<  endl;
                cin >> totalcreditsthismonth;

                newbalance = (beginningbal - totalchargesthismonth + totalcreditsthismonth);
        }

                cout <<" Credit Limit exeeded " <<  endl;
return 0;
)
Im not sure about charge/credits to be honest, haha :P
It's got them flipped. im switching stuff around and it still looks like this

Enter balance: (i say) 3000

Enter Charges: (i say) 3000

Enter Credits: (isay) 0

Enter Credit Limit: (i say) 5000

"You have an available ballance"

any ideas?
Topic archived. No new replies allowed.