Do while help

In this code, when the program is repeated, it skips over the salesperson's name entry. Can someone point out what I'm missing
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
string salespersonname;
int main()
{ char choice;
do{
double monthly_sales = 0.00, subtotal = 0.00;

cout << "Please enter the salespersons name: ? ";
getline (cin,salespersonname);

cout << "Please enter the sales from each month in the quarter."<<endl;

for (int count = 1; count <=3; count ++)
{do {
cout << "Month " << count << " Sales: $ ";
cin >> monthly_sales;
if (monthly_sales<0)
{
cout << "Not a vaild entry, please try again"<<endl;
}
subtotal = monthly_sales+subtotal;
}while (monthly_sales<0);
}

cout<<fixed<<showpoint<<setprecision(2);
cout << "The quarter average sales for "<< salespersonname << " is $"
<<subtotal/3<<endl;

cout<<endl<<"Would you like to perform other calculation?(Y/N)"<<endl;
cin >> choice;
}while (choice=='Y'||choice=='y');

}
Have you learned about cin.ignore() yet? I placed it in your code with the comments.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <limits> // To use std::numeric_limits.

using namespace std;
string salespersonname;
int main()
{
	char choice;
	do {
		double monthly_sales = 0.00, subtotal = 0.00;

		cout << "Please enter the salespersons name: ? ";
		getline(cin, salespersonname);

		cout << "Please enter the sales from each month in the quarter." << endl;

		for (int count = 1; count <= 3; count++)
		{
			do {
				cout << "Month " << count << " Sales: $ ";
				cin >> monthly_sales;
				if (monthly_sales < 0)
				{
					cout << "Not a vaild entry, please try again" << endl;
				}

				subtotal = monthly_sales + subtotal;
			} while (monthly_sales < 0);
		}

		cout << fixed << showpoint << setprecision(2);
		cout << "The quarter average sales for " << salespersonname << " is $"
			<< subtotal / 3 << endl;

		cout << endl << "Would you like to perform other calculation?(Y/N)" << endl;
		cin >> choice;

		// The cin.ignore function tells the cin object to skip one or more
		// characters in the keyboard buffer.
		// It skips the max number of characters or until a newline (\n)
		// is encountered.
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	} while (choice == 'Y' || choice == 'y');

}
Topic archived. No new replies allowed.