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
|
#include <iostream>
#include <cstring>
using namespace std;
void process(int *&data, int size)
{
for(int i=0;i<size;++i)
{
cout<<"Number ["<<i<<"]: ";
cin>>data[i];
cin.clear();
cin.ignore(1000,'\n');
}
}
void output(int *data, int size)
{
for(int i=0;i<size;++i)
cout<<data[i]<<' ';
cout<<'\n';
}
int main()
{
char x[6];
int y, *z;
while(cout<<"Enter a string (input or output): "){
cin.getline(x,7,'\n');
if(strcmp(x,"input")==0){//strcmpi is non standard for c++
cout<<"Enter a size: ";
cin>>y;
cin.clear();
cin.ignore(1000,'\n');
delete []z;
z = new int[y];
process(z,y);
}else if(strcmp(x,"output")==0){//strcmpi is non standard for c++
output(z,y);
}else{
delete []z;
return 0;
}
}
}
|
Enter a string (input or output): input
Enter a size: 3
Number [0]: 5
Number [1]: 7
Number [2]: 2
Enter a string (input or output): output
5 7 2
Enter a string (input or output): exit
Process returned 0 (0x0) execution time : 18.553 s
Press any key to continue. |