C++ 程序似乎正在执行两个if语句
因此,我需要程序执行满足我提出的条件的任一if语句。我使用的输入是70、15和30000。它应该进入第二个if语句。下面是代码C++ 程序似乎正在执行两个if语句,c++,C++,因此,我需要程序执行满足我提出的条件的任一if语句。我使用的输入是70、15和30000。它应该进入第二个if语句。下面是代码 #include <iostream> #include <fstream> using namespace std; int information1(int hourR); int information2(int conTime); int information3(int income1); int hourR,conTime,incom
#include <iostream>
#include <fstream>
using namespace std;
int information1(int hourR);
int information2(int conTime);
int information3(int income1);
int hourR,conTime,income1;
ofstream outfile;
int information1(int hourR)
{
cout<<"Enter hourly rate.";
cin>>hourR;
return hourR;
}
int information2(int conTime)
{
cout<<"Enter total consulting time in minutes.";
cin>>conTime;
return conTime;
}
int information3(int income1)
{
cout<<"Enter income.";
cin>>income1;
return income1;
}
int main()
{
int hours,consultTime,income,fortyPercent,seventyPercent;
outfile.open("Billing amount.txt");
hours=information1(hourR);
consultTime=information2(conTime);
income=information3(income1);
if(income<=25000) {
if(consultTime<=30) {
outfile<<"No service charge.";
}
else {
fortyPercent=hours*.4*45/60;
outfile<<fortyPercent;
}
}
else
{
if(consultTime<=20){
outfile<<"No service charge.";
}
else
{
seventyPercent=hours*.7*45/60;
outfile<<seventyPercent;
}
}
outfile.close();
return 0;
}
#包括
#包括
使用名称空间std;
内部信息1(内部时间);
内部信息2(内部连续时间);
国际信息3(国际收入1);
内部时间、持续时间、收入1;
出流孔的直径;
内部信息1(内部小时)
{
库图尔;
返回时间;
}
内部信息2(内部连续时间)
{
coutconTime;
返回conTime;
}
内部信息3(内部收入1)
{
coutincome1;
退货收入1;
}
int main()
{
整小时,咨询时间,收入,四月份,七月份;
outfile.open(“Billing amount.txt”);
小时=信息1(小时);
consultTime=information2(conTime);
收入=信息3(收入1);
如果(收入您当前的结构如下所示:
if(someCondition) {
//doStuff
} else {
//do something else
}
if(someOtherCondition) {
//do stuff
} else {
//do something else
}
if(income<=25000) {
if(consultTime<=30) {
//do stuff
} else { //this else is paired with if(consultTime<30) and within the if(income<=30)
//do stuff when income<=25000 and consultTime>30
}
} else /*income>250000*/ { //this else is paired with if(income<=25000)
if(consultTime<=20) {
//do stuff
} else { //this else is paired with if(consultTime<=20) and within the else paired with if(income<=30)
//do stuff when income>25000 and consultTime>20
}
}
代码将对第一对if
执行if
或else
,对第二对执行if
或else
我能想到的最简单的解决方案如下:
if(someCondition) {
//doStuff
} else {
//do something else
}
if(someOtherCondition) {
//do stuff
} else {
//do something else
}
if(income<=25000) {
if(consultTime<=30) {
//do stuff
} else { //this else is paired with if(consultTime<30) and within the if(income<=30)
//do stuff when income<=25000 and consultTime>30
}
} else /*income>250000*/ { //this else is paired with if(income<=25000)
if(consultTime<=20) {
//do stuff
} else { //this else is paired with if(consultTime<=20) and within the else paired with if(income<=30)
//do stuff when income>25000 and consultTime>20
}
}
if(Income为什么您认为它不会同时执行两个if语句?改进代码格式!!!!目前它确实同时执行两个语句,但我需要它来执行其中一个。就像我使用的输入一样,我的输出文件中没有服务费用。@user2812652所以请澄清输入和条件…并开始调试您的程序!我做了您的更改,但我得到了21不收取服务费。您知道21可能来自哪里吗?请将您的更改编辑为原始问题。不要在评论中发布。@user2812652 21是来自:hours*.4*45/60的预期答案,当hours=70时。您似乎没有更改原始代码以使其使用此答案中的解决方案。@user2812652ted将您实现的代码添加到原始问题中,这样我就可以看到您实际上是如何实现它的,我只能猜测为什么您的程序正在做任何事情。如果您需要进一步的帮助(我只能假设您需要,因为您还没有接受我的答案),您需要编辑原始问题,以包含您实现我发布的代码的尝试。