C++ 开关结构(运行最后一种情况两次)
C++ Switch Structure (Running Last Case Twice)
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Named constants residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;
//Named constants business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
int main()
{
//Variable declarations
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
//declaring file streams
ifstream inFile;
ofstream outFile;
//open the file streams
inFile.open("input.txt");
outFile.open("output.txt");
//check wheather file exists or not
if (inFile.fail())
{
cout << "The input file cannot be found! ";
return 1;
}
//output console message
cout << "n==========================================nn";
cout << " PROGRAM TO COMPUTE THE CABLE BILLn";
cout << "n==========================================nn";
//output outfile message
outFile << "n==========================================n";
outFile << " Customer Details";
outFile << "n==========================================n";
//loop until end of file
while (!inFile.eof())
{
cout << "The account number is: ";
inFile >> accountNumber;
cout << accountNumber<<"n";
cout << "The customer type: "
<< "R or r (Residential), "
<< "B or b (Business): ";
inFile >> customerType;
cout << customerType << endl;
//switch to residential or business customer type
switch (customerType)
{
case 'r':
case 'R':
cout << "Enter the number"
<< " of premium channels: ";
cin >> numOfPremChannels;
cout << "nn";
amountDue = RES_BILL_PROC_FEES
+ RES_BASIC_SERV_COST
+ numOfPremChannels *
RES_COST_PREM_CHANNEL;
//write to output file
outFile << setw(25) << left << "nCustomer Account Number: "
<< accountNumber << endl;
outFile << setw(25) << left << "Amount Due: "
<< fixed << setprecision(2) << "$" << amountDue << endl;
break;
case 'b':
case 'B':
cout << "Enter the number of basic "
<< "service connections: ";
cin >> numOfBasicServConn;
cout << "Enter the number"
<< " of premium channels: ";
cin >> numOfPremChannels;
cout << "nn";
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
//write to output file
outFile << setw(25) << left << "nCustomer Account Number: "
<< accountNumber << endl;
outFile << setw(25) << left << "Amount Due: "
<< fixed << setprecision(2) << "$" << amountDue << endl;
break;
default:
//write to output file
outFile << "nThe account number " << accountNumber << " has an invalid customer type." << endl;
break;
} //end switch
} //end eof while loop
//close input file
inFile.close();
//close output file
outFile.close();
return 0;
}
谁能向我解释为什么最后一个案例总是运行/打印两次?这是我们必须为课堂编写的基本程序,我似乎无法理解为什么它不起作用。我的 .eof() while 循环有问题吗?我尝试使用do-while循环,但我得到了相同的结果。我还尝试通过不包含默认情况来更改输入文件,但它仍然运行最后一组两次。
**对于基本连接和通道的数量,只需输入任何有效整数这是我的输入文件:
157744232 b
253453534 r
335345435 R
445345345 B
545345345 t
这不是因为switch
结构,而是因为eof
调用:该函数仅在尝试读取下一项失败后才开始返回true
。
将循环更改为无限循环(for (;;)
或while(true)
,没关系),并在读取数据后执行eof
检查。如果得到true
,请使用break
结束循环。否则,请继续执行您的switch
语句。
循环的结构应该是这样的:
for (;;) {
cout << "The account number is: ";
if (!(inFile >> accountNumber))
break;
cout << accountNumber<<"n";
cout << "The customer type: "
<< "R or r (Residential), "
<< "B or b (Business): ";
if (!(inFile >> customerType))
break;
cout << customerType << endl;
switch (customerType) {
... // The rest of your code
}
}
当你在文件上写一个循环时,你应该使用这样的东西:
while( inFile >> somevar ) {
...
}
或者这个:
while( true ) {
inFile >> somevar1;
if( !inFile ) break;
...
}
这是常见的错误。
相关文章:
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种惯用的方法可以在不存储变换或不必要地重新计算的情况下找到数组变换的最小/最大值?
- 在C++中,有没有一种方法可以让我在不传递参数的情况下拥有一个函数
- 有没有一种方法可以在不设置精度的情况下设置小数位数
- 有没有一种方法可以在不复制数据的情况下从string_view创建字符串流
- 是否有一种设计模式或面向对象的基本原则来处理这种共享资源的情况?
- free():仅在一种情况下指针无效
- C++ 在一种特定情况下替代无效*
- 有没有一种很好的方法来实现具有默认失败情况的条件类型?
- 为什么在一种情况下,我会收到带有字符串文字的已弃用转换警告,而在另一种情况下却没有?
- 我如何仅在一种情况下或某些情况下专业攻击效果
- 空格很重要的另一种情况(也许?
- 为什么模板参数只能在一种情况下推导
- C++为什么在一种情况下保存到文件有效,而另一种情况则无效
- 在一种情况下调用构造函数的顺序C++
- C++ 开关结构(运行最后一种情况两次)
- 如何在一种情况下实现具有显著不同实现的类
- 是否有一种情况,可变函数应该优先于可变模板
- 为什么在头文件中使用私有内部类作为参数会出错,而在另一种情况下在调用函数中会出错?
- 为什么在一种情况下模板参数需要volatile,而在另一种情况下不需要