Tips For C++ Programmers - Read & Learn

Vishal Sharma
@vishal-pysGmK โ€ข Oct 27, 2024

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 <iostream.h>



use

#include <iostream>
 
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Ankita Katdare

    @abrakadabra Aug 28, 2012

    #-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-pysGmK Aug 28, 2012

    #-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-e8RF2B Aug 28, 2012

    Thank You. That was really useful.๐Ÿ˜€

  • Vishal Sharma

    @vishal-pysGmK Aug 28, 2012

    #-Link-Snipped-#
    good to know that! ๐Ÿ˜€

  • monujatt

    @monujatt-1M9EHi Aug 29, 2012

    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-pysGmK Aug 30, 2012

    #-Link-Snipped-#
    Yeah! I've seen such people.. ๐Ÿ˜จ

  • sulochana anand

    @sulochana-anand-OrGmKr Aug 31, 2012

    good knowledge given by u.thanx

  • Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    #-Link-Snipped-#
    thanks .. ๐Ÿ˜€

  • gaurav91

    @gaurav91-VU6IN3 Sep 1, 2012

    i too had worked with c++!! but i didn't knew most of it ..true knowledge ..

  • Vishal Sharma

    @vishal-pysGmK Sep 1, 2012

    #-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-pysGmK Sep 1, 2012

    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-VU6IN3 Sep 1, 2012

    hey since when r u working in c++?

  • Vishal Sharma

    @vishal-pysGmK Sep 1, 2012

    gaurav91hey since when r u working in c++?

    I'm a second year student, so, Just 3 months.. ๐Ÿ˜€

  • gaurav91

    @gaurav91-VU6IN3 Sep 1, 2012

    wow awesome buddy! so where r u studying c++ from i mean which books do u refer?

  • Vishal Sharma

    @vishal-pysGmK Sep 1, 2012

    gaurav91wow 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-VU6IN3 Sep 1, 2012

    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-pysGmK Sep 1, 2012

    ๐Ÿ˜€ 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-i8PcCa Sep 2, 2012

    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<<k;

    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-pysGmK Sep 2, 2012

    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-i8PcCa Sep 2, 2012

    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-pysGmK Sep 2, 2012

    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-wboxAY Sep 6, 2012

    Thanks for share given good knowledge.

  • sulochana anand

    @sulochana-anand-OrGmKr Sep 6, 2012

    vishal really u r having vishal knowledge.good

  • sulochana anand

    @sulochana-anand-OrGmKr Sep 6, 2012

    David EmperorHey 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<<k;
    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-pysGmK Sep 6, 2012

    sulochana anandthanx 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-pysGmK Sep 8, 2012

    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

    @shivanibhupal-7hs6rH Sep 22, 2012

    Vishal0203I'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-pysGmK Sep 22, 2012

    Shivani.bhupalSERIOUSLY.....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

    @shivanibhupal-7hs6rH Sep 24, 2012

    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

    @shivanibhupal-7hs6rH Sep 24, 2012

    Vishal0203Nothing 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-i8PcCa Sep 24, 2012

    Like this ๐Ÿ˜€

  • grsalvi

    @grsalvi-7IhIh1 Sep 28, 2012

    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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>

    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 "<<ch<<"is : "<<i; [line A]
    cout<<"Press Enter to Exit";
    getchar(); [line B]

    exit(0);
    }
    }


    return 0;
    }

  • cranberrypie

    @cranberrypie-6KrThs Sep 28, 2012

    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-pysGmK Oct 2, 2012

    Shivani.bhupalMaking 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-pysGmK Oct 2, 2012

    cranberrypieohhh !!!๐Ÿ˜’
    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-pysGmK Oct 2, 2012

    grsalviWhy 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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>

    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 "<<ch<<"is : "<<i; [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-NsBEdD Oct 2, 2012

    grsalviWhy 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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>

    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 "<<ch<<"is : "<<i; [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-pysGmK Oct 2, 2012

    #-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-7IhIh1 Oct 2, 2012

    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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    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 "<<ch<<"is : "<<i<<endl;
                
                cout<<"Press Enter to Exit"<<endl;
                
                fflush(stdin);
    
                getchar();
            
                exit(0);
            }
        }
    
    
        return 0;
    }
    [code\n]
  • simplycoder

    @simplycoder-NsBEdD Oct 2, 2012

    grsalviactually 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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>
     
     
     
    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 "<<ch<<"is : "<<i<<endl;
             
                cout<<"Press Enter to Exit"<<endl;
             
                fflush(stdin);
     
                getchar();
         
                exit(0);
            }
        }
     
     
        return 0;
    }
    [code\n]

    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-pysGmK Oct 2, 2012

    grsalviactually 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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>
     
     
     
    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 "<<ch<<"is : "<<i<<endl;
               
                cout<<"Press Enter to Exit"<<endl;
               
                fflush(stdin);
     
                getchar();
           
                exit(0);
            }
        }
     
     
        return 0;
    }
    [code\n]

    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-pysGmK Oct 2, 2012

    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-7IhIh1 Oct 2, 2012

    simplycoderendl 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-7IhIh1 Oct 2, 2012

    Vishal0203I 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 <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>



    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 "<<ch<<"is : "<<i;

    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-pysGmK Oct 2, 2012

    grsalviNo 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-7IhIh1 Oct 2, 2012

    case 2: endl added to line B

    #include <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>



    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 "<<ch<<"is : "<<i;

    cout<<"Press Enter to Exit"<<endl; [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-7IhIh1 Oct 2, 2012

    Vishal0203you'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-pysGmK Oct 2, 2012

    grsalviBuddy 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-7IhIh1 Oct 2, 2012

    #-Link-Snipped-# #-Link-Snipped-# : see the two cases

  • simplycoder

    @simplycoder-NsBEdD Oct 2, 2012

    grsalvicase 2: endl added to line B

    #include <iostream.h>
    #include<stdio.h>
    #include<stdlib.h>



    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 "<<ch<<"is : "<<i;

    cout<<"Press Enter to Exit"<<endl; [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<<endl is equivalent to std::cout<<"\n"<<std::flush;

    Just my 2 cents.