adding loop

Sep 11, 2013 at 10:59pm
Well I am working on something that add's up any bills a person may have , but I want to have a loop that asks the user each time it starts do ayou have a bill if 'Y' then it goes and gets the bill amount then at the end it repeats and ask's the same things, but if the user does have more than 1 bill i need a loop that would keep track of each data input then add all of them up at the very end when the user puts 'N' for no.

Sep 11, 2013 at 11:08pm
sound like you need a for loop. if you're already using one, lets see your code
Sep 12, 2013 at 3:43am
Alright Well i added in a for loop but when I display the total it's just zero how do I add what was just gathered during the loop

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
#include <iostream>
#include <iomanip>

using namespace std;

double PayRate;
double Extra;
double Bill;
double Personal;
double Total;
double End_total;
double Bill_total;
double Hours;
int more;

int main()
{
	cout<<"Please enter your pay rate\n";
	cin >> PayRate;
	cout<<"Thank You\n";
	cout<<"Please enter your total hours worked\n";
	cin>>Hours;
	cout<<"Thank You\n";
	cout<<"Please enter any extra income this week\n";
	cin>>Extra;
	cout <<"Thank you.... one moment while totals are calculated\n";
	Total = PayRate*Hours-0.22+Extra;
	for (int Bill=0; Bill!=-1;Bill++)
	{
		cout<<"do you have a bill youd like to enter?\n";
		cin>>Bill;
		cout<<"Thank you"<<endl;
	}
	cout<<Bill<<endl;
	system("pause");
}

Last edited on Sep 13, 2013 at 12:37am
Sep 12, 2013 at 11:07pm
you know after trying it out I think a endless while loop would prove easier than a for loop. Something like this
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
#include <iostream>
#include <iomanip>

using namespace std;

double PayRate;
double Extra;
double Bill;
double Personal;
double Total;
double End_total;
double Bill_total;
double Hours;
int more;

int main()
{
	cout<<"Please enter your pay rate\n";
	cin >> PayRate;
	cout<<"Thank You\n";
	cout<<"Please enter your total hours worked\n";
	cin>>Hours;
	cout<<"Thank You\n";
	cout<<"Please enter any extra income this week\n";
	cin>>Extra;
	cout <<"Thank you.... one moment while totals are calculated\n";
	Total = PayRate*Hours-0.22+Extra;

	char ans = NULL;

	while(1>0)
	{
		cout<<"do you have a bill youd like to enter?\n";
		cin>>ans;
		if(ans == 'y' || ans == 'Y')
		{
			cout<<"Enter the amount of the bill.\n";
			cin>>Bill;
			Bill_total += Bill;	
			continue;
		}
		if(ans == 'n' || ans == 'N')
		{
			break;
		}
		else
		{
			cout<<"Invalid Choice try again.\n";
		}
	}
	cout<<Bill_total<<endl;
	system("pause");
}


that should work, I tried it out.

EDIT: Oh yea and when you post code use the <> thing under format, it looks much nicer and it's easier to read.
Last edited on Sep 12, 2013 at 11:19pm
Topic archived. No new replies allowed.