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

Reading a list of characters and storing in an array

本文关键字:数组 存储 字符 列表 读取      更新时间:2023-10-16

我对以下代码有问题,我不知道为什么没有打印循环外。有了这个代码,我希望程序忽略用户输入的任何空格,在输入空格后,之前输入的数字将存储在数组位置。像这样,我希望6和78存储在2个阵列位置,而不是将它们单独存储为6 7 8。

这是我的代码:

while ((in=getchar()) != '0')
{
    if (in == ' ')
    {
        printf("spacen ");
        continue;
    }
    else
    {
        printf("assigningn ");
        input[i]=in;
    }
    i++;
}
printf("Out of Loop");

当输入5 6 78时,我的输出是:分配空间分配空间分配分配分配

对于这个输出,我怀疑78是否被存储在一个存储位置中。

我真的很感谢你的帮助,谢谢

C++:

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

C:

int array[ 10];
int i = 0, retval;
while( i < 10 && ( retval = scanf( "%d", &array[ i++])) == 1) ; 
if( i == 10) {
    // array full
}
if( retval == 0) {
    // read value not an integer. matching failure
}
if( retval == EOF) {
    // end of file reached or a read error occurred
}

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

你可以像这样存储整个数字(扩展你的代码):

bool currentNumberStarted = false;
int currentNumber = 0;
int idx = 0;
while ((in=getchar()) != '0')// you probably want '' instead of '0'
{
    if (in == ' ')
    {
                    if (currentNumberStarted)
                    {
                        input[idx]=currentNumber;
                        idx++;
                        currentNumberStarted = false;
                    }
        printf("spacen ");
        continue;
    }
    else
    {
        printf("assigningn ");
                    currentNumberStarted = true;
                    currentNumber *= 10;
        currentNumber += in;
    }
}
printf("Out of Loop");

首先,我非常怀疑while循环是否会结束,即使您对''进行了此操作,因为您使用的是char变量来存储输入。不是字符串,只有字符串在末尾使用'',我们如何从键盘输入"\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("spacen ");
         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("assigningn ");
         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;
}

输出:-

E:>example.exe

78 2 65 998 1

分配

分配

空间

分配

空间

空间

分配

环路外

78 2 65 998 1