C++ 循环询问用户输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6935362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:57:27  来源:igfitidea点击:

Loop asking user for input into an array

c++arraysloops

提问by audiedoggie

You all have helped me so much in the past, I am coming back for more help!

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

I am working on another C++ assignment for school. I have to collect 20 numbers between 10 and 100 from the user and compare them all and print out the ones that are not duplicates.

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

The part I am stuck on is trying to get the user input to loop until I have all 20 numbers. What I have makes sense to me, but it apparently is not working. Sorry for the ridiculous comments, I have to comment everything for class.

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

Any light you can shed on my goof up would be amazing! Thank you so much!

你能在我的愚蠢行为上散发出的任何光芒都会很棒!非常感谢!

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
}

回答by ssell

for( int count = 0; count < 20; count++ )
{
    cout << "Please enter a number between 10 and 100: ";
    cin >> userInput;

    FindDuplicates[ count ] = userInput;
}

You can also check for valid input with:

您还可以通过以下方式检查有效输入:

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

If you do that just make sure you have initialized UserInput to something. So:

如果您这样做,请确保您已将 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;
}

回答by michael.shan

your problem is how to enter 20 valid numbers, is it? I think the do while loop will help you.

你的问题是如何输入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);

Hope that will help you.

希望这会帮助你。

回答by zw324

FindDuplicateswill give you the pointer pointed to the array (see this), not the size. You should use

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

for(count=0; count<20; count++;)

for(count=0; count<20; count++;)

instead.

反而。

回答by Richard

There is a syntax error on the line below, check how for statements work. There's also a problem with the <FindDuplicates. Make sure you understand what that variable means.

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

for(count=0; count<FindDuplicates; count++;)

for(count=0; count<FindDuplicates; count++;)