You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++实现体重最值查找函数遇问题,求代码修正帮助

问题分析与修复方案

核心错误点

  1. 函数参数顺序完全错位
    remove_persons函数定义的参数顺序与main函数中的调用顺序不匹配,直接导致剔除逻辑使用了错误的数组数据:
  • 函数定义参数:name[], surname[], weight[], height[], age[]
  • main调用传参:name, surname, age, weight, height
    这会让函数里的age[i]实际指向main中的height数组,height[i]指向weight数组,weight[i]指向age数组,彻底打乱了数据关联,是导致最值与姓名无法正确匹配的根本原因。
  1. 返回值类型不匹配
    fatfunc和thinfunc的返回值声明为float,但实际返回的是整数类型的索引值,类型不匹配会引发编译警告,甚至导致运行时索引值被错误转换。

  2. 次要问题

  • 输入身高时未做范围校验(不符合输入提示的要求)
  • 拼写错误:fatest应为fattest,thinest应为thinnest
  • 全局变量N的使用不够规范(不影响核心功能,但可优化)

修正后的代码

#include <iostream>
#include <string>
using namespace std;

int N;

// 修正参数顺序,与调用逻辑一致:name, surname, age, height, weight
void remove_persons(string name[], string surname[], float age[], float height[], float weight[])
{
    for (int i = 0; i < N; i++)
    {
        if (age[i] < 18 || height[i] < 140 || height[i] > 220)
        {
            for (int j = i; j < N - 1; j++)
            {
                name[j] = name[j + 1];
                surname[j] = surname[j + 1];
                age[j] = age[j + 1];
                height[j] = height[j + 1];
                weight[j] = weight[j + 1];
            }
            N--;
            i--;
        }
    }
}

// 修正返回值类型为int,匹配索引的整数类型
int fatfunc(string name[], string surname[], float weight[])
{
    float fat = weight[0];
    int index_fat = 0;

    for (int i = 1; i < N; i++)
    {
        if (weight[i] > fat)
        {
            fat = weight[i];
            index_fat = i;
        }
    }

    return index_fat;
}

// 修正返回值类型为int
int thinfunc(string name[], string surname[], float weight[])
{
    float thin = weight[0];
    int index_thin = 0;

    for (int i = 1; i < N; i++)
    {
        if (weight[i] < thin)
        {
            thin = weight[i];
            index_thin = i;
        }
    }

    return index_thin;
}

int main()
{
    string name[99];
    string surname[99];
    float weight[99];
    float height[99];
    float age[99];
    int fat, thin;

    do
    {
        cout << "Nr. of participants 1-30: ";
        cin >> N;
    } while (N <= 0 || N >= 30);

    for (int i = 0; i < N; i++)
    {
        cout << "Name[" << i << "]: ";
        cin >> name[i];
        cout << "Surname[" << i << "]: ";
        cin >> surname[i];
        do
        {
            cout << "Age[" << i << "] (>18 years): ";
            cin >> age[i];
        } while (age[i] <= 18);
        // 新增身高输入的范围校验
        do
        {
            cout << "Height in cm [" << i << "] (140-220 cm): ";
            cin >> height[i];
        } while (height[i] < 140 || height[i] > 220);

        cout << "Weight [" << i << "]): ";
        cin >> weight[i];
    }

    // 调用参数与函数定义完全匹配
    remove_persons(name, surname, age, height, weight);

    thin = thinfunc(name, surname, weight);
    cout << endl;
    // 修正拼写错误thinest -> thinnest
    cout << surname[thin] << " " << name[thin] << " is the thinnest with a weight of " << weight[thin] << endl;

    fat = fatfunc(name, surname, weight);
    cout << endl;
    // 修正拼写错误fatest -> fattest
    cout << surname[fat] << " " << name[fat] << " is the fattest with a weight of " << weight[fat] << endl;

    return 0;
}

修复说明

  1. 调整remove_persons的参数顺序,使其与main函数的调用参数一一对应,确保剔除逻辑使用正确的年龄、身高数据。
  2. 将fatfunc和thinfunc的返回值类型从float改为int,匹配索引的整数类型,避免类型转换错误。
  3. 为身高输入添加do-while范围校验,符合输入提示的要求。
  4. 修正输出文本中的拼写错误,让表述更准确。

内容的提问来源于stack exchange,提问作者Silviu Stroe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:54:23