Looking for another way to write c++ code

Started by
12 comments, last by rip-off 14 years, 11 months ago
I am looking for a different way to write c++ codes, which I think might be more elegant. Here are the codes: ifstream infile; infile.open (sMyFile, ifstream::in); char str[100] = ""; double dMyVar = 0.0; //The focus is on the following 2 lines infile >>str ; //for example, str might be "1.27D-09" dMyVar = FuncApplyStandard (str); //Replace "D" with "e" inside of the func //Convert str to double then return it, infile.close(); Function FuncApplyStandard will replace "D" inside of the string with "e", then return a double value to dMyVar. I would like to know if I can combine the following two lines into 1 line: infile >>str ; //for example, str might be "1.27D-09" dMyVar = FuncApplyStandard (str); Actually I hope I can pass str directly to the function, process it, then pass it to dMyVar. Better as infile >>str >> FuncApplyStandard >> dMyVar; Of course the above line won't work in C++. Could anybody show us how to write a workable line similar to the above? Thank you.
Advertisement
Probably more C++-like:

//constructor is capable of opening the line and ifstream is implicitly in reading modeifstream infile(sMyFile); //some error checking here, in case the file couldn't be openedif (!infile) {...}//use string type unless you really want to risk buffer overflowsstd::string str; infile >>str;std::replace(str.begin(), str.end(), 'D', 'e');double dMyVar = convert_to<double>(str);//where convert could be implemented by you (perhaps using std::stringstream)//or you could download boost and use boost::lexical_cast<double>(str);//this can also fail - who knows what the file really contains - so you should//somehow deal with it (boost::lexical_cast throws a bad_lexical_cast exception in such case//no need to explicitly close the file since that is done automatically//when file stream goes out of scope

Quote:Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.
elegant != everythingOnOneLine
elegant == easy to read

Whether it's all on one line or not, the compiler is going to make it roughly equivalent. C++ Code is for humans, bytecode is for computers

-me
Quote:Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


error C2039: 'replace' : is not a member of 'std'

std::replace doesn't work.
Quote:Original post by JohnsonGPS
Quote:Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.



What exactly are you defining "elegant" to be? The offered source code is good idiomatic C++ and does the job needed in a fairly robust manner (assuming you bother to do the error checking and other suggested stuff).

Elegance is not the same thing as having to type less code [wink]


Quote:Original post by JohnsonGPS
Quote:Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


error C2039: 'replace' : is not a member of 'std'

std::replace doesn't work.


Make sure you #include <algorithm> in your code file.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
Quote:Original post by JohnsonGPS
Quote:Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.



What exactly are you defining "elegant" to be? The offered source code is good idiomatic C++ and does the job needed in a fairly robust manner (assuming you bother to do the error checking and other suggested stuff).

Elegance is not the same thing as having to type less code [wink]


No, I don't assume that I can define "elegant" of the codes. I just thought, to pass the variables as inputs to the function (the nexst step operation) by >>, is quite straight-forward.
If you remove the comments it might get more elegant.

If you want to cram more on one line, you might need a helper function for file input (which here is quite inelegant since it doesn't check whether the read operation succeeded). :

#include <fstream>#include <iostream>#include <boost/algorithm/string.hpp>#include <boost/lexical_cast.hpp>template <class T>T read_one(std::istream& is){    T s;    is >> s;    return s;}int main(){    try {        std::ifstream fin("a.txt");        double d = boost::lexical_cast<double>(boost::replace_first_copy(read_one<std::string>(fin), "D", "e"));        //...    }    catch (std::exception& e) {        std::cout << e.what();    }}
I disagree. The >> operator has a very well defined purpose in the context of reading from stream objects in C++. To add another meaning to the operator as you suggest (i.e. to have it pass parameters to a function) makes your code more confusing, not less.

The goal of a programming language is to communicate to the programmer exactly what the code is doing, and, preferably, why it is doing that. The syntax of reading from a stream, passing the read data into a function, then working with the result (i.e. the syntax given above by visitor) is good because it is not ambiguous.


If you want an extreme example, consider this code. Now, note that if I really bend over backwards, I could actually write working C++ code this way - but it would be much more work and have no benefit, because the purpose of the code is not clear:

a + std::cout - 66[foo] >> "pie";

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I think the definition of elegant that we're looking for is for the code to be both clear and succinct, and robust if it's that sort of region of code (sometimes error checking just needs to be moved somewhere out of the way).

And good heavens, ApochPiQ, please tell me that that isn't actual code! [wow]

This topic is closed to new replies.

Advertisement