Learning C/C++ - Tutorial

Discussion in 'Programming & Scripting' started by Fallen, Jun 14, 2012.

  1. OmfgRegister

    OmfgRegister Member

    Joined:
    Mar 7, 2009
    Messages:
    151
    I like the effort, but I think its misguided and rushed, the rushed part isn't your fault because you can't do a c++ tutorial in three forum posts and expect people to understand everything..
    As for the misguided part, i have no idea why you start in C.

    In my opinion you should have an object oriented view of the language from the beginning so when you do introduce objects you will have an idea of how they work.
    It would also allow you to black box pointers to a later time when they are needed.. Introducing pointers at an early stage is just dumb imo, It is something that you need concrete examples of why you would actually use them.
    Furthermore newbs should be using the std::String class from the beginning and leaving out the internals of the char* until they need to be told about after the chapter one arrays and during the chapter on pointers.

    Don't get me wrong its a good tutorial it just tries to introduce too many concepts at once. I feel that indepth C++ tutorials should be left for books and not forum topics. And you should leave the forums for OT specific coding. Or very very basic C++ (variables, loops, conditional statements, functions)

    Don't let this discourage you, I applaud your efforts and your tutorial is pretty good in covering everything.


    Edit: Please added Visual Studio to your list of compilers, It is by far the nicest Compiler (imo). And it is almost solely used in a professional setting. The express version of it is free and has all the features you would need, and all the features the other ones have.
     
  2. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    Just some corrections...
    On the switch part: If it's the last statement then you don't have to "break" the operation since the switch will stop after it.
    e.g.
    [CPP]int main() {
    int i = 5;

    switch (i) {
    case 1:
    printf("i is 1.");
    break;
    case 2:
    printf("i is 2.");
    break;
    default:
    printf("i is not 1 or 2.");
    }
    return 0;
    }[/CPP]
    [CPP]int main() {
    int i = 5;

    switch (i){
    case 1:
    printf("i is 1.");
    break;
    case 2:
    printf("i is 2.");
    break;
    case 3:
    printf("i is not 1 or 2.");
    } //then case 4 and case 5
    return 0;
    }[/CPP]
    General: When you have an operation with a single action you don't necessarily have to put it inside brackets ({})
    e.g.
    [CPP]if(true)
    printf("BUY SOME APPLES");[/CPP]

    [CPP]for(int i = 0; i < 11; i++)
    printf("Counting ponies: %d", i);[/CPP]
     
  3. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    @Ratser you're absolutely correct, thanks; I'll edit my main post soon.
    @Omfgregister I agree with with some parts of your post, expect a somewhat re-write in the near future!
     
    Last edited: Jul 1, 2012
  4. Core_

    Core_ Well-Known Member

    Joined:
    Jul 9, 2010
    Messages:
    1,551
    =D Great tutorial just start reading it, and here is my first homework.

    [​IMG]

    And the second.

    [​IMG]

    [Cpp]#include <stdio.h>
    int main() {
    int drinks = 0;
    printf("- Choose your favorite beverage using its number.\n [1]Coke \n [2]Beer \n [3]Dr.Pepper \n [4]Fanta \n [5]Tequila \n: ");
    scanf("%d", &drinks);
    switch (drinks){
    case 1:
    printf("- Your choose is Coke\n");
    break;
    case 2:
    printf("- Your choose is Beer\n");
    break;
    case 3:
    printf("- Your choose is Dr.Pepper\n");
    break;
    case 4:
    printf("- Your choose is Fanta\n");
    break;
    case 5:
    printf("- Your choose is Tequila\n");
    }
    }
    [/Cpp]
    oops forgot that..
    [​IMG]
    [cpp]#include <stdio.h>
    int main() {
    int drinks = 0;
    printf("- Choose your favorite beverage using its number.\n [1]Coke \n [2]Beer \n [3]Dr.Pepper \n [4]Fanta \n [5]Tequila \n: ");
    scanf("%d", &drinks);
    if (drinks >= 6) {
    printf("-THERE'S NO SUCH OPTION");
    }
    else {
    switch (drinks){
    case 1:
    printf("- Your choose is Coke\n");
    break;
    case 2:
    printf("- Your choose is Beer\n");
    break;
    case 3:
    printf("- Your choose is Dr.Pepper\n");
    break;
    case 4:
    printf("- Your choose is Fanta\n");
    break;
    case 5:
    printf("- Your choose is Tequila\n");
    }
    }
    }[/cpp]
     
    Last edited: Jul 1, 2012
  5. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    Just a tip. Instead of doing...
    [CPP]
    if (drinks >= 6) {
    printf("-THERE'S NO SUCH OPTION");
    }
    else {[/CPP]
    ...add "printf("-THERE'S NO SUCH OPTION");" as a "default" action at the end of the switch. It's basically the same thing but saves you some lines.
    e.g.
    [CPP]
    switch (somenumbers)
    {
    case 1:
    printf("ONE!\n");
    break;
    case 2:
    printf("TWO!\n");
    break;
    default: //this will be executed when "somenumbers" isnt "1" or "2"
    printf("THIS ISNT ONE OR TWO\n");
    }[/CPP]
     
  6. Core_

    Core_ Well-Known Member

    Joined:
    Jul 9, 2010
    Messages:
    1,551
    :p right thanks
     
  7. Zatjin

    Zatjin Premium User Premium User

    Joined:
    Sep 18, 2011
    Messages:
    2,198
    What are you guys writing in?

    Want to learn! :-D

    ---------

    I'm not able to build my code, any suggestions?

    At the build log it's checking wether the code is at my computer or not.


    ---update:
    created a project;
    this:
    "Homework - Debug" uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...
    Nothing to be done.
     
    Last edited: Jul 2, 2012
  8. Shinmaru

    Shinmaru エロルアー Scripter!

    Joined:
    Aug 20, 2007
    Messages:
    1,988
    Naj tuto!
    let me clear up something:
    () >>> Parenthesis
    [] >>> Brackets
    {} >>> Curly Brackets
     
  9. OmfgRegister

    OmfgRegister Member

    Joined:
    Mar 7, 2009
    Messages:
    151
    What compiler are you using?, my suggestion would be to download microsoft visual studio 2010 express.. its free and is the best out for c++ imo
     
  10. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    Yep. Visual Studio is the best one atm.
    @Zatjin: I'd say you should get VS 2010 or 2008. 2010 is better to check errors with more detail, but 2008 has a better and noob-friendly auto-complete (IntelliSense).

    Since there's no explanation on how to start it a console application project in VS here: File>>New>>Project...>>... then I don't know what template are they using here. I would go for Visual C++>>CLR>>CLR Console Application but it could also be a Win32 application Visual C++>>Win32>>Win32 Console Application.
    Anyways, after you choose the template, create a project name, a solution name and that's it.
     
  11. Zatjin

    Zatjin Premium User Premium User

    Joined:
    Sep 18, 2011
    Messages:
    2,198
    Thanks, but T.Core told me to get Dev-CPP.


    It's working, and I love it :D
     
  12. Evan

    Evan A splendid one to behold Staff Member Global Moderator

    Joined:
    May 6, 2009
    Messages:
    7,014
    I'll post a set-up tutorial on how to start programming in MSVC++ later today.
    IMO, MSVC is much better.
     
  13. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Add a small thing to this, if you're not going to format a string do not use printf, use puts, faster.
     
  14. Zatjin

    Zatjin Premium User Premium User

    Joined:
    Sep 18, 2011
    Messages:
    2,198
    I made my own calculater, which adds and multiplies, what do I get?! :D

    A + code which loops five times.
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main (void)
    {
         int num1;
         int num2;
         int sum;
         int loopcount;
         loopcount = 0;
         
         while(loopcount<5){
        printf("\nEnter your number:");
         scanf("%d", &num1);
         scanf("%d", &num2);
         sum = num1 + num2;
         printf("Sum was %d", sum);
         getch();
         loopcount = loopcount + 1;
    }
    }
    The result: [​IMG]


    A multiplying code which doesn't loop.
    Code:
    #include <stdio.h>
    
    int mult (int x, int y);
    
    int main (void)
    {
        int x;
        int y;
        
        printf( "Please put two numbers to be multiplied:");
        scanf( "%d", &x);
        scanf( "%d", &y);
        printf("The answer of your two numbers %d\n", mult ( x, y) );
        getch();
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }
    
    The result: [​IMG]
     
  15. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    @Zatjin: Good idea, I have an idea for you (alittle bit harder than this one):
    Write a program that asks the user the numbers he want to calculate & using what operation (+, -, /, *, ...) - can be done with switch statements and characters. (Consider this a new excersie, i'll add it to main post).
     
  16. Zatjin

    Zatjin Premium User Premium User

    Joined:
    Sep 18, 2011
    Messages:
    2,198
    Oh gosh.
    I will try.

    *cough* teacher, need helpz *cough*


    --------------UPDATE----------------

    [​IMG]
    What I have for now.

    Going to have to do like if I press 3, it'll tell you: You chose option nr. 3 +. Please tell me the numbers you would like to be summed up.
    And it'll sum up it. It variates from each one.

    If you choose 1 which is multiplying it'll tell you: You chose option nr. 1 *. Please tell me the numbers you would like multiplied.

    Then it remains how I'll do that!
     
    Last edited: Jul 2, 2012
  17. OmfgRegister

    OmfgRegister Member

    Joined:
    Mar 7, 2009
    Messages:
    151
    Raster you must of botched an installation or something, they all have IntelliSense.. 2010 has error highlighting too which is really nice compared to 08

    But yea if anything they hadd more features to the intellisense throughout the versions
     
  18. Erikas Kontenis

    Erikas Kontenis Well-Known Member

    Joined:
    Jul 3, 2009
    Messages:
    1,438
    If I were you I would use like this.

    Code:
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int choise;
    	
    	cin >> choise;
    	
    	switch(choise)
    	{
    		case 1:
    			cout << "Choosed: 1";
    			break;
    		case 2:
    			cout << "Choosed: 2";
    			break;
    		default:
    			cout << "Choose between numbers which I said.";
    	}
    		
        system("\n\nPAUSE");
        return EXIT_SUCCESS;
    }
    
    
     
  19. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Why can't you do it? It's quite simple with your current knowledge, but here it is if you're really stuck.
    [cpp]#include <stdio.h>

    int main() {
    puts("Press CTRL-C to quit or type */+- to calculate 2 numbers of your choice.");
    for (;;) {
    char c;
    if ((c = fgetc(stdin)) == EOF) {
    puts("Bye.");
    return 0;
    }

    switch (c) {
    case '+':
    puts("addition");
    break;
    case '-':
    puts("subtract");
    break;
    case '/':
    puts("divide");
    break;
    case '*':
    puts("multiply");
    break;
    default:
    puts("Invalid operator, try again.");
    break;
    }
    fflush(stdin);
    }
    /* will never reach here. */
    }
    [/cpp]
     
  20. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    I dunno how to use puts although I think I'm gonna have to learn it next month when I start doing Form Applications in college.

    Of course, I know both VS 2008 and 2010 have IntelliSense. What I mean is that compared to VS2008, VS2010 doesn't have a drop-down function list. The way IntelliSense works in VS2010 is more like for tracking errors, not autocomplete like VS2008.
     

Share This Page