Tips For C++ Programmers - Read & Learn

Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();



2) While programming for C++, instead of
#include 


use

#include 
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}

The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                     // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS

Replies

  • Ankita Katdare
    Ankita Katdare
    #-Link-Snipped-# That is a really useful post. I have pinned it. Keep writing useful articles and posts as tutorials for CEans.
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    Thanks a lot! ๐Ÿ˜€ I'll update this post if I get along some new things to make the program efficient!
  • Priya Nadkarni
    Priya Nadkarni
    Thank You. That was really useful.๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    good to know that! ๐Ÿ˜€
  • monujatt
    monujatt
    With reference to this -> Small tips for programmers::
    Your code should have proper comments ...sometimes programmer itself not able to understand what an API does(even that is written by him).... ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    Yeah! I've seen such people.. ๐Ÿ˜จ
  • sulochana anand
    sulochana anand
    good knowledge given by u.thanx
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    thanks .. ๐Ÿ˜€
  • gaurav91
    gaurav91
    i too had worked with c++!! but i didn't knew most of it ..true knowledge ..
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    There are many more things.
    Like the use of iomanip.h
    In C++, it is another header which contains the functions, which can make your presentation on output screen better!! ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    ONE MORE THING I FORGOT TO ADD IN THE POST!

    Avoid using conio.h header

    conio.h is not considered as the standard library. All the compilers may not support this header. Hence we avoid using it.

    So, the functions like clrscr(); and getch(); are also considered as the non standard functions. As, I've earlier mentioned, the alternative for clrscr(); is system("cls");
    Similarly, for getch(); the alternative is getchar(); which is in stdio.h

    Do not forget to flush the input stream before using getchar(); and its equivalent in C++ cin.get();
  • gaurav91
    gaurav91
    hey since when r u working in c++?
  • Vishal Sharma
    Vishal Sharma
    gaurav91
    hey since when r u working in c++?
    I'm a second year student, so, Just 3 months.. ๐Ÿ˜€
  • gaurav91
    gaurav91
    wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
  • Vishal Sharma
    Vishal Sharma
    gaurav91
    wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
    The books doesn't have anything related to all this. The books just teach us the basics. To make your skills advanced, do a research. ๐Ÿ˜€
    The best thing, search the internet, join useful forums! There are many intellects across the world who are keen to help you out! And they even help making your programs more efficient!
    These tips are used for efficient programming!
  • gaurav91
    gaurav91
    that's y asked becoz ..i didn't find all these tips or anything related anywhere in the books .. but all these tips r one of the unknown.. and u r sharing ur knowledge that's good.. for people like me also ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    ๐Ÿ˜€ I'm glad it's helpful.
    majority of people in India are unaware of these things as we are never taught about all this in our college. So, the forums like CE create good platform of learning!
  • David Emperor
    David Emperor
    Hey dude ๐Ÿ˜€ add for people
    that scanf() and printf () are working fast than cin and cout
    the view of scanf is scanf ("%/*const char * format*/", variable);
    formats are d- decimal integer , e,E,f,g,G - float types, u - unsigned int, h, H- hexidemical number, s - string, c-char , o - octal integer
    for example scanf ("%f", &n); printf ("%f", n);
    for newline type printf ("\n"); for tab printf ("\tab");
    ๐Ÿ˜‰
    when you use functions the value that you gave to function is being copied in parameters, so if you want make changes in the given variables you must use
    void f(int& k, int& s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    instead of
    void f(int k, int s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    for calculating 2^k fast use
    int s=1;
    s=s<for swaping values of variables use
    a=a^b;
    b=a^b;
    a=b^a;
    I think that's enough ๐Ÿ˜€
    sorry for weak english ๐Ÿ˜–
  • Vishal Sharma
    Vishal Sharma
    that scanf() and printf () are working fast than cin and cout
    you are right! ๐Ÿ˜€
    But, these are the tips for using c++. scanf and printf functions belong to C
    Hence, you cannot use them when you are asked to program in c++.

    Remaining things are all fine.. ๐Ÿ˜€
  • David Emperor
    David Emperor
    bro you can use scanf and printf in c++ ๐Ÿ˜‰
    you only need to include cstdio and it will work
    as well you can use fscanf and fprintf and every input/output comands that belong to C ๐Ÿ˜‰
  • Vishal Sharma
    Vishal Sharma
    cstdio is just a new name given to stdio.h
    Just because the C and C++ programs are performed in integrated environments, doesn't mean that you can flip flop the libraries.
    just a difference in name, doesn't create difference in functions. If you use printf and scanf() then there will be no much difference in C and C++. As C++ is advanced, we use cin and cout instead of scanf and printf as cin and cout allows us to utilize the special features like "cascading" which cannot be done using printf().

    Even the iomanip.h header functions like setw(), setprecision() which play an important role in clear presentation of output, can be easily operated using cascading of cin and cout.

    for more info, see this.
    #-Link-Snipped-#
  • malaka
    malaka
    Thanks for share given good knowledge.
  • sulochana anand
    sulochana anand
    vishal really u r having vishal knowledge.good
  • sulochana anand
    sulochana anand
    David Emperor
    Hey dude ๐Ÿ˜€ add for people
    that scanf() and printf () are working fast than cin and cout
    the view of scanf is scanf ("%/*const char * format*/", variable);
    formats are d- decimal integer , e,E,f,g,G - float types, u - unsigned int, h, H- hexidemical number, s - string, c-char , o - octal integer
    for example scanf ("%f", &n); printf ("%f", n);
    for newline type printf ("\n"); for tab printf ("\tab");
    ๐Ÿ˜‰
    when you use functions the value that you gave to function is being copied in parameters, so if you want make changes in the given variables you must use
    void f(int& k, int& s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    instead of
    void f(int k, int s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    for calculating 2^k fast use
    int s=1;
    s=s<for swaping values of variables use
    a=a^b;
    b=a^b;
    a=b^a;
    I think that's enough ๐Ÿ˜€
    sorry for weak english ๐Ÿ˜–
    thanx its working.till now i was having two methods of swaping but now three.
  • Vishal Sharma
    Vishal Sharma
    sulochana anand
    thanx its working.till now i was having two methods of swaping but now three.
    Thanks!!
    And It's just XOR method of swapping! I thought its easy so didn't share it! sorry! ๐Ÿ˜›
  • Vishal Sharma
    Vishal Sharma
    UPDATE:

    I've seen many people using cout or (printf in C) to print just one character.
    cout<< or printf is very expensive way of printing a character.
    the reason for that are:

    1> gets the format string
    2> parse and analyze the string
    3> find out that there's a CHAR format specifier
    4> call the char output sub function
    5> gets the character parameter
    6> output the character to the screen

    These are the operations performed by cout<< or printf() to print just a character. These things happen just in a fraction of seconds but as this thread is about "efficient" programming, I'll suggest you to STOP using cout<< or printf() to print one character.

    You have the facility of cout.put() or (putchar() in C, both are equivalent) to print one character. It just reads the character and prints it without following so many steps like cout<< .

    NOTE:: putchar() must be used only in C! Using it in C++ is not a good way to perform c++ programming. putchar() and cout.put() are equivalent but they must not be flip flopped
  • Shivani.bhupal
    Shivani.bhupal
    Vishal0203
    I'm a second year student, so, Just 3 months.. ๐Ÿ˜€
    SERIOUSLY.....udont seem like 2nd year student...plz give me tips on how you study
  • Vishal Sharma
    Vishal Sharma
    Shivani.bhupal
    SERIOUSLY.....udont seem like 2nd year student...plz give me tips on how you study
    Nothing special! I spend more time in playing ๐Ÿ˜›
    I just implement what teachers teach in college, in a different way! Like making software, combining all the topics n all! Which gives you a real time experience. I got these ideas while making a software. These tips are important for efficient programming.
    And another thing, pay attention in class. That's it! ๐Ÿ˜€
  • Shivani.bhupal
    Shivani.bhupal
    Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
  • Shivani.bhupal
    Shivani.bhupal
    Vishal0203
    Nothing special! I spend more time in playing ๐Ÿ˜›
    I just implement what teachers teach in college, in a different way! Like making software, combining all the topics n all! Which gives you a real time experience. I got these ideas while making a software. These tips are important for efficient programming.
    And another thing, pay attention in class. That's it! ๐Ÿ˜€
    Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
  • David Emperor
    David Emperor
    Like this ๐Ÿ˜€
  • grsalvi
    grsalvi
    Why line B is getting executed before line A in following cpp program ?
    ๐Ÿ˜•
    As after running the line A displays after you press EnteR.
    but getchar comes after line A.
    ????

    include
    #include
    #include

    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<[line A]
    cout<<"Press Enter to Exit";
    getchar(); [line B]

    exit(0);
    }
    }


    return 0;
    }
  • cranberrypie
    cranberrypie
    ohhh !!!๐Ÿ˜’
    i dont think the first three rules which you described is also known to our professors!!๐Ÿ˜
    anyways thanks for the info!! ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    Shivani.bhupal
    Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
    I mostly make educational software's for my juniors and sometimes if teachers ask me to make some software then i make it, like for library records, for sports person details (branch, attendance etc.) and the best thing, I don't give these for free ๐Ÿ˜ I charge them ๐Ÿ˜ ๐Ÿ˜‰
  • Vishal Sharma
    Vishal Sharma
    cranberrypie
    ohhh !!!๐Ÿ˜’
    i dont think the first three rules which you described is also known to our professors!!๐Ÿ˜
    anyways thanks for the info!! ๐Ÿ˜€
    yeah! Even my professors didn't know about it.. ๐Ÿ˜›
    These things are not taught in colleges. It's good to know that its helpful ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    Why line B is getting executed before line A in following cpp program ?
    ๐Ÿ˜•
    As after running the line A displays after you press EnteR.
    but getchar comes after line A.
    ????

    include
    #include
    #include

    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<[line A]
    cout<<"Press Enter to Exit";
    getchar(); [line B]

    exit(0);
    }
    }


    return 0;
    }
    I'll have a look at it soon! BTW i didn't understand your question, can you be more clear??????
  • simplycoder
    simplycoder
    grsalvi
    Why line B is getting executed before line A in following cpp program ?
    ๐Ÿ˜•
    As after running the line A displays after you press EnteR.
    but getchar comes after line A.
    ????

    include
    #include
    #include

    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<[line A]
    cout<<"Press Enter to Exit";
    getchar(); [line B]

    exit(0);
    }
    }


    return 0;
    }
    A query from my side, I think you are following this approach.
    1)Input a character.
    2)Press Enter.
    3)The result is shown in cmd prompt.
    Now according to you, line B is executing first.
    If this is your question, then the program is following its routine.
    Till and unless you convey your program that the input is finished from your side, it will not process any further.
    so Enter is a key reserved to break an input.
    (While entering an array, space or enter is treated as the next element).
    So after you press enter, you conveyed the program that you have finished with whatever you input you wanted,and now its free to process it.

    If you are still in doubt, hit a cout with some text before line B.
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    I totally agree with simplycoder. If your question is same as what simplycoder guessed, then you just need to add cin.ignore(); before getchar();
    As simplycoder said, getchar() reads the input enter key you pressed while entering the character. So, your program doesn't wait for pressing enter.
  • grsalvi
    grsalvi
    actually the problem is, on running the code :

    A character is asked .(user types character and presses enter)

    Then the ASCII value gets printed after you press 'Enter key'.

    This means line B is causing me to press enter and then line A is displaying ASCII value.
    Why an additional enter is required ?

    However i added 'endl' keyword now its working fine .Why?
    New properly working code :

    #include 
    #include
    #include
    
    
    
    int main()
    {
    
        char ch;
        cout<<"Enter a character: ";
        cin>>ch;
        
    
        for(int i=65;i<=122;i++)
        {
            if(ch==i)
            {
                cout<<"The ASCII value of "<
                                        
  • simplycoder
    simplycoder
    grsalvi
    actually the problem is, on running the code :

    A character is asked .(user types character and presses enter)

    Then the ASCII value gets printed after you press 'Enter key'.

    This means line B is causing me to press enter and then line A is displaying ASCII value.
    Why an additional enter is required ?

    However i added 'endl' keyword now its working fine .Why?
    New properly working code :

    #include 
    #include
    #include
     
     
     
    int main()
    {
     
        char ch;
        cout<<"Enter a character: ";
        cin>>ch;
     
     
        for(int i=65;i<=122;i++)
        {
            if(ch==i)
            {
                cout<<"The ASCII value of "<
    endl is nothing but END-LINE, that means the next thing that is supposed to be displayed would be on the next line of the console.
    Its not the endl, rather fflush(stdin) which does the trick.

    In the previous version, you didn't write anything that would interfere after the character was entered. whereas here, it is fflush(stdin).
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    actually the problem is, on running the code :

    A character is asked .(user types character and presses enter)

    Then the ASCII value gets printed after you press 'Enter key'.

    This means line B is causing me to press enter and then line A is displaying ASCII value.
    Why an additional enter is required ?

    However i added 'endl' keyword now its working fine .Why?
    New properly working code :

    #include 
    #include
    #include
     
     
     
    int main()
    {
     
        char ch;
        cout<<"Enter a character: ";
        cin>>ch;
       
     
        for(int i=65;i<=122;i++)
        {
            if(ch==i)
            {
                cout<<"The ASCII value of "<
    No no no... You're getting it wrong!!
    its not the getchar() being executed before displaying result.
    the enter key you are pressing after typing the character is to tell the compiler that the "cin statement is finished, go to next step" viz. same as scanf(); in C.
    getchar() is just to hold your output on screen it is nowhere related to the execution of program.
    anymore queries?? go ahead! ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    I mean getchar() is to get character from the keyboard. (in your program, it holds output on screen until you press a key)
  • grsalvi
    grsalvi
    simplycoder
    endl is nothing but END-LINE, that means the next thing that is supposed to be displayed would be on the next line of the console.
    Its not the endl, rather fflush(stdin) which does the trick.

    In the previous version, you didn't write anything that would interfere after the character was entered. whereas here, it is fflush(stdin).
    No dude earlier i had added only fflush() still the problem didn't get solved.Try the code your self .Its acting weird ๐Ÿ˜‰
  • grsalvi
    grsalvi
    Vishal0203
    I mean getchar() is to get character from the keyboard. (in your program, it holds output on screen until you press a key)
    case 1:

    #include
    #include
    #include



    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<
    cout<<"Press Enter to Exit";

    fflush(stdin);

    getchar();

    exit(0);
    }
    }


    return 0;
    }


    out put : (A is typed and Enter pressed )

    :Enter a character: A

    output : (one more enter presssed)

    Enter a character: A

    The ASCII value of Ais : 65Press Enter to ExitPress any key to continue[/code]
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    No dude earlier i had added only fflush() still the problem didn't get solved.Try the code your self .Its acting weird ๐Ÿ˜‰
    you're right! If you are using endl, you need not flush the stream, as endl itself is stream manipulator which first prints the character '\n' and then flushes the stream itself.
    If you are not using endl, you can use fflush() or to be more efficient, you can use cin.ignore()
  • grsalvi
    grsalvi
    case 2: endl added to line B

    #include
    #include
    #include



    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<
    cout<<"Press Enter to Exit"< [line B]

    fflush(stdin);

    getchar();

    exit(0);
    }
    }


    return 0;
    }


    output: (A typed & enter pressed)

    Enter a character: A
    The ASCII value of Ais : 65Press Enter to Exit

    Note: only one enter was sufficient to show answer .This means in case 1 : getchar() is some how making user to type one more enter before displaying answer .
  • grsalvi
    grsalvi
    Vishal0203
    you're right! If you are using endl, you need not flush the stream, as endl itself is stream manipulator which first prints the character '\n' and then flushes the stream itself.
    If you are not using endl, you can use fflush() or to be more efficient, you can use cin.ignore()
    Buddy even cin.ignore does not work.It is only 'endl' in line B which helps.Is this a bug in cpp?
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    Buddy even cin.ignore does not work.It is only 'endl' in line B which helps.Is this a bug in cpp?
    umm.. did you try tracing the program??
  • grsalvi
    grsalvi
    #-Link-Snipped-# #-Link-Snipped-# : see the two cases
  • simplycoder
    simplycoder
    grsalvi
    case 2: endl added to line B

    #include
    #include
    #include



    int main()
    {

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;


    for(int i=65;i<=122;i++)
    {
    if(ch==i)
    {
    cout<<"The ASCII value of "<
    cout<<"Press Enter to Exit"<[line B]

    fflush(stdin);

    getchar();

    exit(0);
    }
    }


    return 0;
    }


    output: (A typed & enter pressed)

    Enter a character: A
    The ASCII value of Ais : 65Press Enter to Exit

    Note: only one enter was sufficient to show answer .This means in case 1 : getchar() is some how making user to type one more enter before displaying answer .
    Oh, I added endl to line A.
    I agree with Vishal,
    cout<
    Just my 2 cents.
  • grsalvi
    grsalvi
    Vishal0203
    umm.. did you try tracing the program??
    Ya all instructions are executed sequentially.
    But output is displayed after you respond to getchar() ,by pressing enter .
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    #-Link-Snipped-# #-Link-Snipped-# : see the two cases
    I've seen the cases. Actually, i dont have any programming environment in my lappy now, so i'll suggest you to tracethe program once and see hoe it actually runs!
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    Ya all instructions are executed sequentially.
    But output is displayed after you respond to getchar() ,by pressing enter .
    oh crap!! i don't see any reason behind it..
  • grsalvi
    grsalvi
    Guys problem solved
    Ran the same code( just getchar(),no endl ,no fflush ) in turbo cpp .It worked normal๐Ÿ˜€.

    The problem is with visual c++.๐Ÿ˜ก
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    Guys problem solved
    Ran the same code( just getchar(),no endl ,no fflush ) in turbo cpp .It worked normal๐Ÿ˜€.

    The problem is with visual c++.๐Ÿ˜ก
    turbo cpp is waste!! It'll execute all unefficient things.. the true success is when it executes properly in visual C++ ๐Ÿ˜–
  • simplycoder
    simplycoder
    I agree with Vishal, if you dnt have Visual C++ (personally I dont suggest this as well). use DevC++.

    Turbo C++ will lead you to wrong usage of C++ as language aswell as standard library functions.
  • grsalvi
    grsalvi
    Vishal0203
    turbo cpp is waste!! It'll execute all unefficient things.. the true success is when it executes properly in visual C++ ๐Ÿ˜–
    But it visual c++ ,i told you what problem is happening.
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    But it visual c++ ,i told you what problem is happening.
    I use DEV C++
    I compiled the program and executed it too. I don't face any problems like you are facing. its just the same (only one press of enter key).
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Thanks pal the namespace tag can you please send some links to understand it more
  • Vishal Sharma
    Vishal Sharma
    jeffrey samuel
    Thanks pal the namespace tag can you please send some links to understand it more
    C++ is the introduction to the namespaces. If have a look on any higher programming language like C#, you'll see an abundant use of namespaces.

    there you go... this is the best documentation about the namespaces ๐Ÿ˜€

    #-Link-Snipped-#
  • bossbs
    bossbs
    first know about basic of c++ you can read the diploma book it will help you more
  • Jeffrey Arulraj
    Jeffrey Arulraj
    bossbs
    first know about basic of c++ you can read the diploma book it will help you more
    Thanks for the advice pal but I prefer knowing Data structures as they help me more in applying the basics
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Vishal0203
    C++ is the introduction to the namespaces. If have a look on any higher programming language like C#, you'll see an abundant use of namespaces.

    there you go... this is the best documentation about the namespaces ๐Ÿ˜€

    #-Link-Snipped-#
    thanks again for the link love it
  • Vishal Sharma
    Vishal Sharma
    jeffrey samuel
    Thanks for the advice pal but I prefer knowing Data structures as they help me more in applying the basics
    Data structures will surely help you learn new things, but you need to know the basics like templates, overloading etc. Since we don't like the compiler catching hundreds of errors in our program. ๐Ÿ˜‰
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Yeah true I learnt the basics real well but here and there I have some difficulties in understanding algorithms

    In case of OOPS I am confident of them a little cos of frequent helping of other class mates I got more acquainted with it
  • shanmugapriya kr
    shanmugapriya kr
    thanks a lot ............. this would really help me as i am in the initial stage of engineering๐Ÿ˜
  • Vishal Sharma
    Vishal Sharma
    shanmugapriya kr
    thanks a lot ............. this would really help me as i am in the initial stage of engineering๐Ÿ˜
    I hope it does!
  • shanmugapriya kr
    shanmugapriya kr
    thanks a lot vishal ๐Ÿ˜›
    Vishal0203
    I hope it does!
  • Vishal Sharma
    Vishal Sharma
    UPDATE::

    Use of getline() to get the input of a string or a line (common for both).

    the getline function present in plays an important role to get the input of a string, as well as a line. The getline function is common for both the situations, if you just know how to use it!

    We can take the input of a string through cin. But what about a line??
    cin takes a continuous string and as soon as it encounters an empty space, it terminates the string.

    But, by using getline() , a delimiter can be added! which means, we can tell the compiler when to terminate the string.

    SYNTAX

    getline(istream *, str, 'delim');

    istream * : it is an input stream pointer (cin).
    str : it is the declared string by the user.
    'delim' : it is the delimiter. represented in single quotes.

    Implementation

    Let me show you a small example!

    #include 
    #include 
    using namespace std;
    int main()
    {
      string a;
      cout<<"Enter the full name: "<In the above example, the delimiter is set as '\n' (you can even take it as '\r'). Hence, the input will be taken as long as you don't hit enter key.

    If I take Input as: Barak Obama
    then the output will be: Barak Obama

    now, if I change the delimiter as

    getline(cin , a , ' ');  /* delimiter set as \n */
    
    now we see that the delimiter is set to "space". Hence the input will be taken only up to the first space. That means, the string terminates when it encounters a space.

    If I take input as: Barak Obama
    then the output will be: Barak

    So, We see that everything is dependent on the delimiter! ๐Ÿ˜€
    doubts can be asked comments!
  • grsalvi
    grsalvi
    Vishal0203
    UPDATE::

    Use of getline() to get the input of a string or a line (common for both).

    the getline function present in plays an important role to get the input of a string, as well as a line. The getline function is common for both the situations, if you just know how to use it!

    We can take the input of a string through cin. But what about a line??
    cin takes a continuous string and as soon as it encounters an empty space, it terminates the string.

    But, by using getline() , a delimiter can be added! which means, we can tell the compiler when to terminate the string.

    SYNTAX

    getline(istream *, str, 'delim');

    istream * : it is an input stream pointer (cin).
    str : it is the declared string by the user.
    'delim' : it is the delimiter. represented in single quotes.

    Implementation

    Let me show you a small example!

    #include 
    #include 
    using namespace std;
    int main()
    {
      string a;
      cout<<"Enter the full name: "<In the above example, the delimiter is set as '\n' (you can even take it as '\r'). Hence, the input will be taken as long as you don't hit enter key.

    If I take Input as: Barak Obama
    then the output will be: Barak Obama

    now, if I change the delimiter as

    getline(cin , a , ' ');  /* delimiter set as \n */
    
    now we see that the delimiter is set to "space". Hence the input will be taken only up to the first space. That means, the string terminates when it encounters a space.

    If I take input as: Barak Obama
    then the output will be: Barak

    So, We see that everything is dependent on the delimiter! ๐Ÿ˜€
    doubts can be asked comments!
    The Function cin.getline() , does the same. Right ??
  • grsalvi
    grsalvi
    One Of the added feature of ANSI c++ is Standard template library.
    The containers like MAP are very useful.Hence i am using the STL Map.
    I wanted to know if we can store a map (with keys and values we have entered in it) to a file and also read back from it.
    I checked web but found nothing proper.
    If possible post code for both read and write.
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    The Function cin.getline() , does the same. Right ??
    there is a difference!
    cin.getline() belongs to whereas getline() belongs to
    to use cin.getline() you need to specify the size of string like we do in C programming (char a[10]).
    but, in getline you don't need to specify a size because it grows dynamically (declared as string a).
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    One Of the added feature of ANSI c++ is Standard template library.
    The containers like MAP are very useful.Hence i am using the STL Map.
    I wanted to know if we can store a map (with keys and values we have entered in it) to a file and also read back from it.
    I checked web but found nothing proper.
    If possible post code for both read and write.
    it is little advanced for me!! #-Link-Snipped-#
    might know the answer for this!
    I've tagged him here.. hope he helps you ๐Ÿ˜€
  • grsalvi
    grsalvi
    grsalvi
    The Function cin.getline() , does the same. Right ??
    okay so getline is used for string objects.
    I was wondering that the string objects also follow the rule of '\0' at end of characters as in character arrays ??
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    okay so getline is used for string objects.
    I was wondering that the string objects also follow the rule of '\0' at end of characters as in character arrays ??
    getline() takes a delimiter. As I've mentioned in the post. '\0' is inbuilt for this. As soon as the compiler encounters the delimiter it places a string terminator at the end.
  • grsalvi
    grsalvi
    Check out following code.

    Case 1:
    #include
    fstream f;
    f.open("d:\\test.tsv",ios::out|ios::in);
    
    For the above code.
    The program creates file test.tsv and gives no error for opening that is { f.fail() returns 0}.

    Case : 2

    #include
    using namespace std;
     
    fstream f;    f.open("d:\\test.tsv",ios::out|ios::in);
    
    In 2nd case also code is same but library has been changed to ANSI one.
    Now problem is ,on running program ,file "test.tsv" is not created and f.fail() returns true.



    Why its like that ? And how can i get functioning like that of case 1,using ANSI fstream library file.
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    which programming environment do you use??
  • grsalvi
    grsalvi
    Vishal0203
    #-Link-Snipped-#
    which programming environment do you use??
    IDE is visual c++ 6.0 .
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    IDE is visual c++ 6.0 .
    Okay!
    the first code snippet is inefficient and wrong. you cannot declare both ios::in and ios:: out together and it doesn't follow any ANSI standard. Since you are using Visual C++, all the codes following any standard of the turbo C or the DEV C or any other environment,works fine. But, If you try to execute the first snippet in DEV, you'll get an error as the .h formats are not for C++.

    Your second snippet is completely fine, but it doesn't produce the file because you've confused the compiler! The file is unavailable in the system at the start. So to create the file, you must either use ios::in or ios:: out (not together).

    still if you want to use both together, add the append mode too.

    f.open("d:\\test.tsv",ios::out|ios::app|ios::in);
    you can try seeing any code snippets across the web. You'll never see ios::in and ios:: out together

    Hope you understood! awaiting for your reply!
  • grsalvi
    grsalvi
    Vishal0203
    Okay!
    the first code snippet is inefficient and wrong. you cannot declare both ios::in and ios:: out together and it doesn't follow any ANSI standard. Since you are using Visual C++, all the codes following any standard of the turbo C or the DEV C or any other environment,works fine. But, If you try to execute the first snippet in DEV, you'll get an error as the .h formats are not for C++.

    Your second snippet is completely fine, but it doesn't produce the file because you've confused the compiler! The file is unavailable in the system at the start. So to create the file, you must either use ios::in or ios:: out (not together).

    still if you want to use both together, add the append mode too.

    f.open("d:\\test.tsv",ios::out|ios::app|ios::in);
    you can try seeing any code snippets across the web. You'll never see ios::in and ios:: out together

    Hope you understood! awaiting for your reply!
    Thanl you, i got the point.
    The use of ios::in and ios:: out is for the purpose that if you want a single file stream to be used for
    writing and reading a file.
    This common mode is used many times in text book of cpp but with file.
    Anyways issue resolved.
  • Vishal Sharma
    Vishal Sharma
    grsalvi
    Thanl you, i got the point.
    The use of ios::in and ios:: out is for the purpose that if you want a single file stream to be used for
    writing and reading a file.
    This common mode is used many times in text book of cpp but with file.
    Anyways issue resolved.
    when programming in C++, using .h format is of no use.. C++ is the next step of C then why not moving on to next step instead of following the ancestor ways.. That's the reason I preffer programming in DEV, as it doesn't allow such old methods to work! (shows an error even if you use conio.h header)

    I prefer these books.. These are very helpful..
    The Complete reference C++
    How to program C++ -> Dietel
  • safiajen0055
    safiajen0055
    #-Link-Snipped-#

    Hiiiiii,
    Thanks for sharing the tips For C++ Programmers. This post is very useful for me.
    Please keep sharing this type of useful information with us...
  • Vishal Sharma
    Vishal Sharma
    #-Link-Snipped-#
    Good to know that it was useful to you. There are other useful posts pinned in the discussion area. They may help you learn more! ๐Ÿ˜€
  • safiajen0055
    safiajen0055
    #-Link-Snipped-#

    Thanks for telling me this .
    I want to ask you one more thing can you help about the Java programming.
    Because i have so many problems with Java Programming.
    Please Reply me.. I am waiting for you Reply
  • fahad baig
    fahad baig
    this is very useful post for us.keep posting these kind of posts which helps us in programming ic various languages.
  • simplycoder
    simplycoder
    safiajen0055
    #-Link-Snipped-#

    Thanks for telling me this .
    I want to ask you one more thing can you help about the Java programming.
    Because i have so many problems with Java Programming.
    Please Reply me.. I am waiting for you Reply
    I think it would be better if you state your problem without waiting for a reply (preferably in a new thread) on this forum. In case if Vishal0203 is not available, someone else might help you out. Or there can be multiple solutions etc..etc
  • Vishal Sharma
    Vishal Sharma
    safiajen0055
    #-Link-Snipped-#

    Thanks for telling me this .
    I want to ask you one more thing can you help about the Java programming.
    Because i have so many problems with Java Programming.
    Please Reply me.. I am waiting for you Reply
    I think simply coder is right! I'm not familiar to java yet ๐Ÿ˜’ The subject is in next semester..
    So it would be better if you create a new thread so that you'll get your problems solved soon.
  • safiajen0055
    safiajen0055
    Vishal0203
    I think simply coder is right! I'm not familiar to java yet ๐Ÿ˜’ The subject is in next semester..
    So it would be better if you create a new thread so that you'll get your problems solved soon.

    Hiiiii ,

    Thanks for Advise me. I will create new thread related to Java Programming problems ..

    ๐Ÿ˜€ ๐Ÿ˜€
  • Chinu1
    Chinu1
    Hello... You give me best knowledge about system("cls");. Before reading your sharing, always i was use clrscr();.
  • Vishal Sharma
    Vishal Sharma
    Chinu1
    Hello... You give me best knowledge about system("cls");. Before reading your sharing, always i was use clrscr();.
    Yeah. I think almost everyone starts with that. But if you notice at the headers of clrscr(); and system("cls"); then, you see that clrscr(); belongs to conio.h (console input output) whereas, system("cls"); belongs to stdlib.h (standard library). Since, it is STANDARD, we use system("cls");
  • UGINIRAJ
    UGINIRAJ
    Vishal0203
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


    2) While programming for C++, instead of
    #include 


    use

    #include 
     
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    fflush (stdin);  // cstdio header
     
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }
    The same things can be done while using the loop conditions or conditional statements

    // make your code look clear...
     
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                    // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS
    this is very very useful foe us keep it going for better growth....
  • dhruba sunuwar
    dhruba sunuwar
    hey can you suggest me some c++ projects for high school!!!
  • Jeffrey Arulraj
    Jeffrey Arulraj
    dhruba sunuwar
    hey can you suggest me some c++ projects for high school!!!
    Check our project section Buddy. Don't misuse other threads here PLS
  • dhruba sunuwar
    dhruba sunuwar
    Conqueror
    Check our project section Buddy. Don't misuse other threads here PLS
    i am really sorry but i am new to this site.so, don't know much about it..but where is this "project section"???
  • Neeraj Sharma
    Neeraj Sharma
    dhruba sunuwar
    i am really sorry but i am new to this site.so, don't know much about it..but where is this "project section"???
    Here it is

    #-Link-Snipped-#

    Please explore the site as you will be able to find lot of god things here
  • Min1a1k1shi
    Min1a1k1shi
    Thanks a lot for sharing this awesome information ๐Ÿ˜‰
  • shibi raj
    shibi raj
    It's so nice..
    Thanks very much for posting this..
  • Vishal Sharma
    Vishal Sharma
    Many people ask suggestion for C++ projects.
    Its not me or someone else here, will give you the ideas. It has to be generated in your brain..
    If you come up with an idea, just follow the following steps to check whether you are making it right

    ==> BEFORE CODING
    only a few points to be remembered before typing the code.
    > Know about the needs of the client (generally a question for students).
    > Construct a basic idea of the concepts to be used to meet the needs of client.
    > Always program, keeping the idea about the future needs of the client. Predict the mind of client and plan up for further demands of client.
    > The needs of the client may change! So always add comments to your code for further references.

    ==> IMPORTANT

    Make sure you make the program user friendly. Program in such a way that the users who do not know anything about programming can also understand what they have to do.

    Your Program must always be ready to handle the exceptions to avoid application crash. There are users who enter some unknown or out range data.. at this situation application must,
    1> handle the exception
    2> Generate error message
    3> rollback
    For example, in a calculator if you enter a number 100000000000000000000000000000 the calculator may generate a message "out of range". Same Idea must be implemented.

    to know more about #-Link-Snipped-#
    Feel Free to ask doubts ๐Ÿ˜€
  • shravan kumar.TV
    shravan kumar.TV
    Its a valuable information and useful to beginners...Thanks for that information..
  • vikaskumar11233
    vikaskumar11233
    Hey, Every thing is clear to me, but I have a little doubt in void main() function. I learn that void returns nothing, then why do you say that it returns a garbage value. Please made this concept clear to me.
  • Vishal Sharma
    Vishal Sharma
    vikaskumar11233
    Hey, Every thing is clear to me, but I have a little doubt in void main() function. I learn that void returns nothing, then why do you say that it returns a garbage value. Please made this concept clear to me.
    Declaring main() as void doesn't mean that the program won't return anything. It means that the program may not return a fixed value (garbage value), i.e. it may not return 0 like in int main() (we return 0 or EXIT_SUCCESS at the end if we declare it as int).

    Every c/c++ program must return 0 to indicate its success. And, other reason is C/C++ programming standards. They say that int main() is the best way. (the reason i've explained you above)
  • Indo European
    Indo European
    The key to superior software engineering is to hub away from developing monolithic applications that do only one job, and focus on budding libraries. One way to think of libraries is as a program with many entry points. Every record you write becomes an inheritance that you can pass on to other developers. Just like in mathematics you expand little theorems and use the little theorems to hide the complexity in proving bigger theorems, in software engineering you develop libraries to take care of low-level details once and for all so that they are out of the way every time you make a deferent implementation for a variation of the problem.
  • safiajen0055
    safiajen0055
    Nice In
    Vishal0203
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


    2) While programming for C++, instead of
    #include 


    use

    #include 
     
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    fflush (stdin);  // cstdio header
     
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }
    The same things can be done while using the loop conditions or conditional statements

    // make your code look clear...
     
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                    // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS

    Nice Information ๐Ÿ˜€
    Thanks for sharing this Information With us ๐Ÿ˜€
  • Vishal Sharma
    Vishal Sharma
    safiajen0055
    Nice In


    Nice Information ๐Ÿ˜€
    Thanks for sharing this Information With us ๐Ÿ˜€
    and yeah, instead of fflush(stdin) before cin.get(), you can even use cin.sync()
    it avoids inclusion of another header file cstdio ๐Ÿ˜€
  • Ganesh MSD
    Ganesh MSD
    Vishal0203
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


    2) While programming for C++, instead of
    #include 


    use

    #include 
     
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    fflush (stdin);  // cstdio header
     
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }
    The same things can be done while using the loop conditions or conditional statements

    // make your code look clear...
     
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                    // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS
    Nice post.! It is so useful..!!
  • Min1a1k1shi
    Min1a1k1shi
    The tips herein do not only explain how to write better code, but rather, they present the rationale behind the new language rules. Obviously, there are many other perennial good tips that C++ programmers can benefit from. However, I'm sure that this collection will provide you with many insights and guidelines for professional, efficient, and bug-free C++while teaching you some new coding and design techniquesโ€‹
  • Badbade Abhi
    Badbade Abhi
    Vishal0203
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


    2) While programming for C++, instead of
    #include 


    use

    #include 
     
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    fflush (stdin);  // cstdio header
     
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }
    The same things can be done while using the loop conditions or conditional statements

    // make your code look clear...
     
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                    // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS
    Thank you Vishal sir.. In future further programming i will definitely follow your instruction...Hattsss of your knowledge sir...
    with regards,
    Abhi
  • Vishal Sharma
    Vishal Sharma
    Badbade Abhi
    Thank you Vishal sir.. In future further programming i will definitely follow your instruction...Hattsss of your knowledge sir...
    with regards,
    Abhi
    I hope you do! because most people say that they'll program in this way but no one actually does!
  • Sanket0731
    Sanket0731
    what is the meaning of the line 'using namespace std'?
    what are namespaces?
  • simplycoder
    simplycoder
    Sanket0731
    what is the meaning of the line 'using namespace std'?
    what are namespaces?

    Try to google up. Google is your best friend in most of the cases.
  • zeeshanaayan07
    zeeshanaayan07
    C++ is the mother of language without c++ we can service coding in all web developer software. Thanks for sharing a great post. I appreciate your thinking.
  • vikaskumar11233
    vikaskumar11233
    Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
  • Neeraj Sharma
    Neeraj Sharma
    vikaskumar11233
    Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
    Can you share a code snippet of where exactly you were facing the problem?
  • Vishal Sharma
    Vishal Sharma
    vikaskumar11233
    Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
    I'm unable to visualize need a snippet. If not the exact code post an example similar to it!
  • zeeshanaayan07
    zeeshanaayan07
    I think this the c++ coding
  • Neeraj Sharma
    Neeraj Sharma
    zeeshanaayan07
    I think this the c++ coding
    I know its C++ coding. I want him to post the part of the code he is facing problems with.
  • zeeshanaayan07
    zeeshanaayan07
    Learn C++ Coding because this is the basic thing which the base of all coding like html, css, php, csharp etc. if you want to learn #-Link-Snipped-# click there are the brilliant tutorial
  • Neeraj Sharma
    Neeraj Sharma
    zeeshanaayan07
    Learn C++ Coding because this is the basic thing which the base of all coding like html, css, php, csharp etc. if you want to learn #-Link-Snipped-# click there are the brilliant tutorial
    I guess you don't understand what I am asking him. I know C++ since a lot of years.
    I would recommend you to read my posts and understand it before giving your advice.
  • Indo European
    Indo European
    Suppose you declare something like this in your code:
    const int* ptr=new int;
    To who is constantans is bided? Pointer or Pointed object? The ans is pointed object. So the following is legal:
    int u;
    int v;
    const int* ptr=&u;
    ptr=&v;//OK, pointer can be changed
    But not the following:
    int u=3;
    const int* ptr=&u;
    *ptr=4;//error, pointer is declared to address const int
    As pointer is being declared to point to const object and not itself being const, it can be declared without being initialized:
    const int* ptr;//Ok, no initialization of ptr needed.
    Now, suppose you want to declare pointer itself to be constant, so that the pointer cannot change its value and hence, will point only one object during its existence. Most of us will declare following when asked to do this for the first time in our life:
    int const* ptr;

    But this means the same as above, ptr happens to address a constant object. To declare pointer itself to be constant, use the following segment:
    int* const ptr=&someint;

    Now you can't make ptr to point to something else, but you can change the value of the object being pointed by it. To declare a const pointer which points to an const object any of the two segment is ok:
    int x=2;
    const int* const ptr1=&x;
    int const* const ptr2=&x;

    Passing one or more argument to a function as const: When you pass an argument to a function as const, it doesn't mean that you can't pass non-const object as an argument to that object. Only thing you are telling the compiler is that this variable has to retain its value during the execution of that function. So, if by mistake you try to change its value inside the function code, compiler will flag error.
    void somefunc(const int num){
    //...
    ++num;//OOPs, by mistake, But compiler will show it as an error
    //saving your precious time from hunting for the possible bug here
    //..
    }
  • simplycoder
    simplycoder
    jimm
    hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
    please help me day after tommorow is my exam of c++.

    thank you in advance.
    I am not sure what you mean by concatenation using while loop?
    a loop would be used only for iterative process.

    Anywas, show your work, then we would be able to help you.
  • Vishal Sharma
    Vishal Sharma
    jimm
    hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
    please help me day after tommorow is my exam of c++.

    thank you in advance.

    If you show your approach we'll try to correct you.
    Direct codes are not given!
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Well just use the logic till word end that is Null character the first word has to be stored in the array

    When null is encountered using if clause call the second word and do the rest of the assignment you are done with your programming

    About x,y Not sure what you are asking here mate
    jimm
    hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
    please help me day after tommorow is my exam of c++.

    thank you in advance.
  • Vishal Sharma
    Vishal Sharma
    A Better way to code In C++

    As, C++ gives you feature of class, you can use it to make separate modules or headers and just LINK it to your program. This reduces the size of code and each module becomes self explanatory.

    You just need to invoke the object of the class you have in your header in to your main file. Remember, that's how cin and cout work. They are objects not functions like printf() and scanf().

    I'm not giving any code here, just an example
    suppose you have to conduct an exam, and you have to write a code for the interface, then the questions are generated in separate module, solutions are checked in a separate module and your marks are evaluated in separate module. Your interface just uses the objects of those modules to do its task.
    Then, instead of writing a long code, you can do something like this..
    #include "iostream"
    #include "questions.h"
    #include "solutions.h"
    #include "marks.h"
    using namespace std;
    int main() {
      ...............
      ...............
    }
    your questions are generated in questions.h
    solution is checked in solution.h
    marks are evaluated in marks.h
    Finally your result is displayed through your main.cpp file
  • sneha thorat
    sneha thorat
    i agree with vishal printf and scanf is used in the c.if there is program in c++ cin and cout is used instead of printf and scanf.
  • simplycoder
    simplycoder
    sneha thorat
    i agree with vishal printf and scanf is used in the c.if there is program in c++ cin and cout is used instead of printf and scanf.

    cin and cout are preferred in C++ program.
    However, scanf and printf would work faster than cin and cout.

    Same way a C program can also be programmed in the way #-Link-Snipped-# has written.

    Am I missing anything here?
  • Vishal Sharma
    Vishal Sharma
    simplycoder
    cin and cout are preferred in C++ program.
    However, scanf and printf would work faster than cin and cout.

    Same way a C program can also be programmed in the way #-Link-Snipped-# has written.

    Am I missing anything here?

    Yeah, printf and scanf provide fast access.. can't deny that! But standards and cascading! ๐Ÿ˜€

    and yeah, even a C program can be made that way, I didn't say that it cant be done in C. I'm just illustrating the general concept used in C++
    Almost all the inbuilt header files (after c++) have classes and require object to use its functionality.. So, my post (i think) makes it easier to understand if we just try to compare the already existing things to this post!
  • sneha thorat
    sneha thorat
    k.i got it.........
  • sneha thorat
    sneha thorat
    simplycoder
    cin and cout are preferred in C++ program.
    However, scanf and printf would work faster than cin and cout.

    Same way a C program can also be programmed in the way #-Link-Snipped-# has written.

    Am I missing anything here?
    you are right........m just saying that on the basis of c++ program.
  • Vishal Sharma
    Vishal Sharma
    sneha thorat
    you are right........m just saying that on the basis of c++ program.
    Yeah! actually it doesn't make much difference...
    cout

    this is for cout!

    pf
    This is for printf()
  • sneha thorat
    sneha thorat
    Vishal0203
    Yeah! actually it doesn't make much difference...
    cout

    this is for cout!

    pf
    This is for printf()
    yaah i understood.thanks for the extra knowledge.
  • Vishal Sharma
    Vishal Sharma
    UPDATE

    Now This Post goes for C programming (can be used in c++ too but i prefer getline() for this in c++ as it matches the standard)

    While taking the input of a string, many of us use scanf("%s",a); etc.
    this way is correct but it cannot handle the exception of exceeding the character array size!

    As we all are programmers, we have an intention that the user must not face any kind of problems with the software and its obvious that user is un-aware of the size of string we have prescribed!
    Lets take an example here....

    #include
    int main() {
      char a[10];
      scanf("%s",a);
      printf("%s",a);
      return 0;
    }
    the size of array is 10 but users are unaware of it and they may enter a string passing beyond the size like in this case!

    b

    Hence there is a problem of memory out source and ultimately, the application collapse showing this error report!

    a

    and as we all know we never want our application to crash, so we can take some necessary measures to avoid this problem

    so, if i replace my above code with the following code, it handles this exception and it ignores whatever is entered beyond the array size.. the two functions responsible for this are fgets() and fputs()

    #include
    int main() {
       char a[10];
       fgets(a,sizeof(a),stdin);
       fputs(a,stdout);
       return 0;
    }
    
    The output will be something like this, showing only the first 10 digits entered! In this way this problem is handled and the application doesn't crash!

  • anishrushi
    anishrushi
    For more questions and answers on c++ hit upon CrazyEngineers
  • rahulkhan
    rahulkhan
    Nothing in this link ( c++ ) #-Link-Snipped-# CrazyEngineers #-Link-Snipped-#
  • vaibhav bhanawat
    vaibhav bhanawat
    hello friends what should i do to convert my c++ code into an software which can be installed into different computer...........i am unable to open the exe file.
    ..
  • Vishal Sharma
    Vishal Sharma
    vaibhav bhanawat
    hello friends what should i do to convert my c++ code into an software which can be installed into different computer...........i am unable to open the exe file.
    ..
    I think its the system architecture which is not allowing you to run the exe on other PC's. Make sure that the architecture of your system and the other system are same. If you don't want this kind of trouble, create two setups (separate for both x86 and x64). I was facing the same problem with my app. I had to follow this step.
  • kiran devaguptapu
    kiran devaguptapu
    thanks......very useful.
  • rahul69
    rahul69
    One good practice is to avoid hard-coding the array size(in case static array is necessary), for example:
    instead of creating :
    int array [50];
    better way is :
    #define max 50
    .
    .
    int array [max];
    
    so if we need to increase array size, in complex program, we can be sure no array is left, otherwise it will be a havoc.
  • sneha thorat
    sneha thorat
    thanks fr d useful post........
  • Kiran.laxman.hiwane
    Kiran.laxman.hiwane
    how to initialise and declare a funtion inside and outside the class
  • oumar
    oumar
    thanks guys for your contributions
  • Sirajul islam Shawon
    Sirajul islam Shawon
    Vishal0203
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();



    2) While programming for C++, instead of
    #include 


    use

    #include 
    
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    fflush (stdin);  // cstdio header
    
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }

    The same things can be done while using the loop conditions or conditional statements

    // make your code look clear...
    
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                     // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS
  • Md shahin hawlada
    Md shahin hawlada
    Really useful that topic a lot's of thanks
  • Gnanam Ramalingam
    Gnanam Ramalingam
    hey dude its really useful keep updating
  • ankush0809
    ankush0809
    is it necessary to learn c++ or html for PHP or if i want to learn wordpress only then, can i learn direct wordpress ?
  • Vishal Sharma
    Vishal Sharma
    ankush0809
    is it necessary to learn c++ or html for PHP or if i want to learn wordpress only then, can i learn direct wordpress ?
    You eat the entire cake, not just the icing ๐Ÿ˜‰
  • MANU BHARADWAJ
    MANU BHARADWAJ
    I am currently using turbo c++ compiler should I change to any other ?
  • Vishal Sharma
    Vishal Sharma
    manu bharadwaj
    I am currently using turbo c++ compiler should I change to any other ?
    Yeah, use dev c++ if yu are a starter or else use visual studio.
  • MANU BHARADWAJ
    MANU BHARADWAJ
    Vishal0203
    Yeah, use dev c++ if yu are a starter or else use visual studio.
    Thank u vishal where can I download dev c++
  • Vishal Sharma
    Vishal Sharma
    manu bharadwaj
    Thank u vishal where can I download dev c++
    Google it buddy.
  • Edward Kagimu
    Edward Kagimu
    dude pliz would u hel[p and elaborate to me how to doiwnload and install the gcc compilers on window seven operating sytem?
  • Vishal Sharma
    Vishal Sharma
    kagimu Edward
    dude pliz would u hel[p and elaborate to me how to doiwnload and install the gcc compilers on window seven operating sytem?
    You can download Dev C++, gcc compilers are packaged with it. Just add the bin path in your environment variables and you are good to go
  • Vishal Sharma
    Vishal Sharma
    Redfredg
    This is very indepth and informative tips. I have been learning C++ for 5 months and I do not know if I understand if I know the language like, I am suppose to. How do you know if you are okay with C++? I am right now learning polymorphism and I Semi understand it right now.
    The way I understood C++ and oops concepts was by writing all my data structure code in C++. Also, by comparing the methods of C with C++.
  • Vishal Sharma
    Vishal Sharma
    Betty Maria
    Good tips but why did you use printf(); in C++?
    printf ("HELLO CEANS");

    I think you should use:
    cout << "HELLO CEANS";
    Agreed. I mentioned that somewhere in this long thread, as I was not able to update the post.

You are reading an archived discussion.

Related Posts

i have a great doubt regarding my project"BCI based wheelchair control using eeg", what can i do to get the eeg signals in the place where i work. is it...
"Tharang12" is a techno Cultural Fest organized by Jyothi Engineering College, Thrissur, Kerala on 10th -12th October, 2012. The might and strength of seven core engineering branches of this college,...
Like every year, IIT Bombay will be organizing Techfest 2013 between 3rd to 5th January, which is just a few months away from now. With a super-looking website already up,...
name: l. neerajakshi branch: electronics and communications college : ssit, A.P. hobbies: watching t.v., reading books..........etc aim: want to become electronic engineer.. i had joined the group to improve my...
whats the basic difference between hub and switch?