Ignoring whitespace?

I have to create a function which reads 6 characters from the user and stores it into an array:

char c[6];



1
2
3
4
5
6
7
void ReadData(char x[], int size) //for char data type
{
	for (int i = 0; i < size; i++)
	{
		cin.get(x[i]);
	}


When I output what the user entered for the characters, it does not show all of the chars.

the whole code:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <ctime>
#include <array>
#include <iomanip>

using namespace std;

void ReadData(int  x[], int size);
void ReadData(char x[], int size);
void ReadData(string x[], int size);
void ReadData(float x[], int size);
template<class T>
void DisplayData(T x[], int size);

int main()
{
	int a[5];
	char c[6];
	float d[4];
	string s[4];

	cout << "Enter 5 integer numbers: ";
	ReadData(a, 5); 

	cout << "This is array a: ";
	//Display integer a
	DisplayData(a, 5);
	cout << endl;

	cout << "Enter 6 characters: ";
	ReadData(c, 6);

	cout << "This is array c: ";
	//Display char c
	DisplayData(c, 6);
	cout << endl;

	cout << "Enter 4 decimal numbers: ";
	ReadData(d, 4);

	cout << "This is array d: ";
	//Display float d
	cout << setprecision(2);
	DisplayData(d, 4);
	cout << endl;

	cout << "Enter 4 names: ";
	ReadData(s, 4);

	cout << "This is array s: ";
	//Display string s
	DisplayData(s, 4);
	cout << endl;




	system("PAUSE");
	return 0;
}
void ReadData(int x[], int size) //for int data type
{
	for (int i = 0; i < size; i++)
	{
		if (i != size) //Disables the user to input more than the size data requirement 
		{
			cin >> x[i];
		}
		if (i == size)
		{
			break;
		}
	}
}
void ReadData(char x[], int size) //for char data type
{
	for (int i = 0; i < size; i++)
	{
		cin.get(x[i]);
	}

}
void ReadData(float x[], int size) // for float data type
{
	for (int i = 0; i < size; i++)
	{
		if (i != size) //Disables the user to input more than the size data requirement 
		{
			cin >> x[i];
		}
		else
		{
			break;
		}
	}
}
void ReadData(string x[], int size) // for string data type
{
	for (int i = 0; i < size; i++)
	{
		cin >> x[i];
	}
}
template <class T>
void DisplayData(T x[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << x[i] << " ";
	}
	cout << endl;
}
get is unformatted input. It will read everything, including things which formatted input skips. And there is at least one character left in input buffer after integer input: '\n', newline.
So your first character would be read as '\n'. Also if you use spaces between characters, spaces are character too and will be counted. Either use formattd input (if spaces are not needed to be recognised as characters) or clear buffer before input.
Can you clarify how I should read the character then?

Should I start with cin.ignore();

then


for (int i = 0; i < size; i++)
{
cin >> x[i];
}
Depending. Should you read space delimeded characters? Should you threat whitespace characters (tab, newline, space...) as characters? What about non-printing characters?
Topic archived. No new replies allowed.