• C++ Beginner
    45 replies, posted
  • Decided I want to learn C++. Started about three days ago a few hours a day reading the tutorials on [url]www.cplusplus.com[/url] and I have a few questions. First of all will I be able to learn it all online or should I buy a book? Is C++ even the best first language to learn? If you know C++ how did you learn? Here is something I have made from what I have learned so far. [code] #include<iostream> #include<string> #include<sstream> using namespace std; int addition(int a, int b) { int A; A = a + b; return (A); } int subtraction(int B, int C) { int S; S = B - C; return (S); } int multiplication(int d, int e) { int M; M = d * e; return (M); } int division(int f, int g) { int D; D = f / g; return (D); } int main() { string choice ("0"); int choiceb, x, y, z; cout << "1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\n\nSelect one of the above: #"; getline (cin, choice); stringstream(choice) >> choiceb; if (choiceb == 1) { cout << "\nYou selected Addition\n#"; cin >> x; cout << "Plus#"; cin >> y; z = addition (x,y); cout << "Equals: " << z; } else if (choiceb == 2) { cout << "\nYou selected Subtraction\n#"; cin >> x; cout << "Minus#"; cin >> y; z = subtraction (x,y); cout << "Equals: " << z; } else if (choiceb == 3) { cout << "\nYou selected Multiplication\n#"; cin >> x; cout << "Times#"; cin >> y; z = multiplication (x,y); cout << "Equals: " << z; } else if (choiceb == 4) { cout << "\nYou selected Division\n#"; cin >> x; cout << "Divided By#"; cin >> y; z = division (x,y); cout << "Equals: " << z; } else cout << "The number " << choiceb << " is not a working number. Please select 1-4"; } [/code] I know a lot of things in it are pointless but it was just for the learning experience. But please tell if you have any suggestions to make it better/smaller or anything i'm doing wrong. Last thing is what would be a good project for a beginner like myself? I know I have tons more to read and learn but I feel I learn best by actually making simple programs like these. P.S. This is my first post. Apologies if I'm posting this in the wrong section or something.
  • [QUOTE=Jookia;35870514]To make it smaller or better? Use operators instead of functions.[/QUOTE] The functions were just for testing It would be better to just use operators.
  • Avatar of darkrei9n
  • Rather than fifty million else ifs you can use a switch in this case. It would look like this. [code] switch(choiceb) { case 1: (code here) break; case 2: (code here) break; case 3: (code here) break; case 4: (code here) break; } [/code] And as Jookia said, use operators rather than functions.
  • Watching the entire 60+ videos of "C++ spoon feed" helped me a lot, especially when learning about pointers. [url]http://www.youtube.com/watch?v=tyVhn0FWWB4[/url] I recommend it to all first time programmers who want to start with C++. To answer your question about either learning from books or online: It is all a matter of personal preference. I've never bought a book and instead used online resources to help myself learn but what I did, most importantly, was practised. Always practise whenever you can until you fully understand something because solving problems on your own helps hard-wire these solutions into your brain.
  • [QUOTE=Giacomand;35870923]Watching the entire 60+ videos of "C++ spoon feed" helped me a lot, especially when learning about pointers. [url]http://www.youtube.com/watch?v=tyVhn0FWWB4[/url] I recommend it to all first time programmers who want to start with C++. To answer your question about either learning from books or online: It is all a matter of personal preference. I've never bought a book and instead used online resources to help myself learn but what I did, most importantly, was practised. Always practise whenever you can until you fully understand something because solving problems on your own helps hard-wire these solutions into your brain.[/QUOTE] Awesome! Those videos look like there going to help a bunch. Will definitely start watching them tomorrow.
  • Avatar of W00tbeer1
  • One thing I would personally do is change the temporary method variables all to be the same three names, such as A, a, and b. Since they are only used for each call and not globally, you can make it easier on yourself rather than making some named 'e' or 'g'. Not really a big deal, just something I would do.
  • Avatar of Mr_Razzums
  • [QUOTE=darkrei9n;35870606]Rather than fifty million else ifs you can use a switch in this case. It would look like this. [code] switch(choiceb) { case 1: (code here) break; case 2: (code here) break; case 3: (code here) break; case 4: (code here) break; } [/code] And as Jookia said, use operators rather than functions.[/QUOTE] Doing functions in this case is perfectly fine. The more you learn to use break things down into functions the easier OOP will be. Its good practice anyway.
  • Avatar of gparent
  • [QUOTE=Mr_Razzums;35878743]The more you learn to use break things down into functions the easier OOP will be.[/QUOTE] The more you learn about the difference between OOP and procedural programming, the easier both will be.. It is not good practice to have useless functions. In this case it almost makes sense because the OP is doing it on purpose, but in a real program you would eliminate the function since it is useless.
  • Avatar of Protocol7
  • [QUOTE=gparent;35888934]The more you learn about the difference between OOP and procedural programming, the easier both will be.. It is not good practice to have useless functions. In this case it almost makes sense because the OP is doing it on purpose, but in a real program you would eliminate the function since it is useless.[/QUOTE] C++ gets really cool once you implement custom classes. And that's obviously where OOP comes from. Even just little things, like in my most recent homework I had a data wrapper class and put it in a single vector instead of three vectors of standard datatypes. It's a minute difference but once I changed it, the difference was immediately clear and made the rest of the assignment easier than shit.
  • Avatar of ief014
  • So far from my experience, books have helped me much more than tutorials. This is what I used. Helped me a lot with pointers and references in C++ when I was starting out. [img_thumb]http://freebookgroup.com/media/2011/12/BeginCThroughGameProgram.jpg[/img_thumb]
  • Avatar of Protocol7
  • Ah, fucking pointers. Nuff said. When I was dumb in C++ they annoyed the hell out if me, I just put in ampersands and stars until things worked
  • Avatar of gparent
  • [QUOTE=Protocol7;35888985]C++ gets really cool once you implement custom classes.[/QUOTE] Separating your code into functions has very little to do with OOP. That was my point. I never said writing functions was bad or that OOP didn't make things more obvious. Just that they both have little to do with each other.
  • Avatar of Protocol7
  • [QUOTE=gparent;35890710]Separating your code into functions has very little to do with OOP. That was my point. I never said writing functions was bad or that OOP didn't make things more obvious. Just that they both have little to do with each other.[/QUOTE] I... was agreeing with you...?
  • Avatar of gparent
  • [QUOTE=Protocol7;35890740]I... was agreeing with you...?[/QUOTE] Great I suppose, just wasn't obvious from the way you basically kept preaching about OOP when I was making the point that it was irrelevant to the discussion...
  • Avatar of camacazie638
  • I currently have limited internet access and am searching for any possible .zips or .rars of sample code, training videos or tutorials. Also, besides starting to learn C++, are there any other languages that one could benefit more from in the coming years? Mobile apps seem to be in demand, but that’s just my opinion.
  • Avatar of WTF Nuke
  • [QUOTE=deathshead;35894309]Looks ok.. I suggest never using a computer again to ensure it doesn't suck more than it already does.[/QUOTE] Well that's rude.
  • Avatar of Soulmemories
  • Personally? A college class that forces you to do work and having a teacher or TA that will work with you helps loads. Also with the switch statement, you need to be careful if you make a bunch of those, they'll end up jumping between different case statements, and you'll wonder why you program is glitching up the whole time.
  • cplusplus.com was the best tutorial I know of; it's the first and only tutorial I used if you don't count me looking up syntax for certain functions and tutorials on GUI libraries as C++ tutorials. Only other thing you should need is google. And don't bother with books, they are always outdated to some point and contain a limited amount of knowledge, whereas with the internet, if you don't understand something, you just look it up. As long as internet is available, books will always be useless in my opinion. The internet is all of the world's knowledge a few keystrokes away; a book is just a few pages with a few webpages' worth of information which costs money. (unless you just download a .pdf of the book)
  • Avatar of gparent
  • [QUOTE=Timster480;35917507] And don't bother with books, they are always outdated to some point and contain a limited amount of knowledge, whereas with the internet, if you don't understand something, you just look it up. As long as internet is available, books will always be useless in my opinion. The internet is all of the world's knowledge a few keystrokes away; a book is just a few pages with a few webpages' worth of information which costs money. (unless you just download a .pdf of the book)[/QUOTE] The thing about books is that the technical content is often of a much more consistent quality and they go very in depth in the subjects they touch. Whereas cplusplus.com for example is merely a beginner's introduction to C++ and doesn't really teach you a lot of important stuff.
  • Avatar of T3hGamerDK
  • Though it's not as good as reading a book, or some of the more complete documentations on the web, there's a wiki that contains 'books' for programming, learning latin or spanish, how algorithms work and how you improve on them, and how computers work, and a lot more. I use it everyday, and improving works the same as wikipedia (it's wikimedia software) [url]http://en.wikibooks.org/wiki/Subject:Computing[/url] I thought you guys might find it VERY useful :)
  • I learned C++ from books, but the internet wasn't what it is today. [QUOTE=Timster480;35917507] And don't bother with books, they are always outdated to some point and contain a limited amount of knowledge...[/quote] This is often true, but not with C++. The language isn't changing. However... [QUOTE=Timster480;35917507]...whereas with the internet, if you don't understand something, you just look it up. As long as internet is available, books will always be useless in my opinion. The internet is all of the world's knowledge a few keystrokes away; a book is just a few pages with a few webpages' worth of information which costs money. (unless you just download a .pdf of the book)[/QUOTE] I pretty much agree with this. There's nothing so amazing about any of my books that would motivate me to recommend them, but I won't throw them out. [editline]12th May 2012[/editline] [QUOTE=darkrei9n;35870606]Rather than fifty million else ifs you can use a switch in this case. It would look like this.[/QUOTE] There is nothing wrong with using a stack of else-ifs, but changing to the code to use a switch is a good noob exercise. Knowing how to use both is good.
  • Avatar of Behemoth_PT
  • I think before you go to C++ or C# you should start with just C. I think before you go to C++ or C# you should get some experience in C too. I don't know any C++, never learned and don't know the differences but I can figure out what you're trying to do just by looking at the code. But that's just from my experience. [B]EDIT:[/B] I think for simple functions like multiplications, divisions, sums and subtractions you could make macros instead of functions. Macros aren't C/C++ but they define values or simple operations to a macro you want to make. for example: if you have, lets say a variable called a that repeats itself throughout the code and, let's say it's value is 50, you can define 50 as a macro for something like VALUE like this: instead of this: [CODE] int addition(int a, int b) { int A; A = a + b; return (A);} int subtraction(int B, int C) { int S; S = B - C; return (S); } int multiplication(int d, int e) { int M; M = d * e; return (M); } int division(int f, int g) { int D; D = f / g; return (D); }[/CODE] You can do this: [CODE]# define addition(a,b) ((a)+(b)) # define subtraction(a,b) ((a)-(b)) # define mult(a,b) ((a)*(b)) # define div(a,b) ((a)/(b)) [/CODE] Careful with this because it's not C and it's a bit advanced to you. #define is a pre-processing directive. And in some cases it will cause some algebric errors that your compiler will not warn you about. For example: see why I put ((a)*(b)) in the mult(a,b) function instead of just a*b? let's say I write this code with just a*b: [CODE] # include <stdio.h> # define mult(a,b) a*b main() { printf("%d*%d=%d\n", 3+1, 2+3, mult(3+1,2+3)); [/CODE] The output will be 4x5=8 witch is wrong! Why did the compiler do this? Because he assumed you were making the following operation (3+1*2+3)=8. Now Let's say we want to make a more complex operation like 1000/mult(2+3,7+3) So now that we no longer have problems with the sums inside our macro, the result of mult(2+3,7+3)=5*10=50, right? Being the final result 1000/50=20 Is it? [CODE]#include <stdio.h> #define mult(a,b) (a)*(b) main() { printf("%d\n", 1000/mult(2+3,7+3));[/CODE] } $ prog 2000 $ Weird, eh? The result of the division is bigger than 1000. Let's see if you can figure this one out by looking at the expansion made by the pre-processor: [CODE]printf("%d\n", 1000\(2+3)*(7+3));[/CODE] So basicaly multiplication and division have the same order of precedence, so the following will happen: 1000/5 = 200 200*10 = 2000 That's why you use: [CODE]#define mult(a,b) ((a)*(b))[/CODE] That's it! This is the same as making "#define NUM 100" except this time you're using #define to define instructions instead of values. However if you want to define a value using C you use constants: C [CODE]const int num = 10[/CODE] not C, yet usable: [CODE]#define NUM 10[/CODE] Note that macros are a useful and quick alternative to some functions in C. Since you're a beginner I recomend you train functions first before jumping to the use of macros. And if you do use macros use them as a substitute for the most simple and generic functions like sums, multiplications... etc etc... Macros are more complex than what I just taught you. You can make lots of functions with them and other cool stuff that you'll surely learn by yourself in the future. However, don't use them now. Use them when you've mastered cycles, functions, files, pointers or vectors first.
  • Avatar of gparent
  • Or you can just use functions and rely on the compiler to optimize this for you. And make your code debuggable at the same time.
  • Avatar of blankthemuffin
  • [QUOTE=Behemoth_PT;35937732]Note that macros are a useful and quick alternative to some functions in C. Since you're a beginner I recomend you train functions first before jumping to the use of macros. And if you do use macros use them as a substitute for the most simple and generic functions like sums, multiplications... etc etc...[/QUOTE] There is absolutely no reason to use the preprocessor here, or even bring it up really. Not sure what the fuck you're talking about when you say the preprocessor is not C.
  • Avatar of Behemoth_PT
  • I was just giving a tip in macros to avoid writing big functions that do so little. Nothing important, really, just informational. It's not the pre-processor that isn't C, its the usage of #define. That isn't C. Go check. [editline]14th May 2012[/editline] [QUOTE=gparent;35943489]Or you can just use functions and rely on the compiler to optimize this for you. And make your code debuggable at the same time.[/QUOTE] That's why I recomended the usage of macros in small functions instead of big ones. Because with macros, if you have an error or mistake it will be a pain in the ass to try to figure it out. Also I don't use any debugger. Where I study they just want us to use the fedora text editor and the console, and that's it. So basically when we get a segmentation fault, it's chaos. But, yes, as I said before, I was just giving a useful tip because there is no harm in using macros as substitutes for such small functions. As for bigger and more complex functions, absolutely, use functions!
  • Avatar of Jookia
  • [QUOTE=Behemoth_PT;35950793]there is no harm in using macros as substitutes for such small functions.[/QUOTE] Yes, there is. You lose type safety, the compiler doesn't get to optimize it, you can cause unintended side effects.
  • Avatar of blankthemuffin
  • [QUOTE=Behemoth_PT;35950793]I was just giving a tip in macros to avoid writing big functions that do so little. Nothing important, really, just informational. It's not the pre-processor that isn't C, its the usage of #define. That isn't C. Go check.[/QUOTE] Yes it is, define is most definitely a part of ISO C99, I just checked. Section 6.10 might be worth a read. [QUOTE=Behemoth_PT;35950793] That's why I recomended the usage of macros in small functions instead of big ones. Because with macros, if you have an error or mistake it will be a pain in the ass to try to figure it out. Also I don't use any debugger. Where I study they just want us to use the fedora text editor and the console, and that's it. So basically when we get a segmentation fault, it's chaos. But, yes, as I said before, I was just giving a useful tip because there is no harm in using macros as substitutes for such small functions. As for bigger and more complex functions, absolutely, use functions![/QUOTE] There's a reason to use macros in this example, the fact that they're dumb text replacements that don't worry about type. This means the function will behave like the standard operators because that's really all they expand to. Please don't take that as you being right though, since for the most part there's no reason to replace a function, small or large with a macro unless you actually know what you're doing. The lack of type safety and the code bloat they generate is completely counter productive for the general case. Not to mention without a froody compiler like clang they can be a pain in the ass to debug.