Why is first letter of string getting cut off on output?

Everything is working fine except that the "N" in "North" keeps getting cutoff in the output. Does anyone know why? I tried a lot to figure it out.

Thank you!

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>

using namespace std;

struct CorpData
{
	string division;
	double first,
		   second,
		   third,
		   fourth,
		   total,
		   average;
		   
	CorpData (string d = "unknown", double fi = 0, double se = 0, 
			  double th = 0, double fo = 0, double tot = 0, double avg = 0)
	{
		division = d;
		first = fi;
		second = se;
		third = th;
		fourth = fo;
		total = tot;
		average = avg;
	}
};

void getSales(CorpData &);

void setSales(const CorpData &);

int main(void)
{
	CorpData north, south, east, west;

	getSales(north);
	getSales(south);
	getSales(east);
	getSales(west);
	
	cout << " " << endl;
	setSales(north);
	cout << " " << endl;
	setSales(south);
	cout << " " << endl;
	setSales(east);
	cout << " " << endl;
	setSales(west);
	
	return 0;
}

void getSales(CorpData &sales)
{
	cout << "Enter the sales data for each division\n" << endl;
	cout << "Division: ";
	cin.get();
	
	getline(cin, sales.division);
	
	cout << "First Quarter Sales: ";
	cin >> sales.first;
	
	cout << "Second Quarter Sales: ";
	cin >> sales.second;
	
	cout << "Third Quarter Sales: ";
	cin >> sales.third;
	
	cout << "Fourth Quarter Sales: ";
	cin >> sales.fourth;	
}

void setSales(const CorpData &sales)
{
	cin.clear();
	cin.ignore(INT_MAX, '\n');
	
	cout << "Divison: " << sales.division << endl;
	cout << "Average Sales: " << ((sales.first + sales.second + sales.third + 
								  sales.fourth) / 4) << endl;
	cout << "Total Sales: " << (sales.first + sales.second + sales.third + 
								  sales.fourth) << endl;
}
You are doing a cin.get() on line 58 before you read the name of the division on line 60; this causes the first character to go into that get() instead of the sales.division string.

I suspect you are doing that because your formatted input on line 72 breaks the getline the next round through because of the trailing '\n' character. Instead of doing cin.get() there, just use cin.ignore() to ignore up to the trailing newline after you've read all your formatted input (i.e., after line 72).
just use cin.ignore() to ignore up to the trailing newline
And I prefer to use std::getline(std::cin >> std::ws. input): skips all leading whitespace characters, you do not need to check if previous input was formatted or not...

Only reason to not use it is if you need to store leading whitespcaes.
Thanks!
Topic archived. No new replies allowed.