C++ 在c+;中,在用户定义的函数内比较用户指定数量的数字+;
第一次在这里张贴海报。 请记住,我仍然是非常新的C++和一般编码。 我试图重写一个程序,询问用户想要比较多少个数字,然后输入每个数字的值。我已经通过for和if语句实现了这一点,但是现在我被要求使用用户定义的函数来实现这一点。到目前为止,我遇到的问题是,我输入的第一个数字是保存的数字,但它确实会提示输入其他数字,并且函数内部的for循环工作正常。如果您能给我一些建议,说明我做得不对,我们将不胜感激。我声明了所有需要的变量,所以我只把原型放进去以节省空间C++ 在c+;中,在用户定义的函数内比较用户指定数量的数字+;,c++,user-defined-functions,C++,User Defined Functions,第一次在这里张贴海报。 请记住,我仍然是非常新的C++和一般编码。 我试图重写一个程序,询问用户想要比较多少个数字,然后输入每个数字的值。我已经通过for和if语句实现了这一点,但是现在我被要求使用用户定义的函数来实现这一点。到目前为止,我遇到的问题是,我输入的第一个数字是保存的数字,但它确实会提示输入其他数字,并且函数内部的for循环工作正常。如果您能给我一些建议,说明我做得不对,我们将不胜感激。我声明了所有需要的变量,所以我只把原型放进去以节省空间 int main() { in
int main()
{
int DisplayGreatest(int,int);
do {
cout << "Please select an option" << endl;// Menu Options
cout << "A: Highest" << endl;
cout << "B:Lowest" << endl;
cout << "C: Quit " << endl;
cin >> Selection; // Menu Selection
cout << endl;
if ( (Selection == 'a') || (Selection == 'A') )
{
cout << "How many numbers do you want to use?" << endl;
cin >> Aselection;
cout << "enter your first choice" <<endl;
cin >> currentAinput;
DisplayGreatest (Aselection,currentAinput);
cout << "The largest number is "<< currentAinput << endl;
}
}
}
intmain()
{
int(int,int);
做{
coutint-DisplayGreatest(int,int)
可能应该在全局命名空间中声明,即:
int DisplayGreatest(int, int);
int main()
{
}
int DisplayGreatest (int Aselection, int currentAinput)
{
}
此外,您可以在循环中接受输入,但更自然的做法是接受整个整数,然后使用相同的想法找到最高/最低值(循环每个整数并保存当前的最高/最低值)
编辑:哦,我明白你现在的问题了。你需要在函数末尾返回keepAinput并将其分配给currentAinput或其他变量,然后打印你分配给的任何变量。或者直接打印结果,即:
cout << "Highest number is " << DisplayGreatest(Aselection, currentAinput) << endl;
cout您的代码中有几个错误:
- 首先,您没有使用函数displayMax返回的值
- 您永远不会将输入的第一个值与其他值进行比较(在第一次比较之前,您会在一行中进行两次比较)
- 在错误的范围内声明函数DisplayMaximest(应在全局范围内,或将整个函数放在main之前)
- 您在main中的do循环没有结束条件。(缺少while语句,可能您在自己的版本中有该语句)
- 您返回了在DisplayBest函数中输入的最后一个值,而不是最大的值
以下是一个功能版本:
#include <iostream>
using namespace std;
int DisplayGreatest(int Aselection)
{
int keepAinput = 0;
int currentAinput;
do {
cin >> currentAinput;
if (currentAinput > keepAinput)
{
keepAinput = currentAinput;
}
} while(--Aselection > 0);
return keepAinput;
}
int main()
{
char Selection;
do {
int Aselection;
int currentAinput;
cout << "Please select an option" << endl;// Menu Options
cout << "A: Highest" << endl;
cout << "B:Lowest" << endl;
cout << "C: Quit " << endl;
cin >> Selection; // Menu Selection
cout << endl;
if ( (Selection == 'a') || (Selection == 'A') )
{
cout << "How many numbers do you want to use?" << endl;
cin >> Aselection;
cout << "enter your first choice" <<endl;
cout << "The largest number is "<< DisplayGreatest (Aselection) << endl;
}
} while(Selection != 'C' && Selection != 'c');
}
#包括
使用名称空间std;
整数显示最大值(整数选择)
{
int-keepAinput=0;
电流输入;
做{
cin>>当前输入;
如果(当前输入>保持输入)
{
keepAinput=currentAinput;
}
}而(--a选择>0);
回输;
}
int main()
{
字符选择;
做{
内部选择;
电流输入;
非常感谢!是的,我在原始代码中有while语句,不想粘贴B和C选项,因为它们不应该影响它。我正在使用的书建议将函数放在main之后,而原型放在main之前。这有什么真正的意义吗?我知道原型现在放错了什么地方,谢谢或者指出这一点。不是真的,更多的是风格和品味的问题。有些人更喜欢先使用main,你也会在文件的开头得到一个很好的所有函数列表。但另一方面,你必须在两个地方进行更改。程序的功能将是相同的。对于以后的项目,你很可能会使用类和/或头文件。
#include <iostream>
using namespace std;
int DisplayGreatest(int Aselection)
{
int keepAinput = 0;
int currentAinput;
do {
cin >> currentAinput;
if (currentAinput > keepAinput)
{
keepAinput = currentAinput;
}
} while(--Aselection > 0);
return keepAinput;
}
int main()
{
char Selection;
do {
int Aselection;
int currentAinput;
cout << "Please select an option" << endl;// Menu Options
cout << "A: Highest" << endl;
cout << "B:Lowest" << endl;
cout << "C: Quit " << endl;
cin >> Selection; // Menu Selection
cout << endl;
if ( (Selection == 'a') || (Selection == 'A') )
{
cout << "How many numbers do you want to use?" << endl;
cin >> Aselection;
cout << "enter your first choice" <<endl;
cout << "The largest number is "<< DisplayGreatest (Aselection) << endl;
}
} while(Selection != 'C' && Selection != 'c');
}