Reputation: 41
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a , b , c , d;
ifstream myfile;
myfile.open ("numbers.txt");
myfile >> a, b, c;
d = a + b + c;
ofstream myfile;
myfile.open ("result.txt");
myfile << d;
myfile.close();
return 0
}
The number.txt
file contains 3 numbers 10
, 8
, 9
. I am trying to get the program to read them and sum them up in the results.txt.
The errors I get are:
conflicting declaration 'std :: ifstream myfile'
no match for 'operator << in myfile << d'
'myfile' has a previous declaration as 'std :: ifstream myfile'
Upvotes: 1
Views: 2724
Reputation: 42828
(This only addresses one of the two errors in your code.)
myfile >> a, b, c;
This line doesn't read input to all three variables a
, b
, and c
. It only reads input to a
, then evaluates b
and discards the value, then evaluates c
and discards the value.
What you want is:
myfile >> a >> b >> c;
This will read a value to all three variables from myfile
.
Upvotes: 3
Reputation: 2711
You cannot declare two different variables with the same name. You are first declaring myfile
to be of type std::ifstream
and then later you declare myfile
to be of type std::ofstream
. Name your output stream variable differently.
Upvotes: 2