Learning C/C++ - Tutorial

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

  1. OmfgRegister

    OmfgRegister Active Member

    Joined:
    Mar 7, 2009
    Messages:
    173
    Likes Received:
    46
    Best Answers:
    0
    2010s intelliSense can do everything that 2008s can do.. if you want a list of available functions hit control space.. control space is like the auto complete hot key

    Maybe you could provide a concrete example where 2010's intellisense fails comparison to 2008?
     
  2. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    261
    Best Answers:
    0
    Using puts is simple, but just abit faster than printf, and using it when there's no need for formatting, i.e:
    [cpp]puts("hello world");[/cpp]
    makes more sense than:
    [cpp]printf("hello world\n");[/cpp]
    see: c++ - mixing cout and printf for faster output - Stack Overflow
     
  3. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    Likes Received:
    33
    Best Answers:
    0
    Ok. Especifically, IntelliSense's autocomplete function doesn't work in VS2010 with C++ projects and it even shows a message at the lower left corner saying "Visual C++ 2010 does not have IntelliSense for C++/CLI.". Apparently the support for it was reduced because of time and resource constraints.

    As long as it isn't the same case as system("cls") and Console::Clear where the first one uses more resources to go faster, it's ok lol.
     
  4. OmfgRegister

    OmfgRegister Active Member

    Joined:
    Mar 7, 2009
    Messages:
    173
    Likes Received:
    46
    Best Answers:
    0
    And yes intellisense is depreciated with CLI but if your doing normal console apps and nothing visual you won't have that problem
    They depreciated it because they basicly dropped support for CLI and the intellisense would no longer be up to date (from what i've read atleast)
     
  5. Evan

    Evan A splendid one to behold Premium User

    Joined:
    May 6, 2009
    Messages:
    7,011
    Likes Received:
    923
    Best Answers:
    0
    This is the way how I set up a new C++ file




    STEP 1:
    Click "New Project"
    [​IMG]

    STEP 2:
    Click "Win32", then click "Win32 Console Application", then come up with the name for the file
    [​IMG]

    STEP 3:
    Uncheck "Precompiled header" and then check "Empty project"
    [​IMG]

    STEP 4:
    Right click "Source Files" folder to add a new cpp file
    [​IMG]

    STEP 5:
    Select "CPP File" and come up with a name for the file
    [​IMG]

    STEP 6:
    Success
    [​IMG]
     
    OmfgRegister likes this.
  6. Ratser

    Ratser Retired // Making time for yoloswagmod

    Joined:
    Feb 11, 2009
    Messages:
    482
    Likes Received:
    33
    Best Answers:
    0
    Now I get it, I forgot to mention I start all my projects with CLR Console Application because most of the things I learned in college needed .Net Framework and well...I kinda got the habit of always starting my projects with the .NET compilation settings on by default. That's why IntelliSense autocomplete doesn't works in my case, but if you're making a Win32 Console Application it will totally work as you say. Also, afaik they are planning to re-add the autocomplete support in CLI in VS2012.
     
  7. OmfgRegister

    OmfgRegister Active Member

    Joined:
    Mar 7, 2009
    Messages:
    173
    Likes Received:
    46
    Best Answers:
    0
    Your correct on the 2012 inclusion.. although as of right now i don't see a forms application in the CLR section as in 2010.. they give you CLR Console app, Class Lib, and CLR empty project
    I'm running the RC version of 2012 they might add forms back into the full version, 2012 is pretty sweet if you haven't checked it out yet.. my favorite addition: they added a drop down for all the files in your project and it will list what functions and classes are in those files

    EDIT - you can add forms really easily onto blank apps.. all functionality seems to be returned they definiatly fixed intellisense without a doubt.. might actually start making visual forms in c++ now instead of java.. gotta see how easy it is :)
     
  8. _Arthur

    _Arthur

    Joined:
    Oct 22, 2009
    Messages:
    2,195
    Likes Received:
    151
    Best Answers:
    0
    about time ;3
     
  9. Korrex

    Korrex Well-Known Member

    Joined:
    Jan 19, 2008
    Messages:
    2,749
    Likes Received:
    116
    Best Answers:
    0
    ... or you could just make it an empty project to start with ;p.

    [​IMG]
     
  10. JuanC

    JuanC Member

    Joined:
    Sep 24, 2009
    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    C++ is better?
     
  11. Zatjin

    Zatjin Premium User Premium User

    Joined:
    Sep 18, 2011
    Messages:
    2,202
    Likes Received:
    212
    Best Answers:
    0
    Weey! I've started once again after a little break :)

    I've messed around a little, but forgot the most. So I am going to start with lesson one.
    Updating this soon.
     
  12. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    261
    Best Answers:
    0
    Bump.
    Well, it depends on what you're using it for... but C is low-level, I'd recommend escaping the strings part of C explained in this tutorial and jumping right into C++... C is just, hard.

    Doing an update soon.
     
    Last edited: Aug 13, 2012
  13. meliis

    meliis Member

    Joined:
    Feb 17, 2008
    Messages:
    121
    Likes Received:
    12
    Best Answers:
    0
    why do you use printf? why dont you use cout ?? :eek: ive programmed in 1 year in school but my teacher sucked and i did not learn much from him..
     
  14. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    261
    Best Answers:
    0
    I used printf when teaching C not C++
     
  15. Reaper7

    Reaper7 dunno wat 2 rite here

    Joined:
    Feb 18, 2011
    Messages:
    133
    Likes Received:
    1
    Best Answers:
    0
    hey dude, why wont use conio.h and getch instead ? well, aint actually sure if u can use them in c, but its certainly available in c++ :p
     
  16. Jetro

    Jetro jangeldev

    Joined:
    Aug 1, 2011
    Messages:
    452
    Likes Received:
    67
    Best Answers:
    0
    @Fallen
    could you explain a bit about this? I've been reading and i found it too useful
     
  17. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    261
    Best Answers:
    0
    @Repear7 conio.h is a C library
    @Jetro Sure, that's easy.

    - - - Updated - - -

    There you go (It can be even sexier tho :p):
    Code (Text):
    1. #include <iostream>  /* I/O Streaming */
    2. #include <stdio.h>
    3. #include <string>    /* C++ string */
    4. #include <errno.h>   /* errror number */
    5. #include <string.h> /* for strerror() */
    6. #include <sys/stat.h>  /* stat() the file */
    7. #include <stdexcept>
    8.  
    9. class FileException : public std::exception {
    10. public:
    11.     FileException(const std::string& error) throw() { m_error = error + " " + std::string(strerror(errno)); }
    12.     ~FileException() throw() { }  /* do nothing */
    13.  
    14.     const char *what() const throw() { return m_error.c_str(); }
    15. private:
    16.     std::string m_error;
    17. };
    18.  
    19. class File {
    20.     /// Disable copying and/or assignments
    21.     File(const File& file);
    22.     File& operator=(const File& f);
    23.  
    24. public:
    25.     enum FileOpenMode {
    26.         OpenRead,
    27.         OpenWrite,
    28.         OpenReadWrite
    29.     };
    30.  
    31.     File(const std::string& name)
    32.         : m_file(NULL), m_fileName(name)
    33.     { }
    34.  
    35.     ~File() { close(); }
    36.  
    37.     void open(File::FileOpenMode _mode) {
    38.         std::string mode;
    39.         switch (_mode) {
    40.             case File::OpenRead:        mode = "r"; break;
    41.             case File::OpenWrite:       mode = "w"; break;
    42.             case File::OpenReadWrite:   mode = "rw"; break;
    43.             default:   /* work around for invalid modes */
    44.                 throw FileException("Invalid file open mode");
    45.         }
    46.         m_file = fopen(m_fileName.c_str(), mode.c_str());
    47.         if (!m_file)
    48.             throw FileException("failed to open file");
    49.     }
    50.  
    51.     void close() {
    52.         if (m_file && fclose(m_file))
    53.             throw FileException("failed to close file");
    54.     }
    55.  
    56.     void write(const std::string& str, int len = -1) {
    57.         if (len == -1)    len = str.length();
    58.         if (!m_file)
    59.             throw FileException("write() called when file is not open");
    60.  
    61.         if (fputs(str.c_str(), m_file) != len)
    62.             throw FileException("cannot write to file");
    63.     }
    64.  
    65.     void read(std::string& str, int len) {
    66.         if (!m_file)
    67.             throw FileException("read() called when file is not open");
    68.         char buff[len+1];
    69.         if (!fgets(buff, len, m_file))
    70.             throw FileException("cannot read from file");
    71.         buff[len] = '\0';
    72.         str = buff;
    73.     }
    74.  
    75.     int size() {
    76.         struct stat st;
    77.         if (stat(m_fileName.c_str(), &st) != 0)
    78.             throw FileException("cannot stat() the file");
    79.         return st.st_size;
    80.     }
    81.  
    82. private:
    83.     FILE *m_file;
    84.     std::string m_fileName;
    85. };
    86.  
    87. int main(int argc, char *argv[]) {
    88.     std::string fname = "hello.txt";
    89.     if (argc > 1)
    90.         fname = argv[1];
    91.  
    92.     std::clog << "Opening " << fname << std::endl;
    93.     File f(fname);
    94.     f.open(File::OpenRead);
    95.  
    96.     std::string str;
    97.     f.read(str, f.size());
    98.     std::clog << "read: " << str << std::endl;
    99.     return 0;
    100. }
    101.  
     
  18. Alisa1

    Alisa1 New Member

    Joined:
    Oct 29, 2012
    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Hiiiii Fallen,

    You explain the concept of C language in very easy way ...... I understand that you Explain.
    I just read only the Basics of C Language and I am also very excited to read the Part 2 and 3..
    I hope i will get lots of information about C Language from Part 2 and 3.......
     
  19. kingsley666

    kingsley666 Member

    Joined:
    Jul 24, 2011
    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    wow great tutorial, will you continue posting the rest of the turotial partS? i want to learn :)
     
  20. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    261
    Best Answers:
    0
    Not until next summer holiday or maybe never, we'll see. ; -)
     

Share This Page

Loading...