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

C++多分支if-else仅输出首个分支结果问题求助

C++多分支if-else始终执行第一个分支的问题分析与解决

问题原因

1. 变量计算顺序错误

代码中先计算了三个房屋的单价(colonialModel等变量),但此时存储价格和面积的变量(colonialPrice、colonialArea等)还未通过用户输入赋值,属于未初始化状态,会读取内存中的随机垃圾值。同时colonialModel的赋值还出现低级错误:用splitEntryPrice/splitEntryArea计算,而非对应colonial模型的colonialPrice/colonialArea,直接导致单价计算完全错误。

2. 条件判断逻辑表达式完全错误

所有分支的条件写法都不符合需求:

  • 第一个if条件colonialModel < splitEntryModel || singleStoryModel中,|| singleStoryModel的逻辑是判断singleStoryModel是否为非0值,只要该值不为0,整个条件就为真,完全不是“colonial单价比另外两个都低”的逻辑。
  • 后续分支的||用法同样错误,且第三个else if还写错了变量(splitEntryPrice应为splitEntryModel)。
  • 最后一个判断三者相等的条件colonialModel == singleStoryModel && splitEntryModel,&& splitEntryModel是判断该值是否非0,而非判断三者相等。

解决方法

针对上述问题,修正后的完整代码如下:

#include <iostream>
using namespace std;

int main() {
    long colonialPrice, splitEntryPrice, singleStoryPrice;
    double colonialArea, splitEntryArea, singleStoryArea;

    // 先获取用户输入数据
    cout << "Enter base price for colonial model, split-entry, and single-story model >> ";
    cin >> colonialPrice >> splitEntryPrice >> singleStoryPrice;
    cout << endl;

    cout <<"Enter finished area in sqft colonial model, split-entry, and single-story >> ";
    cin >> colonialArea >> splitEntryArea >> singleStoryArea;
    cout << endl;

    // 计算单价:修正赋值错误,添加强制类型转换避免整数除法问题
    double colonialModel = static_cast<double>(colonialPrice) / colonialArea;
    double splitEntryModel = static_cast<double>(splitEntryPrice) / splitEntryArea;
    double singleStoryModel = static_cast<double>(singleStoryPrice) / singleStoryArea;

    // 修正条件判断逻辑,调整全相等分支的顺序(优先判断)
    if (colonialModel < splitEntryModel && colonialModel < singleStoryModel) {
        cout << "The price per square foot of the colonial model is the least" << endl;
    } else if (splitEntryModel < colonialModel && splitEntryModel < singleStoryModel) {
        cout << "The price per square foot of the split-entry model is the least" << endl;
    } else if (singleStoryModel < splitEntryModel && singleStoryModel < colonialModel) {
        cout << "The price per square foot of the single-story model is the least" << endl;
    } else if (colonialModel == splitEntryModel && colonialModel == singleStoryModel) {
        cout << "The price per square foot all three models are the same." << endl;
    } else if (colonialModel == splitEntryModel) {
        cout << "The price per square foot of the colonial and split-entry models tie for the least." << endl;
    } else if (colonialModel == singleStoryModel) {
        cout << "The price per square foot of the colonial and single-story models tie for the least." << endl;
    } else if (singleStoryModel == splitEntryModel) {
        cout << "The price per square foot of the single-story and split-entry models tie for the least." << endl;
    }

    return 0;
}

关键修改点

  • 调整执行顺序:先获取用户输入,再计算单价,避免使用未初始化变量。
  • 修正单价计算:将colonialModel的赋值改为对应模型的价格和面积,添加static_cast<double>确保浮点除法(避免整数除以整数导致的精度丢失)。
  • 修复条件逻辑:将错误的||改为&&,确保条件表达的是“比另外两个单价都低”;修正变量书写错误;将三者相等的判断分支提前,避免被两两相等的分支拦截。

内容的提问来源于stack exchange,提问作者Wendy Xiong

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 16:55:22