Help

I somewhat of an idea on what I'm am doing, but could use some help. I do have a few questions, but I want to try to figure most of them out. My first hurdle is why is my output skipping a line? I also know my calculations are off, but I'll try to figure it out.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	//variables
	char ch;
	int registered = 0;
	int tot_registered = 0;
	double cost = 0.0;
	double ave = 0.0;
	
	//ask for number of people attending
	cout << "Enter the number of registrants(q to quit): ";

	//loop
	while (true)
	{

		//calulations
		cin >> ch;
		if (ch == 'q')
		{
			break;
		}
		cin >> registered;
		if (registered >= 1 && registered <= 3)
		{
			cost = registered * 150;
		}
		else if (registered >= 4 && registered <= 9)
		{
			cost = registered * 100;
		}
		else if (registered >= 10)
		{
			cost = registered * 90;
		}
		else
		{
			cout << "Invalid Input" << endl;
		}
		cout << "Enter the number of registrants(q to quit): ";
		tot_registered += registered;
		ave = cost / tot_registered;

	}
	cout << fixed << setprecision(2);
	cout << "The total number of registrants: " << tot_registered << endl;
	cout << "Total charge for the registrants: $" << cost << endl;
	cout << "Average charge per registraint: $" << ave  << endl;
	return 0;

}[/code]
Last edited on
code tags help a lot.
I made a couple of tiny changes (do while loop, moved cin and cout around a little for the new loop, etc), is this what you had in mind?

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

int main()
{
//variables
char ch {0};
int registered = 0;
int tot_registered = 0;
double cost = 0.0;
double ave = 0.0;

//ask for number of people attending
cout << "Enter the number of registrants(q to quit): ";

//loop
//while (true)
do
{

//calulations
cout << "Enter the number of registrants(q to quit): ";
cin >> registered;
if (registered >= 1 && registered <= 3)
{
cost = registered * 150;
}
else if (registered >= 4 && registered <= 9)
{
cost = registered * 100;
}
else if (registered >= 10)
{
cost = registered * 90;
}
else
{
cout << "Invalid Input" << endl;
}
tot_registered += registered;
ave = cost / tot_registered;
cout << "quit?: ";
cin >> ch;
} while(ch != 'q');

cout << fixed << setprecision(2);
cout << "The total number of registrants: " << tot_registered << endl;
cout << "Total charge for the registrants: $" << cost << endl;
cout << "Average charge per registraint: $" << ave << endl;
return 0;
}
Last edited on
Sorry, just looked up how to use code tags and edited it. Your code shows "quit?" after every input. I was trying to get it to just show "Enter the number of registrants(q to quit)" every line until you enter "q". Thanks for the help by the way.
to do that you have to read their input as a string, convert it to a number if you can, if it fails, see if its a "q" (not 'q' now, its a string now) and if so, stop, else invalid junk typed... like that. Can you do it? You can use stoi() to convert string to int, if you want. Its the easiest way for this program.
Last edited on
Not sure what your output should be, but consider:

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

int main()
{
	size_t registered {};
	size_t tot_registered {};
	double cost {};

	while ((std::cout << "Enter the number of registrants (q to quit): ") && (std::cin >> registered)) {
		tot_registered += registered;

		if (registered >= 1)
			if (registered <= 3)
				cost += registered * 150;
			else if (registered <= 9)
				cost += registered * 100;
			else
				cost += registered * 90;
		else
			std::cout << "Invalid Input\n";
	}

	const double ave {cost / tot_registered};

	std::cout << std::fixed << std::setprecision(2);
	std::cout << "The total number of registrants: " << tot_registered << '\n';
	std::cout << "Total charge for the registrants: $" << cost << '\n';
	std::cout << "Average charge per registrant: $" << ave << '\n';
}

This is kind of what I was looking for. Thank you!
Topic archived. No new replies allowed.