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;
}
|