过去你们都帮了我很多,我回来寻求更多的帮助!
我正在为学校做另一个C ++作业。我必须从用户那里收集10到100之间的20个数字并将它们全部进行比较并打印出不重复的数字。
我坚持的部分是试图让用户输入循环,直到我有所有20个数字。我对我有意义,但显然不起作用。对于荒谬的评论感到抱歉,我必须为课堂评论一切。
你在我的蠢事上可以流下的任何光线都会令人惊叹!非常感谢你!
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
}
答案 0 :(得分: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;
}
答案 1 :(得分:3)
你的问题是如何输入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);
希望能帮助你。
答案 2 :(得分:1)
答案 3 :(得分:1)
下面的行有一个语法错误,请检查语句的工作方式。 <FindDuplicates
也存在问题。确保您了解该变量的含义。
for(count=0; count<FindDuplicates; count++;)