簡體   English   中英

如何使用 if 語句修復輸出 2 行而不是 1 行的代碼

[英]How to fix code that is outputting 2 lines instead of one using if statements

我的代碼編譯,當我運行它並輸入一個數字時,它將 output 2 行代碼而不是 1 行。 我不確定導致它的 if 語句是否有問題。 但我想知道是什么導致我的程序使用 if 語句 output 2 行而不是 1 行?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    double weight;

    cout << "Enter the weight of your package: ";
    cin >> weight;

    ofstream sout, mout, lout;
    sout.open("shiprates_small.txt");
    mout.open("shiprates_medium.txt");
    lout.open("shiprates_large.txt");

    if (sout.is_open())
    {
        if (weight >= 1 || weight < 10)
        {
            sout << "Shipping rates: " << endl;
            cout << "Go see shiprates_small.txt " << endl;
        }
        sout.close();
    }
    if (mout.is_open())
    {

        if (weight >= 10 || weight < 30)
        {
            mout << "Shipping rates: " << endl;
            cout << "Go see shiprates_medium.txt " << endl;
        }
        mout.close();
    }

    if (lout.is_open())
    {
        if (weight >= 30)
        {
            lout << "Shipping rates: " << endl;
            cout << "Go see shiprates_large.txt " << endl;
        }
        lout.close();
    }

    return 0;
}

這是 output

Enter the weight of your package: 9
Go see shiprates_small.txt
Go see shiprates_medium.txt

替換或|| &&

if (weight >= 1 && weight < 10) {// der code}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM