You cannot use const
here. But that's not your only problem.
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
Looks good.
float z;
int j=0;
stringstream s;
There should be some int main() {
here, because that is the function called by the runtime when a C++ executable starts. This isn't Bash or Perl where execution simply picks up at the first statement.
And you either need to write using namespace std;
or prefix identifiers like stringstream
as std::stringstream
.
const ifstream fich1;
This won't work. ifstream
must not be declared const
. (This is actually the explanation for the errors you encountered, but you made many more.)
An input file stream is an object with complex inner state, like where in the file you're currently at, error flags etc.; this inner state is changing so const ifstream
simply won't work.
fich1.open("test.txt",ios::in);
This can't work either due to const
, just like all the other operations on fich1
you're doing further down.
if(!fich1)
{ cerr<<"could not open the file";
EXIT_FAILURE;
};
EXIT_FAILURE
is a symbolic constant for a return value. As-is, you could just as well have written 0;
. If you expected this to end your program, you're wrong.
;
The semicolon ends the if
.
else
Since the if
has ended, this is a syntax error.
const string ligne1;
If you declare a string const
, you cannot assign to it. Besides, even if the else
were correct, that semicolon would have ended the else
, because you didn't add braces (as you should always do, even with one-line blocks, because it's so easy to make mistakes otherwise).
The way the code continues, I very much doubt this was your intention. (Some discipline with the indenting makes this kind of mistakes really easy to spot.)
while((!fich1.eof())
How to read until EOF from cin.
{ if(j!=i)
{j++;
Do some proper indenting, will you? Besides, no i
has been declared at this point.
getline(fich1,l1);
I assume l1
is the string you declared earlier as ligne1
. (Since that happened inside the else
, ligne1
is no longer in scope, but I'll let that one pass since you obviously intended the while
to be inside the else
block.)
Anyway, this can't work because both your ifstream
and your string
are constant, i.e. cannot be changed. This does not make sense, and thus getline()
was not defined for const
parameters.
else if(j == i)
Since you didn't close the brace of the if
statement above, this is a syntax error as well. Again, proper indenting discipline would have made this immediately obvious.
{ fich1>>s;
I very much doubt there exists an operator>>()
for const ifstream
, so I am not surprised you get an error here.
z=(float)s;
You're trying to cast (C style, too...) a stringstream
object to a float
number? What do you expect the result might be? It will definitely not be the 3.14
or whatever you've written into test.txt
...
}
}
Lost track of your braces. This code is FUBAR, I didn't even check if it makes sense semantically once all the errors are fixed, and I suspect you're pulling some elaborate troll prank here. If that isn't the case, I suggest you take another good look at whatever textbook you're using to learn C++, because you got a good many things very wrong.
Purely style-related advice: fich1
, ligne1
... don't use localized (non-English) identifiers. It just adds another problem when communicating about your code. (And this is coming from a non-native English speaker.)