[英]How to fix code that is outputting 2 lines instead of one using if statements
My code compiles and when I run it and enter a number, then it will output 2 lines of code instead of one.我的代码编译,当我运行它并输入一个数字时,它将 output 2 行代码而不是 1 行。 I'm not sure if there is something wrong with my if-statement that is causing it.我不确定导致它的 if 语句是否有问题。 But I want to know what it causing my program to output 2 lines instead of 1 with if-statements?但我想知道是什么导致我的程序使用 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;
}
This is the output这是 output
Enter the weight of your package: 9
Go see shiprates_small.txt
Go see shiprates_medium.txt
Replace the or ||
替换或||
with and &&
和&&
if (weight >= 1 && weight < 10) {// der code}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.