用C++判断从键盘输了入几个整数 , 每个数隔一个空格 (菜鸟求教) 望各位高手能指点下 小生感激不尽

#include<stdio.h>
int main()
{
int n,i,min;
int a[2000];//定义一个数组,其实2000可变的
printf("Please input n datas that you want:\n");
scanf("%d",&n);//输入想输入数的个数
不用用户自己输入所输入的个数的
奈何我不懂VB 各位高手能花点时间给我编个不?先谢过各位的回答 虽然我还是不懂 但是至少知道了可以用VB去做 谢啦

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>

using namespace std;
/*
忽略了非数字的输入

如果想仅输入空格和数字,在循环中遇到所有非数字、空格就break;
*/

int main()
{
vector<int> v;
int n;
char ch;

cin.get(ch);
while(ch != 10)
{
if (isdigit(ch))
{
cin.unget();
cin >> n;
v.push_back(n);
}
cin.get(ch);
}

for(int i=0;i<v.size();++i)
cout << i<<'\t'<<v[i]<<endl;

getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-02
按照你的意思,不能定义固定数组2000,要是你输入的数据大于2000,怎么办,就不是按照你说的想输入多少输入多少!不知道我输得对不对,数组的长度是固定的。问题没搞清楚不能帮你解决追问

就是想不固定数值的个数 也不用输入者自己输入所输入的个数 用程序让电脑自己判断

追答

那你就用VB去实现,用数组

第2个回答  2011-09-02
数组 可变的话,需要动态分配内在的数组:
int* a = NULL;
scanf("%d",&n);//输入想输入数的个数

if (n > 0)
a = (int*)malloc(n*sizeof(int));

delloc(a);
第3个回答  2011-09-02
#include <iostream>
using namespace std;
void main()
{
int num=0;
char ch;
cin.get(ch);
while(ch!='\n')
{
if(ch-'0'>0&&'9'-ch>0)
{
num++;
}
cin.get(ch);
}
system("pause");
}
相似回答