Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取字符列表并存储在数组中_C++_C - Fatal编程技术网

C++ 读取字符列表并存储在数组中

C++ 读取字符列表并存储在数组中,c++,c,C++,C,我对下面的代码有问题,我不明白为什么不打印out of loop。使用此代码,我希望程序忽略用户输入的任何空格,在输入空格后,先前输入的数字存储在数组位置。像这样,我希望6和78存储在2个数组位置,而不是单独存储为6 7 8 这是我的代码: while ((in=getchar()) != '0') { if (in == ' ') { printf("space\n "); continue; } else {

我对下面的代码有问题,我不明白为什么不打印out of loop。使用此代码,我希望程序忽略用户输入的任何空格,在输入空格后,先前输入的数字存储在数组位置。像这样,我希望6和78存储在2个数组位置,而不是单独存储为6 7 8

这是我的代码:

while ((in=getchar()) != '0')
{
    if (in == ' ')
    {
        printf("space\n ");
        continue;
    }
    else
    {
        printf("assigning\n ");
        input[i]=in;
    }
    i++;
}
printf("Out of Loop");
输入5 6 78时,我的输出为: 分配 空间 分配 空间 分配 分配 分配

有了这个输出,我怀疑78是否存储在一个内存位置

非常感谢您的帮助,谢谢您

std::vector<int> v;
std::string s;
int i;

std::getline( std::cin, s);         // read full line with whitespaces
std::istringstream iss( s);         // prepare to process the line
while( iss >> i) v.push_back( i);   // read into i and push into vector if 
                                    // operator>> was successful
std::vector v;
std::字符串s;
int i;
std::getline(std::cin,s);//阅读带空格的整行
std::istringstream iss(s);//准备处理生产线
而(i)v.推回(i);//读入i并按入向量,如果
//操作员>>成功
C:

int数组[10];
int i=0,返回值;
而(i<10&(retval=scanf(“%d”)和数组[i++])==1);
如果(i==10){
//阵列已满
}
如果(retval==0){
//读取值不是整数。匹配失败
}
如果(retval==EOF){
//到达文件结尾或发生读取错误
}

您正在一个字符一个字符地决定。因此,您将只存储单个数字或忽略这些数字

您可以这样存储整数(扩展代码):


首先,我非常怀疑while循环是否会结束,即使是对
'\0'
,因为您使用
char
变量来存储输入。不是字符串,只有字符串使用
'\0'
结尾,我们如何从键盘输入'\0'。。???。即使您想将其保持为
'0'
,您也必须输入0作为结束循环的最后一个数字(我认为您不想这样做)

因此,解决方案是:-

输入数字后,您将按ENTER键,该键将生成换行符“\n”,因此您必须检查换行符(
'\n'
),当您使用getchar()函数时,它将在输入结束时返回EOF(-1),所以检查它也很重要。所以在while循环中必须同时检查“\n”和EOF。最后还应该检查存储数字的数组索引号(应该小于1)

我做了一些努力让你在评论中理解这个程序

int main()
{
  int i=0;
  int input[10]={0}; //here only 10 integers can be entered(hence i should be i<10)
  int in;     //To store input character
  int num=0;  //To store number which is converted from character.
  int new=1;  //To check if new number is started 0=false 1=True.
  int count=0;//This is just to know how many numbers entered,also used to print numbers at end.

  while ((in=getchar()) != '\n' && (in!=EOF) && i<10)//should check for both '\n' and EOF and array index also
  {
      if (in == ' ')
      {
         printf("space\n ");
         if(new==0) //if new Number is not started yet.
         {
            new=1;  //Start of a New number.(a number entered after space)
            i++;  //As new number is started it should be stored in new array index.
         }
         continue; //if space is entered just go to begining
      }
      else
      {
         printf("assigning\n ");
         num=in-48;  //converts a character to number (ex:- converts '3' to 3)
         input[i]=(input[i]*10)+num;  //storing the number..This is important do a paper work to understand this step.
         new=0;  //still in same number(we are still processing same number)
      }
  }
  printf("Out of Loop \n");

  count=i+1;  //This gives correct count of numbers entered

  for(i=0;i<count;i++)  //to print numbers.
     printf("%d ",input[i]);
return 0;
}
intmain()
{
int i=0;

int-input[10]={0};//这里只能输入10个整数(因此我应该是IB,因为您从未在输入流中输入零。如果您向程序输入“1 2 0”,则它应该打印“分配空间分配循环外的空间”请提供一个索引,以便我们了解您的意图。现在您正在存储单个字符,这样您就永远不会存储字符串“78”。此外,无论您存储了什么内容还是保留了初始的不确定值,您都始终在增加索引。此外,
getchar()返回- 1错误,你不检查。如果他不想要C++答案,他不应该用C++来标记他的问题。
bool currentNumberStarted = false;
int currentNumber = 0;
int idx = 0;
while ((in=getchar()) != '0')// you probably want '\0' instead of '0'
{
    if (in == ' ')
    {
                    if (currentNumberStarted)
                    {
                        input[idx]=currentNumber;
                        idx++;
                        currentNumberStarted = false;
                    }
        printf("space\n ");
        continue;
    }
    else
    {
        printf("assigning\n ");
                    currentNumberStarted = true;
                    currentNumber *= 10;
        currentNumber += in;
    }
}
printf("Out of Loop");
int main()
{
  int i=0;
  int input[10]={0}; //here only 10 integers can be entered(hence i should be i<10)
  int in;     //To store input character
  int num=0;  //To store number which is converted from character.
  int new=1;  //To check if new number is started 0=false 1=True.
  int count=0;//This is just to know how many numbers entered,also used to print numbers at end.

  while ((in=getchar()) != '\n' && (in!=EOF) && i<10)//should check for both '\n' and EOF and array index also
  {
      if (in == ' ')
      {
         printf("space\n ");
         if(new==0) //if new Number is not started yet.
         {
            new=1;  //Start of a New number.(a number entered after space)
            i++;  //As new number is started it should be stored in new array index.
         }
         continue; //if space is entered just go to begining
      }
      else
      {
         printf("assigning\n ");
         num=in-48;  //converts a character to number (ex:- converts '3' to 3)
         input[i]=(input[i]*10)+num;  //storing the number..This is important do a paper work to understand this step.
         new=0;  //still in same number(we are still processing same number)
      }
  }
  printf("Out of Loop \n");

  count=i+1;  //This gives correct count of numbers entered

  for(i=0;i<count;i++)  //to print numbers.
     printf("%d ",input[i]);
return 0;
}