如果条目大于 90,则打印输出字符串的布尔函数
我正在尝试编写一个程序,允许用户输入他们选择的数量。我希望数字按升序显示并显示平均值。我完成了这一部分,但如果至少有一个数字高于 90,我还想打印一个字符串,如果没有高于 90 的数字,则打印另一个字符串。我认为我非常接近完成这项工作,但是每当我通过编译器运行它时,它都会说我必须使用的布尔函数不接受 1 个参数。我真的不知道该怎么办。
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
int compare(const void* pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1; // negative if a<b
if (a > b) return 1; // positive if a>b
return 0; // 0 for tie
} // compare
double getAverage(int* score, int n)
{
int sum = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += score[i];
double average = double(sum) / n;
return average;
} // getAverage
//Boolen Function to see if there are A grades present
bool getAGrades(int* score, int n)
{
int i = 0;
for (i = 0; i < n; i++){
if (score[i] >= 90){
return true;
cout << "there is at least one A"<<endl;
}else {
return false;
cout<<" No A Grades "<<endl;
}
}
}
int main()
{
int size;
cout << "How many scores? ";
cin >> size;
cin.ignore(1000, 10);
int* score = new int[size];
int i; // loop counter
for (i = 0; i < size; i++)
{
cout<< "Enter a number: ";
cin >> score[i];
cin.ignore(1000, 10);
} // for
qsort(score, size, sizeof(int), compare);
for (int i = 0; i < size; i++) {
cout << score[i] << ' ';
}
cout <<endl;
cout << "Lowest score = " << score[0] << endl;
cout << "Highest score = " << score[size-1] << endl;
cout << fixed << setprecision(1);
cout << "Average = " << getAverage(score, size) << endl;
//this is where i want it to print if there are a grades or not
getAGrades(score);
return 0;
} // main
I'm trying to write a program that allows users to enter in an amount of their choosing. I want the numbers displayed in ascending order and have the average displayed. I got this part done, but i also want to have a string printed out if there is at least one number higher than 90 and another string if there is not a number higher than 90. I think im pretty close to making this work, but whenever I run it though the compiler, it says that the boolean function that I have to use does not take 1 arguments. I really don't know what to do.
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
int compare(const void* pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1; // negative if a<b
if (a > b) return 1; // positive if a>b
return 0; // 0 for tie
} // compare
double getAverage(int* score, int n)
{
int sum = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += score[i];
double average = double(sum) / n;
return average;
} // getAverage
//Boolen Function to see if there are A grades present
bool getAGrades(int* score, int n)
{
int i = 0;
for (i = 0; i < n; i++){
if (score[i] >= 90){
return true;
cout << "there is at least one A"<<endl;
}else {
return false;
cout<<" No A Grades "<<endl;
}
}
}
int main()
{
int size;
cout << "How many scores? ";
cin >> size;
cin.ignore(1000, 10);
int* score = new int[size];
int i; // loop counter
for (i = 0; i < size; i++)
{
cout<< "Enter a number: ";
cin >> score[i];
cin.ignore(1000, 10);
} // for
qsort(score, size, sizeof(int), compare);
for (int i = 0; i < size; i++) {
cout << score[i] << ' ';
}
cout <<endl;
cout << "Lowest score = " << score[0] << endl;
cout << "Highest score = " << score[size-1] << endl;
cout << fixed << setprecision(1);
cout << "Average = " << getAverage(score, size) << endl;
//this is where i want it to print if there are a grades or not
getAGrades(score);
return 0;
} // main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你的问题:函数
getAGrades
接受一个int* Score
和一个int n
。从main
返回之前要做的最后一件事是调用getAGrades(score);
,但没有为其指定大小。您还必须传递大小:getAGrades(score, size)
。第二:为什么你的比较函数采用
void*
?毫无理由地这是危险的。为什么不使用带有安全比较函数的类型安全std::sort
?第三:
getAGrades
在每个cout
之前返回,因此它永远不会显示任何内容。它应该cout
,然后然后返回。编辑:Keven 还注意到,当发现不是 A 的成绩时,
getAGrades
返回正如他所解释的,这样的函数应该在找到 A 等级时返回,或者如果它检查了所有等级而没有找到任何等级。删除 for 循环之后的“No A Grades”
块。第四:您的 cin.ignore 没有做任何有用的事情,因为您只有格式化输入。另外,使用
'\n'
而不是10
,否则你会让人感到困惑,而且它更便于移植。第五:优先选择标准容器而不是分配您自己的数组。因为您的代码泄漏了
score
数组。First of all, your question: The function
getAGrades
takes anint* score
, and aint n
. The last thing you do before you return frommain
is callgetAGrades(score);
, but you don't give it a size. You have to also pass the size:getAGrades(score, size)
.Second: why does your compare function take
void*
? That's dangerous for no reason. Why aren't you using the type-safestd::sort
with a safe comparison function?Third:
getAGrades
returns right before eachcout
, so it will never ever display anything. It shouldcout
, and then return.Edit: Keven also noticed that
getAGrades
returns when it finds a grade that isn't an A. As he explained, such a function should return when it finds an A grade, or if it checked ALL of the grades without finding any. Remove the"No A Grades"
block to after the for loop.Fourth: Your
cin.ignore
doesn't do anything useful, since you only have formatted input. Also, use'\n'
instead of10
, otherwise you're going to confuse people, and it's more portable.Fifth: Prefer standard containers to allocating your own arrays. Because your code leaks the
score
array.getAGrades 需要两个参数 - 分数和 n。
当您调用 getAGrades 时,您仅传入一个参数。您还应该传递 n 的参数。
您的 getAGrades 函数体也有一些错误。假设您传入一个数组 [1,100]。在 for 循环的第一次迭代中,它看到 Score[0] == 1,因此返回 false。它永远不会到达 Score[1] 来查看它是否高于 90。因此,您应该延迟返回 false,直到迭代整个数组之后。
另一件事 - 您的 cout 语句在您返回值之后发生,因此它们永远不会被执行。您应该将它们放在 return 语句之前。
getAGrades requires two arguments - score and n.
When you call getAGrades, you only pass in one argument. You should also pass in an argument for n.
Your getAGrades function body also has some errors. Suppose you passed in an array [1,100]. In the first iteration of your for loop, it sees that score[0] == 1, so it returns false. It never gets to score[1] to see that it is above 90. For this reason, you should delay returning false until after you have iterated over the entire array.
One more thing - your cout statements occur after you return a value, so they never get executed. You should put them before the return statements.
使用
std::sort
和std::vector
。我知道这并不是这个问题的真正答案。
但是当您想使用 C++ 时,您不应该尝试使用这些 C 函数,例如
qsort
。使用 std::sort 也可以对 C 数组进行排序。
qsort(score, size, sizeof(int), Compare);
变为std::sort(score, Score+size);
(或std::sort (score, Score+size, Compare);
)C++ 方式(使用
std::vector
和std::sort
,固定getAGrades
函数):Use
std::sort
andstd::vector
.I know it's not really the answer to this problem.
But when you want to use C++ you shouldn't try to use these C-functions like
qsort
.With
std::sort
you can sort c arrays too.qsort(score, size, sizeof(int), compare);
becomesstd::sort(score, score+size);
(orstd::sort(score, score+size, compare);
)C++ way (use of
std::vector
andstd::sort
, with fixedgetAGrades
function):