用C++语言,实现从键盘输入一组整数数据,直至遇到回车键结束输入(不能事先规定数组的长度,不用for)

2025-02-26 18:33:46
推荐回答(1个)
回答1:

#include
#include

using namespace std;

int main()
{
vector vecInt;

printf("now you can input your number('q' to exit):\r\n");
while (1)
{
char str[1024] = {0};
printf("number: \r\n");
scanf("%s", str);
if (str[0] == 'q' && str[1] == 0)
break;
int nNum = atoi(str);
vecInt.push_back(nNum);

printf("okay, go on(press 'q' to exit)\r\n");
}

printf("the following is that you inputed numbers: \r\n");
size_t pos = 0;
while (pos {
printf("%d ", vecInt[pos ++]);
}

printf("\r\n-----the end, press any key to exit--------\r\n");
getchar();
getchar();

return 0;
}

该代码在VC6中调试过,楼主要厚道啊,要给分啊,才10分啊,好廉价啊