用C++语言,实现从键盘输入一组整数数据,直至遇到回车键结束输入(不能事先规定数组的长度,不用for)
1个回答
展开全部
#include <stdio.h>
#include <vector>
using namespace std;
int main()
{
vector<int> 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 <vecInt.size())
{
printf("%d ", vecInt[pos ++]);
}
printf("\r\n-----the end, press any key to exit--------\r\n");
getchar();
getchar();
return 0;
}
该代码在VC6中调试过,楼主要厚道啊,要给分啊,才10分啊,好廉价啊
#include <vector>
using namespace std;
int main()
{
vector<int> 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 <vecInt.size())
{
printf("%d ", vecInt[pos ++]);
}
printf("\r\n-----the end, press any key to exit--------\r\n");
getchar();
getchar();
return 0;
}
该代码在VC6中调试过,楼主要厚道啊,要给分啊,才10分啊,好廉价啊
追问
我没有在编译,问下你这遇到空格会如何?不会停止输入了吧!还有整数数组的长度不准事先申明,只能从输入中统计啊?全满足了吗?如果都满足的话,你就心安理得的拿走这10分吧!不然,你还最好不上哦!我真的很穷啊!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询