C++结构,函数不能打印最小直径
C++ struct, function can't print smallest diameter
这是我的代码,到目前为止,我遇到的问题是打印出行星的最小直径。更新代码。。。。仍然不起作用。。。
#include <iostream>
#include <string>
using namespace std;
struct Planet
{
string name;
int distanceSun;
int diameter;
int mass;
};
int PrintPlanet(Planet planet)
{
cout << "Name: " << planet.name << endl;
cout << "Distance to the sun: " << planet.distanceSun << endl;
cout << "Diameter: " << planet.diameter << endl;
cout << "Mass: " << planet.mass << endl;
return 0;
}
int FindSmallestDiameter(Planet * arr, int n)
{
int resultSmallest = INT_MAX;
for (int j = 1; j < n; j++)
{
if(arr[j].diameter < arr[resultSmallest].diameter)
{
resultSmallest = j;
}
}
return resultSmallest;
}
int main()
{
struct Planet * planet;
int numberPlanet;
cout << "Enter a value for planets: ";
cin >> numberPlanet;
planet = new Planet[numberPlanet];
int enterSelection;
do
{
cout << "Enter selection: n" <<
"1. Print the planet with the smallest diametern" <<
"0. Exit progrman";
cin >> enterSelection;
switch(enterSelection)
{
case 1:
{
int heaviest = FindHeaviestPlanet(planet, numberPlanet);
if (heaviest < 0)
{
cout << "No planet defined.n";
}
else
{
cout << "Heaviest planet: n";
PrintPlanet(planet[heaviest]);
}
}
break;
}
'当在菜单中使用较小直径控制台打印在行星上设置打印命令时:姓名:到太阳的距离:0直径:0质量:0
无论main()
中缺少代码、语法错误以及填充行星的方式如何,您的搜索函数FindSmallestDiameter()
都将永远无法工作:
- 从
resultSmallest = INT_MAX
开始。这是一个非常非常大的数字 - 然后用
j=1
开始循环(通常数组索引从0开始) - 然后尝试访问
arr[resultSmallest].diameter
,这是越界的,并导致未定义的行为。它可能会造成严重破坏或分割错误,但也可能返回随机数,甚至0 - 请注意,即使行星阵列为空,此函数也不会返回负数。因此,你的信息"没有定义的行星"将永远不会显示。更糟糕的是,如果没有定义行星,您将返回
INT_MAX
,这可能会导致main()
中的代码(试图)访问更多越界的元素
可能的修正:
int FindSmallestDiameter(Planet * arr, int n)
{
if (n==0)
return -1; // handle special case first
else {
int resultSmallest = 0; // let's suppose the smallest is the first element
for (int j = 1; j < n; j++) { // then it makes sense to loop starting with the second
if(arr[j].diameter < arr[resultSmallest].diameter) // and challenge the current smalest
resultSmallest = j;
}
return resultSmallest;
}
}
或者使用标准算法std::min_element()
:的较短算法
int FindSmallestDiameter(Planet * arr, int n)
{
return n==0 ? -1 : std::min_element(arr,arr+n,[](const Planet &a,const Planet &b)->bool {return a.diameter<b.diameter;})-arr;
}
问题通过以下函数解决:
Planet FindSmallestDiameter(Planet * arr, int n)
{
Planet smallestDiameter = arr[0];
for (int i = 0; i < n; i++)
{
if (smallestDiameter.diameter < arr[i].diameter)
{
smallestDiameter = arr[i];
}
}
return smallestDiameter;
}
相关文章:
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 为什么 c++ 不能打印向量 B[0]?
- 为什么我不能提供一个字符串参数来打印 ncurses?
- cout 不能一致地打印字符串和变量值,导致输出未对齐
- 任何人都可以向我解释为什么我不能打印出数组值
- 为什么Ubuntu c++中的eclipse不能打印出数据
- 为什么使用预留分配存储后不能迭代和打印矢量的内容?
- 代码无限循环不能只打印一次
- 为什么我可以打印动态分配的对象,但不能打印使用 cout <<在堆栈上创建的对象
- uint8_t不能用 cout 打印
- C++ : 为什么我不能使用 sprintf 打印 const 字符*?
- 为什么这段代码不能正常打印数字?
- 可以使用 cout 打印,但不能使用相同的对象创建新字符串?
- C++结构,函数不能打印最小直径
- 为什么不能按名称打印成员函数的地址?
- C++多项式类,打印成员函数不能很好地使用幂
- 为什么我不能打印出我的字符串数组 c++?
- 为什么我的打印函数会吐垃圾,而我当前的函数却不能正常工作
- 我不能打印我的书的名字,但可以打印其他所有的东西
- 创建了我自己的向量类.可以重载流运算符,但不能按值c++打印