如图,我现在需要这样的一个数组,假如我输入n = 4, 然后我依次输入
1 2
这里的1和2之间有个空格,2后面有个回车
最后得到的结果就是arr[0] = 1, arr[1] = 2,后面的为空
谢谢大家了
如图,我现在需要这样的一个数组,假如我输入n = 4, 然后我依次输入
1 2
这里的1和2之间有个空格,2后面有个回车
最后得到的结果就是arr[0] = 1, arr[1] = 2,后面的为空
谢谢大家了
因为你的要求是int,所以后面没有赋值的一般是初始为0
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cout << "Pls enter arr count: \n";
cin >> n;
int *a = new int[n];
memset(a,0x0,n*sizeof(int));//将数组全部元素赋予0
int i = 0;
int value = 0;
cout << "Pls enter exit number: \n"; //为了在n范围内退出输入循环
int exitNub = -1;
cin >> exitNub;
cout << "Pls enter your arr e.g: 1 2 " << exitNub << "(exit input)\n";
for(;i<n && cin >> value;i++)//i<n防止数组越界
{
if(value == exitNub) //在n的范围内遇到退出数则不再输入
break;
a[i] = value;
}
cout << "\n******************Output****************** " << endl;
for(int j=0;j<n;j++)
{
cout << a[j] << " ";
}
cout << endl;
return 0;
}