【问题标题】:Loop asking user for input into an array循环询问用户输入数组
【发布时间】:2011-10-19 14:32:34
【问题描述】:

你们过去帮了我很多,我回来寻求更多帮助!

我正在为学校做另一项 C++ 作业。我必须从用户那里收集 10 到 100 之间的 20 个数字,然后将它们全部比较并打印出不重复的数字。

我坚持的部分是试图让用户输入循环,直到我拥有所有 20 个数字。我所拥有的对我来说很有意义,但它显然不起作用。对不起,可笑的cmets,我必须为课堂评论所有内容。

任何你能揭露我的愚蠢行为都会令人惊叹!非常感谢!

int FindDuplicates[20]; // my little array
int UserInput; // the number from the user to compare
int count; // the number of numbers entered

for(count=0; count<FindDuplicates; count++;) // what I am trying to do is have count start at 0. as long as it is less than the max array, add 1 to the count
{
    cout << "Please enter a number between 10 and 100: "; // print out what I would like
    cin >> UserInput; // get the number from the user
}

【问题讨论】:

    标签: c++ arrays loops


    【解决方案1】:

    FindDuplicates 会给你指向数组的指针(见this),而不是大小。你应该使用

    for(count=0; count&lt;20; count++;)

    改为。

    【讨论】:

      【解决方案2】:

      下面一行有语法错误,检查 for 语句是如何工作的。 &lt;FindDuplicates 也有问题。确保您了解该变量的含义。

      for(count=0; count&lt;FindDuplicates; count++;)

      【讨论】:

        【解决方案3】:
        for( int count = 0; count < 20; count++ )
        {
            cout << "Please enter a number between 10 and 100: ";
            cin >> userInput;
        
            FindDuplicates[ count ] = userInput;
        }
        

        您还可以使用以下命令检查有效输入:

        while( UserInput < 10 || UserInput > 100 )
        {
            cout << "Please enter a number between 10 and 100: ";
            cin >> UserInput;
        }
        

        如果您这样做,请确保您已将 UserInput 初始化为某些内容。所以:

        int FindDuplicates[ 20 ];
        int UserInput = 0;
        int count = 0;
        
        //count has already been defined, so no need to do it again
        for( ; count < 20; count++ )
        {
            //Keep asking until valid input is given
            while( UserInput < 10 || UserInput > 100 )
            {
                cout << "Please enter a number between 10 and 100: ";
                cin >> userInput;
            }
        
            //Add userInput into the array
            FindDuplicates[ count ] = UserInput;
        }
        

        【讨论】:

          【解决方案4】:

          您的问题是如何输入 20 个有效数字,是吗? 我认为 do while 循环会对您有所帮助。

          int FindDuplicates[20]; // my little array
          int UserInput; // the number from the user to compare
          int count = 0; // the number of numbers entered
          
          do
          {
              cout << "Please enter a number between 10 and 100: "; // print out what I would like
              cin >> UserInput; // get the number from the user
          
              if (UserInput >= 10 && UserInput <= 100)
              count++;
          } while (count < 20);
          

          希望对你有所帮助。

          【讨论】:

          • 非常感谢!现在我看到它似乎很容易!我只需要添加将 UserInput 添加到由 count 标记的插槽中的数组中的行!
          猜你喜欢
          • 1970-01-01
          • 2020-12-21
          • 1970-01-01
          • 1970-01-01
          • 2021-08-13
          • 2021-02-21
          • 2020-04-03
          • 2013-07-05
          • 1970-01-01
          相关资源
          最近更新 更多