我在这里束手无策。我正在用C++编写一个程序,它将读取一个文本文件,其中包含以下内容作为一个简短的示例:
+ 23 34
- 9 8
+ 100 1
* 8 7
^ 2 5
/ 45 8读取器将第一个操作数存储在char类型中,并基于它检索到的char,调用一个函数来对这两个数字执行操作,以下是完成此操作的示例函数。
void doDivision(ifstream &inFile) {
char ch;
int num1, num2;
inFile >> ch >> num1 >> num2;
cout << "Division " << num1 << " " << num2 << " " << "Quotient " << " " << num1/num2 << " Remain " << num1%num2 << endl;
}一件事是,我不确定为什么这个参数是&,这个函数原型不是我写的,但是它来自一本书,也许这就是为什么我不能让它工作。
下面是我的主要函数:
int main()
{
ifstream inFile;
char ch;
int num1, num2;
inFile.open("math.txt");
if (inFile.fail())
{
cout << ch;
cout << "The math.txt input file failed to open";
return -1;
}
while(inFile)
{
switch (ch) {
case '+':
doAddition(inFile);
break;
case '-':
doSubtraction(inFile);
break;
case '*':
doMultiplication(inFile);
cout << "debug " << ch;
break;
case '/':
doDivision(inFile);
break;
case '!':
doFactorial(inFile);
break;
default:
cout << "Invalid Operation" << endl;
}
inFile >> ch;
}
inFile.close();
return 0;
}所有这些加在一起会产生以下意想不到的结果
Invalid Operation
Addition 3 34 sum 37 (wrong data in text file is 23 and 34)
subtraction 0 8 difference 8 (data in textfile is 9 and 8 respectively)我该如何实现这一点,因为我以前从来没有处理过文件,所以我不知所措。
发布于 2013-03-09 06:57:07
一个简单的错误是,在while()循环中,直到第一次通过循环之后才调用inFile >> ch。试着修复它,看看它是否有帮助。
另外,什么是Aniket said in their answer是你需要注意的另一个问题。
因此,简而言之,您的循环应该大致如下:
inFile >> ch;
while(inFile) {
switch(ch) {
case '+':
...
}
inFile >> ch;
}您的函数应该类似于:
void doDivision(ifstream &inFile) {
int num1, num2;
inFile >> num1 >> num2;
...
}发布于 2013-03-09 06:57:23
由于您已经在main()中执行inFile >> ch,因此在doAddition()和其他方法中再次读取它将是错误的(逻辑上)。
既然您已经在读入ch字符的地方发布了doDivision()方法,我假设您在doAddition()中也是这样做的。
还有你的输出:
加法3 34和37 (文本文件中错误的数据是23和34)
告诉我们-您已经读取了1个字符(找到了+),然后在doAddition()方法中读取了另一个字符,-将2读取到doAddition()的ch中,然后读取3和34。
这才是错误的真正所在。
解决方案:将您的doAddition()和所有其他函数更改为如下所示。
void doAddition(ifstream &inFile) {
char ch;
int num1, num2;
inFile >> num1 >> num2;
cout << "Addition of " << num1 << " and " << num2 << " = "<< (num1+num2) << '\n';
}另外,在main()函数中:
while循环应该如下所示:
while(inFile)
{
inFile >> ch;
switch (ch) {
case '+':
doAddition(inFile);
break;
case '-':
doSubtraction(inFile);
break;
case '*':
doMultiplication(inFile);
cout << "debug " << ch;
break;
case '/':
doDivision(inFile);
break;
case '!':
doFactorial(inFile);
break;
default:
cout << "Invalid Operation" << endl;
}
}发布于 2013-03-09 06:57:47
您不需要在第一次迭代时读入ch。实际上,当您检查打开的文件没有问题时,尽管ch未初始化,但仍执行了cout << ch;。您可以简单地将inFile >> ch;移到while循环的顶部。
下一个问题是,在每个doSomething函数中,您再次执行inFile >> ch,这将尝试再次读取操作字符。但是,doSomething函数不需要知道ch,因为函数本身是根据ch的值选择的。
下面是我写这段代码的方法:
ifstream inFile("math.txt"); // You can specify the file name here
char op;
int left_operand, right_operand;
// Use extraction has while condition
while (inFile >> op >> left_operand >> right_operand) {
switch (op) {
// Pass operands to the relevant function
case '+': doAddition(left_operand, right_operand); break;
// ...
}
}
// You do not need to close inFile, it will be closed when it goes out of scope
return 0;https://stackoverflow.com/questions/15304690
复制相似问题