Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

n00b question

Options
  • 08-05-2003 9:37pm
    #1
    Registered Users Posts: 2,543 ✭✭✭


    k i have a c++ program written that takes txt from a file and does a few operations based on that txt, the problem is i cobbled the code together from various sources and its hideously inefficient. so could some1 plz reply with the code for a simple, efficient program to read input from a file eg "text.txt" , which looks like this

    9
    2

    and the program just prints out 9+2=11 to the dos window.
    hope u can help


Comments

  • Registered Users Posts: 491 ✭✭Silent Bob


    Hmmm... This looks awfully like you need someone else to do an assignment for you... :)

    How about you post the code you have first, then we'll see about handing out code on a plate...


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    1) declare a file handler
    2) use the handler to open the file
    3) read in the first number to a variable
    4) read in the second number to a variable
    5) close the file*
    6) add the two variables and save the result in a third variable
    7) output this third variable to the console

    * This can happen anytime after step 4


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by DeadBankClerk
    * This can happen anytime after step 4

    Or not at all if anything goes wrong!

    Generally if you have code which must "close" or otherwise "finish with" some resource I'd say to wrap that resources in a class with the closing action happening in it's destructor (the RAII idiom).

    However the STL file stream objects already do that anyway.

    Hence most of the code is simply this:
    int main(int argc, char* argv[]){
    	int x;
    	int y;
    	ifstream ifs("text.txt");
    	ifs >> x >> y;
    	cout << x + y;
    	return 0;
    }
    

    All that's left is the #includes and a using directive.

    (Yeah I know that sionnach is probably just asking us to do some homework, and I should really just ban them instead, but it brought up RAII, and you can't talk too much about RAII if you ask me).


  • Registered Users Posts: 491 ✭✭flav0rflav


    learn to crawl first, then walk, then run.


  • Closed Accounts Posts: 1,672 ✭✭✭Wolf


    Originally posted by flav0rflav
    learn to crawl first, then walk, then run.

    Well thank god for your helpful comment anyway.

    Also lads remember that if it is for an assignment the if he use things like ifstream hell be caught out as a cheat.

    What I would advise is that if the progy work hand it in. At least its your own work and you understand it which is the main thing.


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Also lads remember that if it is for an assignment the if he use things like ifstream hell be caught out as a cheat.
    Why?
    Given that I know how to use ifstream, and I've never done a Computer Science or Programming course in my life, it is possible that he or she might have cracked a book (or is the idea of a CS student reading outlandish these days).

    Granted an CS assignment might say not to use the library, since there is educational value in having to do the STLs job from scratch, but otherwise use should use it.

    In the real world you always would unless you've a very good reason.

    If ifstream is banned then I'd still recommend using the RAII idiom, even if only through a simple wrapper around the handle.

    I'm sure flav0rflav has a point. I'm not sure what it is.


  • Closed Accounts Posts: 1,672 ✭✭✭Wolf


    Yeah his point was

    Learn to troll first, then spam, then flame.

    What im saying is if a first year student was to use that any of my lectures in first year would have know they were cheating. Even if he said he used a book he would have to understand exactly what it was doing. If he was asked and didnt sound confident then hed be caught out.

    And yes the idea of a CS student (not counter strike student ;) )
    read a book these days OR any days for that matter is rediculous.


  • Registered Users Posts: 2,543 ✭✭✭sionnach


    lol i realise now how much it looks like im trying to getg some1 to do my homework for me, but the reality is that im doin my leaving cert. in a few weeks time, im not doing a Comp Sci. course at all :D Im just trying to make the change over from Java to C++ and its really hard to find a decent help file online on handling input and the like. If ne 1 knows a good place where i can learn all bout reading input and storing it and basically working with txt files plz reply with a link, or if u know a REALLY good C++ book, plz gimme the name so i can get it off amazon r something


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by sionnach
    or if u know a REALLY good C++ book

    The C++ Programming Language by Bjarne Stroustrup is great. It's not a complete newbies book, but if you already know Java like you say, and don't need everything spoon-fed to you (which your hacking while still being at Leaving Cert level implies) then it should be all you need.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Also MSDN's guide to iostream programming has a bit of a Microsoft bias (obviously), but not much and if you are already familiar with templates then it should tell you all you need to know about text input and output.


  • Advertisement
  • Registered Users Posts: 2,543 ✭✭✭sionnach


    ccol, thx for the help


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    And if you hadn't heard the RAII acronym before see "RAII - Resource Acquisition Is Initialization" amongst others.

    Al.


  • Registered Users Posts: 491 ✭✭Silent Bob


    Originally posted by [-UK-]Wolf
    And yes the idea of a CS student (not counter strike student ;) )
    read a book these days OR any days for that matter is rediculous.

    So *that's* where I'm going wrong! Too many books ;)


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    [-UK-]Wolf's post yet again brings up something that always bugs me about the way that programming is taught. Generally I'd say that ifstream should be learnt before the C-style input (and C++ should be learnt before C). ifstream is easier both as something where you say "here's how we bung values into these variables, we'll explain how it works later", and also easier and far more flexible when it comes to real code.

    I can see the point (possibly) in a Computer Science course teaching things the other way around, as computer science isn't programming and C-style input is arguably closer to the raw bytes than C++ style (though it's worth noting that ifstream is mainly composed of inline code that is highly optimisable - release code using ifstream should be pretty efficient).

    Too often though programming is taught in an order that is based on chronology of innovation rather than in a logical order. It's not like you learn how to light a fire with flint and steel before progressing onto matches and finally (if you've stuck with the course long enough) are taught how to turn on your central heating.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by Talliesin
    [-UK-]Wolf's post yet again brings up something that always bugs me about the way that programming is taught. Generally I'd say that ifstream should be learnt before the C-style input (and C++ should be learnt before C). ifstream is easier both as something where you say "here's how we bung values into these variables, we'll explain how it works later", and also easier and far more flexible when it comes to real code.

    I can see the point (possibly) in a Computer Science course teaching things the other way around, as computer science isn't programming and C-style input is arguably closer to the raw bytes than C++ style (though it's worth noting that ifstream is mainly composed of inline code that is highly optimisable - release code using ifstream should be pretty efficient).

    Too often though programming is taught in an order that is based on chronology of innovation rather than in a logical order. It's not like you learn how to light a fire with flint and steel before progressing onto matches and finally (if you've stuck with the course long enough) are taught how to turn on your central heating.

    I first learned Java in college, and it's my langauge of choice at this point. I'd tend to agree. If you're going to learn C++, it should be done properly, using the classes provided in the ANSI libraries. Later on, then students can learn how to make their own classes similar to the standard classes. A bit like using System.in to read input in Java, and then parsing it, when you've a perfectly good BufferedReader class at your disposal.
    I tend to take a dim view of mixing C and C++ tbh. It makes the code seem dirty to me.

    *shrug*

    Maybe it's just me.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by seamus
    I tend to take a dim view of mixing C and C++ tbh. It makes the code seem dirty to me.

    Well it's often important that C++ does allow you to get down to as low a level as C does and sometimes important that you can get lower still with asm or __asm or similar.

    However C++ allows you to do so in a class and then think about it in higher level terms most of the time. In cases where efficiency is paramount then inline code and the compile-time resolution that happens with templates can put the burden of acceiving that efficiency on the compiler's shoulders, not the programmers.

    Low-level concepts can be wrapped in classes programmed so as to make them what I like to think of as "wafer-thin" (they melt in the compiler's mouth). STL implementations tend to be like this a lot (the spec was designed to make such implementations natural and easy), ATL is another source of examples of such classes.

    As such I wouldn't say it's wrong to "do C" in C++, just wrong to do it all the time.


Advertisement