我的代码出错了,谁能解释一下怎么回事?
Posted
技术标签:
【中文标题】我的代码出错了,谁能解释一下怎么回事?【英文标题】:Error in my code, can anyone explain whats wrong? 【发布时间】:2014-03-21 00:44:20 【问题描述】:我是 C++ 新手,我正在开发一个程序,但在第 39 行它显示错误“else”,没有先前的“if”,但我确实有一个先前的 if。有人对我做错了什么有任何意见吗?这是我的程序代码。谢谢。
1 #include <fstream>
2 #include <iostream>
3 #include <cstdlib>
4 using namespace std;
5 int main()
6
7 //Declarations
8 ifstream masterFile;
9 ifstream transactionFile;
10 ofstream newMasterFile;
11 double mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
12 string mClientfName, mClientlName;
13 cout << "Master File Updating Starting" ;
14 masterFile.open("Master.rtf");
15 transactionFile.open("Transaction.rtf");
16 newMasterFile.open("newMaster.rtf");
17 masterFile >> mClientNumber;
18 masterFile >> mClientfName;
19 masterFile >> mClientlName;
20 masterFile >> mtotalClientCost;
21 transactionFile >> tClientNumber;
22 transactionFile >>titemClientCost;
23 while ( transactionFile.eof() )
24
25 while (( masterFile.eof()) && (mClientNumber < tClientNumber))
26
27 newMasterFile << mClientNumber << endl;
28 newMasterFile << mClientfName << endl;
29 newMasterFile << mClientlName << endl;
30 newMasterFile << mtotalClientCost << endl;
31 masterFile >> mClientNumber;
32 masterFile >> mClientfName;
33 masterFile >> mClientlName;
34 masterFile >> mtotalClientCost;
35
36 if (masterFile.eof());
37
38 cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
39 else if (mClientNumber == tClientNumber);
40 mtotalClientCost = mtotalClientCost + titemClientCost;
41 newMasterFile << mClientNumber << endl;
42 newMasterFile << mClientfName << endl;
43 newMasterFile << mClientlName << endl;
44 newMasterFile << mtotalClientCost << endl;
45 masterFile >> mClientNumber;
46 masterFile >> mClientfName;
47 masterFile >> mClientlName;
48 masterFile >> mtotalClientCost;
49 else if (mClientNumber > tClientNumber);
50 cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
51
52 transactionFile >> tClientNumber;
53 transactionFile >> titemClientCost;
54
55 while (masterFile.eof())
56
57 newMasterFile << mClientNumber << endl;
58 newMasterFile << mClientfName << endl;
59 newMasterFile << mClientlName << endl;
60 newMasterFile << mtotalClientCost << endl;
61 masterFile >> mClientNumber;
62 masterFile >> mClientfName;
63 masterFile >> mClientlName;
64 masterFile >> mtotalClientCost;
65
66 cout << "Master File Updating Complete" ;
67
68 masterFile.close();
69 transactionFile.close();
70 newMasterFile.close();
71
72 system("pause");
73 return 0;
74
【问题讨论】:
这不是你的问题,但你可能会觉得这很有趣***.com/questions/5605125/… 【参考方案1】:if (masterFile.eof());
//^^this ; here effectively terminates your if block and
//does not have a closing before else
类似的问题可以在这里找到:
if (mClientNumber == tClientNumber);
//^^another line below same problem
【讨论】:
分号是多余的。它终止 if 语句。 @sj0h 是的,确切地说。我会更新措辞,谢谢!【参考方案2】:在所有 if 语句中,您添加了一个不必要的 ;
。
if (masterFile.eof());
// ^ this semicolon is not supposed to be there
;
将终止 if 语句。
改为这样做:
if (masterFile.eof())
此外,您没有关闭 if 语句中的第一个括号。所以最终的产品应该是这样的:
if (masterFile.eof())
//if body
else if (*next condition*)
//else if body
等等..干杯
【讨论】:
【参考方案3】:首先你在 if 和 else if 语句之后放置了一个分号
if (masterFile.eof());
else if (mClientNumber == tClientNumber);
替换它们。
如果不考虑这个错字 yjen,else if 没有前面的 if 语句。相反,它被放入封闭 if 语句的复合语句中
if (masterFile.eof());
cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
else if (mClientNumber == tClientNumber);
也许你的意思是以下
if (masterFile.eof());
cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
else if (mClientNumber == tClientNumber);
或者
if (masterFile.eof());
cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
if (mClientNumber == tClientNumber);
【讨论】:
【参考方案4】:您在少数地方忘记了 和
。将第 36-51 行更改为
36 if (masterFile.eof())
37
38 cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
39 else if (mClientNumber == tClientNumber)
40 mtotalClientCost = mtotalClientCost + titemClientCost;
41 newMasterFile << mClientNumber << endl;
42 newMasterFile << mClientfName << endl;
43 newMasterFile << mClientlName << endl;
44 newMasterFile << mtotalClientCost << endl;
45 masterFile >> mClientNumber;
46 masterFile >> mClientfName;
47 masterFile >> mClientlName;
48 masterFile >> mtotalClientCost;
49 else if (mClientNumber > tClientNumber)
50 cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
51
【讨论】:
您忘记提及他/她将;
放置在错误的位置并终止了 if 语句。以上是关于我的代码出错了,谁能解释一下怎么回事?的主要内容,如果未能解决你的问题,请参考以下文章