Function to read data types, help!

I have to create a program which asks the user (through a function) to enter data depending on the data type of the following arrays:

int a[5];
char c[6];
float d[4];
string s[4];


I have the following problems:

My program executes fine if the user enters the EXACT number of data, for example:

5 integers, 6 chars, 4 floats, 4 strings.

If the user enters 6 integers, then the last integer will carry over to the char array, etc. How do I avoid this? How do I create a limitation for the user?

Another problem I have is with my string array, it takes MORE than 4 strings, why?

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
114
115
116
117
118
119
120
121
#include <iostream>
#include <string>
#include <ctime>
#include <array>

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
	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++)
	{
		if (i != size) //Disables the user to input more than the size data requirement 
		{
			cin >> x[i], cin.ignore();
		}
		else
		{
			break;
		}
	}

}
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++)
	{
		getline(cin, x[i]);
	}
}
template <class T>
void DisplayData(T x[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << x[i] << " ";
	}
	cout << endl;
}


any help please!
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
#include <iostream>
#include <string>

using namespace std;

template <class Ta>
void read(Ta x[], int size)
{
	for (int i = 0; i < size; i++)	
		cin >> x[i] ;	
}

template <class Te>
void show(Te x[], int size)
{
	for (int i = 0; i < size; i++)	
		cout << x[i] << " " ;	
}


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

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

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

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

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

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

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

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

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

	return 0;
}
As you expect for your program to work with human and not a script, data would be sent one line at a time. that means after each input there will be at least single newline in addition to (possible) extra input.

You can use ignore to skip everything past last input:
1
2
3
4
5
6
7
8
#include <limits>

/*...*/

std::cout << "Enter 5 integer numbers: ";
read(a, 5); 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//Same for other inputs 


And use anup30 templated input/output. It is simplier and cleanier
Topic archived. No new replies allowed.