尝试从输入文件中读取int数时遇到错误

时间:2018-03-07 05:17:31

标签: c++

我是编码C ++的新手。我在尝试从输入文件中读取2个int数时遇到问题,计算它们然后导出到输出。系统在循环的第14,18和21行显示问题。请给我一些建议。谢谢大家!

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream file;
file.open("input.txt");
string word;
word.clear();
int a, b;
int count = 0;
while(file>>word)
{
    count++;
    if (count % 2 == 1){
        a = stoi(word);
    }
    else {
        b = stoi(word);
    }
}
int TOTAL = a + b;
int Difference = a - b;
int Multiply = a * b;
int Division = a / b;
int MUD = a % b;
ofstream out("output.txt");

out << "Input Values: " << a << " " << b << endl;
out << "Sum of two numbers: " << TOTAL << endl;
out << "Difference of two numbers: " << Difference << endl;
out << "Multiply of two numbers: " << Multiply << endl;
out << "Division of two numbers: " << Division << endl;
out << "Modular division of two numbers: " << MUD << endl;
cout << "Calculation written in file" << endl;
out.close();
return 0;
}

1 个答案:

答案 0 :(得分:0)

#include <string>因为您使用的字符串函数是我认为的问题