Thread: C++ Final!

Page 3 of 3 FirstFirst
1
2
3
  1. #41
    Mmm and all these answers are way more advanced than what an introductory C++ course would even expose him to (TBH, I can't even remember the inefficient methods I had to use in my 1st year of C++ lol)

  2. #42

  3. #43
    Deleted
    how can matlab be better, when you can't create an operating system with matlab? Completly different things...
    And i would use APL

  4. #44
    Quote Originally Posted by Vernochan View Post
    ok, sounded like "if you want any resizable array, use a std::vector"
    I suppose I shouldn't write off deque (I normally don't consider it as I tend to only insert from the back when I'm using vector). In all fairness for most learning purposes it doesn't matter too much what library containers they are using (unless they are dealing with thousands of objects stored inside where the differences in an action can be as much as minutes between containers).

    Like an example I just plucked from Accelerated C++, (5.5.1 p86 and 5.5.2 p87) when deleting randomly through a vector and list the times to execute:

    File size: 735 list: 0.1 vector: 0.1
    File size: 7350 list : 0.8 vector: 6.7
    File size: 73500 list: 8.8 vector 597.1

    Just illustrates like programming languages data structures are the right tool for the right job when crunching potentially real data and your flowchart illustrates perfectly (and I will be shamelessly using it myself).

    Quote Originally Posted by Synesis View Post
    Mmm and all these answers are way more advanced than what an introductory C++ course would even expose him to (TBH, I can't even remember the inefficient methods I had to use in my 1st year of C++ lol)
    Yea, I've just finished an "introductory c++" module as part of my degree (I have a fair bit of C++ experience behind me beforehand) and essentially everything was array or vector with linked list tagged on the end as an unexamined part of the module.

  5. #45
    Quote Originally Posted by Synesis View Post
    Mmm and all these answers are way more advanced than what an introductory C++ course would even expose him to )
    yea lol i cant figure out how to fix the infinite loop, and although i would like to fix it i think ill just keep it as is.... im feeling pretty good about my grade anyway

  6. #46
    Runs great, just a side note. My java teacher always tells me that select statements are more efficient than if statements. I dont really listen to him, IF's are a default of mine i guess. Either way, only thing i could reccomend is replacing the if's with select case's.

  7. #47
    cplusplus.com/reference/clibrary/cctype/isalpha/
    cplusplus.com/reference/clibrary/cctype/isalnum/

    Please, make your input checking life easier...

  8. #48
    Deleted
    Quote Originally Posted by TheGiant89 View Post
    Runs great, just a side note. My java teacher always tells me that select statements are more efficient than if statements. I dont really listen to him, IF's are a default of mine i guess. Either way, only thing i could reccomend is replacing the if's with select case's.
    Maybe it is a bit more efficient, but it's not really important, because it's not a time critical application.


    Quote Originally Posted by Erradic View Post
    yea lol i cant figure out how to fix the infinite loop, and although i would like to fix it i think ill just keep it as is.... im feeling pretty good about my grade anyway
    As i said, try to read everything into a string and then parse that string to your valid input type.

    something like

    Code:
    cin >> input; // input has to be a string
    int value = atoi( input.c_str());
    atoi returns the given c-string as integer. returns 0 if it fails and returns INT_MAX or INT_MIN if the value is too large/small for an int.

    there is also atof (string to float).


    with that, you should be able to avoid infinite loops because of wrong inputs.

    also, use cin.clear() to clear your input buffer. that removes any input, you did not process already.

  9. #49
    Accelerated C++, outdated as hell but its still the most recommended book on C++.

    C++ Primer 5th edition.

    These 2 books are -THE- most recommended for beginners, the first book teaches you C++ by making real life programs and the second book gives you a better understanding of C++.

    However, since i'm the kind of person who doesn't learn as well from reading only, i started with Buzz 3D's C++ VTMs. If you have the money, get them all. I already have Accelerated C++ and it's a really good book, downside is the exercises dont have answers.

  10. #50
    High Overlord
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    Holland
    Posts
    112
    http://www.devmaster.net/articles/in...-dev/part1.php
    This is an 20 part long C++ introduction that learns you all the basics for making small games with C++ i am atm in part 12 and enjoyed it alot!

  11. #51
    Deleted
    It looks good at first sight. Although i currently use c#, i think that introduction can be quite useful for that too.

  12. #52
    Quote Originally Posted by Vernochan View Post
    atoi returns the given c-string as integer. returns 0 if it fails and returns INT_MAX or INT_MIN if the value is too large/small for an int.

    there is also atof (string to float).
    Never use the atoi/atof family of functions, they have no way of indicating an error... When the function returns 0 how do you know it did not just successfully parse 0?

    You should use Boost.LexicalCast or if not available due to this being an assignment, you can just write a little template like this:
    Code:
    template<typename To, typename From>
    To convert(const From& value)
    {
        std::stringstream ss;
        To result;
        if (!(ss << value && ss >> result))
            throw std::runtime_error("conversion failed");
        return result;
    }
    Use like this:
    Code:
    string foo = 42;
    int bar = convert<int>(foo);
    Last edited by ZyngaFail; 2011-04-20 at 11:20 AM.

  13. #53
    The thing that glares at me the most from this program is your switch statements. Write it into a function outside of your main() and call it when needed.

    The lesson you should learn is to take code that you find yourself copy/pasting and instead write a function to handle it. You'll save yourself lots of hassle doing this. Just imagine if you wanted to changed your hard-coded board... you'd have to change it EVERYWHERE. Or, imagine that the first time you wrote that block and copy/pasted it everywhere there was a small error in it. Again, you'd have to go back and waste time changing that typo in every location.

    TL;DR version - you should NEVER be copy/pasting code in multiple places. Use function calls instead.

    edit - just so you know - it bothered me enough to register an account with the site so I could post this.
    Last edited by titeywitey; 2011-04-20 at 11:25 AM.

  14. #54
    Deleted
    Quote Originally Posted by ZyngaFail View Post
    Never use the atoi/atof family of functions, they have no way of indicating an error... When the function returns 0 how do you know it did not just successfully parse 0?

    You should use Boost.LexicalCast or if not available due to this being an assignment, you can just write a little template like this:
    Code:
    template<typename To, typename From>
    To convert(const From& value)
    {
        std::stringstream ss;
        To result;
        if (!(ss << value && ss >> result))
            throw std::runtime_error("conversion failed");
        return result;
    }
    Use like this:
    Code:
    string foo = 42;
    int bar = convert<int>(foo);
    ofc you are right, that there are better ways.
    and i already mentioned boost::lexical_cast. but atoi is exactly the right thing for anyone, who is lerning the stuff from the beginning. And we don't know, what the OP already knows and what not, because he didn't post any source code (which is ok, but we have to make many assumptions. and i tend to believe that he did not learn that much in his class). if he never used any other library than std or doesn't know what templates are, it's perfectly fine to do it with atoi for the time being.

  15. #55
    Quote Originally Posted by Vernochan View Post
    And we don't know, what the OP already knows and what not, because he didn't post any source code
    You should check the OP again...

  16. #56
    Quote Originally Posted by Vernochan View Post
    but atoi is exactly the right thing for anyone, who is lerning the stuff from the beginning. *snip* if he never used any other library than std or doesn't know what templates are, it's perfectly fine to do it with atoi for the time being.
    If he is using (just) the standard library (which he is due to using std::string) he should be using std::stringstream as the template and my example on page 2 illustrate. The sooner they learn to use stringstream effectively any conversions where you can't use boost::lexical_cast are a piece of cake without using c standard library functions (which atoi and atof are).

  17. #57
    Deleted
    Quote Originally Posted by titeywitey View Post
    You should check the OP again...
    haven't seen that edit. ok then... ^^
    but now i don't have time for the next few days to look at the source :/

  18. #58
    Deleted
    Quote Originally Posted by Lazaroz View Post
    Seriously though, what is it?
    From what I have gathered, its a game where you play by using C++ codes(as far as im aware, a type of programming) seems too typy for me though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •