interactive while loops

Hi, I am new to this forum and I am having trouble with my interactive while loop. I can't get my program to loop three times, it will only loop once and it won't register my formula and just keeps my inventory balance the same- when it's supposed to change. Any help would be most appreciated.


#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <stdio.h>

using namespace std;


int main()
{
const int MAXNUMS = 3;
int count;
double balance, received, sold, idNum;

cout << "\nThis program will ask you to enter "
<< MAXNUMS << " numbers.\n";
count = 3;
balance = 0;

cout <<setiosflags(ios::fixed) <<setprecision(3);

while (balance <= count){
cout << "Enter a book identification number\n";
cin >> idNum;
cout << "What is the inventory balance at the beginning of the month?\n";
cin >> balance;
cout << "What many copies were received this month?\n";
cin >> received;
cout << "How many copies were sold this month?\n";
cin >> sold;
balance = balance + received - sold;
cout << "The balance is now " << balance;
++count;
}
cout << "\n\nThe final balance is " << balance << endl;

cout<<"\n\n\nThank You.\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Because as soon as your loop runs, balance is no longer less than or equal to count.

1
2
3
while (balance<=count){
...
}


Guessing you meant to put
1
2
3
while (count<=MAXNUMS){
...
}


edit:
Er, that doesn't seem to have fixed the problem. Sorry.
Last edited on
Okay thanks I'll try changing that. Any other helpful pointers?
I see what it is now. Change "count = 3" to "count = 0" and the while loop in my previous post.

Helpful pointers? No, sorry. I'm still fairly new and I have asked for help a few times on here. But reading through some of the posts on this forum has helped me.
Last edited on
You could also use a for-loop.

If you want to do the while-loop:
1
2
3
4
5
6
7
// like kyiro37 said, change count to 0, because you want to count UP to a certain value

while (count < MAXNUMS)
{
	// Your code here
	count++;
}


If you want to do a for-loop:
1
2
3
4
for (int i = 0; i < MAXNUMS; i++)
{
	// Your code here
}
Last edited on
Hey, one more question. So, my program is going through three loops like it should and the formula is working too, but it is not giving me the correct final balance-- it's just giving me the results of the third loop. Any suggestions?
Last edited on
That's because your code tells it to do that!

I'm confused.

Can you please tell me what "balance" is the balance of?

Is it cash?
Last edited on
Topic archived. No new replies allowed.