string search and copy in a file !

This is a discussion on string search and copy in a file ! within the C++ Programming forums, part of the General Programming Boards category; Hello,
I have a text file which contains this...
Mass Flow Rate = 1.06984
Total Press Ratio = 1.05424
Adiabatic ...

>why would i need "int main()" and not "void main()"
Because int is right and void is wrong. Eventually you'll be directed to the C++ standard, where these things are documented, but for now just take our word for it.

>for me it's just a matter of not writing return 0; at the end !!
Remember that I said there are no excuses. C++ automatically returns 0 even if you omit the return statement. This is a perfectly legal C++ program that returns 0:

Code:

int main()
{
}

>and if i'm reading the file the wrong way
>what is the right way ?!
I showed you. You use the return value of getline. It returns the stream, which when used in a conditional statement tells you whether the stream is in a failure state or not. The failure state includes end-of-file, so all you need to do is:

Code:

while ( getline ( my_stream, my_string ) ) {
// Work with a line
}

You can do the same thing with basically every input method. Another common one is:

Code:

while ( my_stream>> my_var ) {
// Use the variable
}

>I don't know why so many "higher up" programmers HATE that.
Because it's wrong. The C++ standard explicitly states that main returns an int. If an implementation doesn't provide for void main then the entire program is has undefined behavior. And undefined behavior is the worst possible thing you can have in C++.

>They take it as an offence and as lazyness.
I'm sorry if it seems that way. We're just on a quest to avoid undefined behavior and promote correct C++.

It's not correct that definition. First there is no guarantee that the value of string::npos is -1. But also how can an unsigned integral possibly be -1? It can't.

npos is defined by the standard as simply being greater than any possible index. That is, it will hold a value that is greater than the last index of the largest possible string. npos is what is returned by several of the string find functions when there is no match.

If Microsoft implementation decides to treat npos as being -1, it's their business. Although it will not be according to what the standard defines. But they can't say it's an unsigned integral. It would have to be a signed integral.

As for the second part of your previous question (sorry missed it before), Prelude is testing against npos to see if the string was found. The find function either returns the index at which the string was found, or that special value mapped to string::npos that means it was not found.

What happens is that integrals wrap around when they reach their limits. For instance, if an int variable had as minimum value 0 and maximum possible value 100, when you give it a value out of bounds, it will wrap. So, -1 becomes 100, -2 becomes 99. Conversely, 101 becomes 0, 102 becomes 1,...

So in fact, I'm wrong and the documentation found in MSDN is right (albeit perhaps confusing to the distracted like me). By initializing to -1, they are in fact giving it the maximum possible value.