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)
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)
c++ sucks, matlab is better
how can matlab be better, when you can't create an operating system with matlab? Completly different things...
And i would use APL
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).
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.
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.
cplusplus.com/reference/clibrary/cctype/isalpha/
cplusplus.com/reference/clibrary/cctype/isalnum/
Please, make your input checking life easier...
Maybe it is a bit more efficient, but it's not really important, because it's not a time critical application.
As i said, try to read everything into a string and then parse that string to your valid input type.
something like
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.Code:cin >> input; // input has to be a string int value = atoi( input.c_str());
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.
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.
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!
It looks good at first sight. Although i currently use c#, i think that introduction can be quite useful for that too.
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:
Use 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; }
Code:string foo = 42; int bar = convert<int>(foo);
Last edited by ZyngaFail; 2011-04-20 at 11:20 AM.
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.
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.
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).