0.7 — A few common C++ problems

In this section, we’ll address some of the common issues that new programmers seem to run across with fairly high probability. This is not meant to be a comprehensive list of compilation or execution problems, but rather a pragmatic list of solutions to very basic issues. If you have any suggestions for other issues that might be added to this list, post them in the comments section below.

Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately.

Answer 1: Some compilers (eg. Bloodshed’s Dev C++) don’t automatically pause the console screen after the program has finished executing. If this is the case with your compiler, the following two steps will fix your problem:

First, add the following line near the top of your program:

#include <iostream>

Second, add the following code at the end of the main() function (right before the return statement):

cin.ignore(255, '\n');

This will cause your program to wait for you to press a key before continuing, which will give you time to examine your program’s output before your compiler closes the console window.

Other solutions, such as the commonly suggested system(“pause”) solution may only work on certain operating systems.

Problem 2: When compiling with Microsoft Visual C++, you get the following error: “c:\vcprojects\test.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive”

Answer 2: This error occurs when the Microsoft Visual C++ compiler is set to use precompiled headers but one of your C++ code files does not include the stdafx header as the first line. To fix this problem, simply locate the file producing the error (in the above error, test.cpp is the culprit), and add the following line at the very top of the file:

#include "stdafx.h"

Alternatively, you can turn off precompiled headers.

Problem 3: When trying to use cin or cout, the compiler says cin or cout is an “undeclared identifier”

Answer 3: First, make sure you have included the following line near the top of your file:

#include <iostream>

In any function where you use use cin or cout, include the following line:

using namespace std;

Problem 4: When trying to use endl to end a printed line, the compiler says end1 is an “undeclared identifier”

Make sure you do not mistake the letter l (lower case L) in endl for the number 1. endl is all letters. I recommend using a font that makes it clear the differences between the letter lower case L, upper case i, and the number 1. Also the letter capital o and the number zero can easily be confused in many fonts. There is a huge list of programming fonts here.

Problem 4: My program compiles but it isn’t working correctly. What do I do?

Answer 4: Debug it! You can find information on how to debug programs in appendix A, specifically sections A.4 and A.5. You will probably find the debugging sections more comprehensible after reading a few more chapters, but I wanted to make you aware of their existence now for future reference.

1.1 — Structure of a program
0.6 — Compiling your first program

112 comments to 0.7 — A few common C++ problems

  • mouna

    hello all!:)

    i have a problem!:(

    the command line don’t work!! this is the error-message.

    ‘g + +’ is not recognized as internal command
    or external, operable program or batch file.

    thnak u for ur help!

  • kemawalker

    Thank you for this sight….I just downloaded the latest XCODE from the Apple Dev site and your examples all actually WORK!! I am tickled that someone finally has something for the mac beginners to learn.

  • eric

    : fatal error C1083: Cannot open precompiled header file: ‘Debugvalue of coins in cents.pch’: No such file or directory
    …….please help…….

  • Dust

    Hello I have a problom,

    When I add the line to make the compiler pause. well every time I attemte to build it it it fails.

    I don’t realy know what the problom is I hope you can help.

  • Good-Luck

    Please help me with this problem.



    using namespace std;

    #define SHM_KEY 9876

    main ()
    int shmid;

    char* ptr;

    ptr = (char*) shmat (shmid, 0,0);
    int X=0;

    string input1;
    getline (cin, input1);

    int length = input1.length();

    while (X<=length)

    *ptr = input1 [X];
    X ++;



  • priyanka.01

    can i do program in turbo c++.

  • viccos

    First I just wanted to say thank you so much for this site. You have really created something special here. As a completely self-taught IT professional I owe my living to people like you and this ranks amongst the best tutorials I have come across in any field.

    With that said I thought you might like to know about a pit-fall I… er… fell into.

    Having successfully run my program once I got excited and tried a few variations. I then found that my program had started blinking up for a fraction of a second and vanishing. I tried all of the things you suggested and then hit the web.

    Half an hour later I realised I was now pressing F5 instead of Control + F5 to run my program. Stupid error on my part but might be worth mentioning in the tutorial with an explanation of why you use Control+F5 rather than just F5 :)

  • zac123

    couldnt get this to stay open. using c++ 2010 express.

    after reading through the comments herei got it to work with this:

    // HelloWorld.cpp : Defines the entry point for the console application.
    #include “stdafx.h”
    using namespace std;
    int main()

    std::cout << "Hello world!" << std::endl;

    thanks for this brilliant web site. ive just started learning c++ sp this will be very useful!

  • Nice one mate…really love what you are doing. Keep me refresh what I have forgotten.

  • mezz

    Would you say this tutorial would be improved if it was explicit in naming, e.g., instead of using using namespace std; using the “std::” reference? I’m just wondering because I’m being told it is OK, but better to future-proof and avoid any inherited name collisions.

  • walsby

    when using codeblocks i get this message when i try to build

    “HelloWorld1 – Debug” uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping…
    Nothing to be done.

    ive tried changing the compiler but theres so many i dont know which to choose.

  • srk

    you can also use the line

    system (“pause”);

    at the end of your code keep the console open.

  • Dragon

    Here’s another issue I have with compiling. A warning always says:
    warning C4244: ‘argument’ : conversion from ‘time_t’ to ‘unsigned int’, possible loss of data
    And then debugging:
    Unhandled exception at 0x104a942c in Take 2.exe: 0xC0000005: Access violation reading location 0x00432ca0.

  • irritated.soul

    Hi. I’m experiencing some issues debugging after testing the examples explained so far in this tutorial. The issue will arise when the output results are sent to the console: the console screen very quickly exits after the program is run, leaving very little chance for me to view the output. Is there another statement which can be added into the code at the end – to enable the console to stay paused for a short but relative period of time?

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • [...] issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. Conclusion Congratulations, you made it through the hardest part of this tutorial (installing the [...]

  • Vignesh

    for all those using dev c++ , this code will work and pause you screen until you enter a character

    using namespace std;

    int main()
    cout<<"hello world!!";
    return 0;

    the getch(); is defined in conio.h and takes a character from input without printing to screen. you can see the output until you press a key.

    just saying even system("pause") will work except that it prints "Press any key to continue" .

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • DidDo0

    you can use and system(“pause”); before return 0;

    Like this ..

    int main(){
    cout<<"hello :P "<<endl;

    return 0;

  • activatedgeek

    hello…i am having a problem exactly same as Problem 1. My .exe file just flashes. I even put the code given to correct that error but the problem remains.

    If I compile and run from the Code::Blocks application (which i am using to compile), the things run fine without any error.
    But if i go to the explorer and run this .exe file from the directory, it is still flashing.

    Please help!!

    Thank you, its been great here from a few past days..!

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • rainandmoonlight

    I studied all of this, but I still have one problem left. When I ask the IDE to build, it singles out the last bracket on the last line and tells me that “Intellisense expected an expression”? Please help

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • Hi. I am new to CPP. I used to have this problem of the output flashing and disappearing, but a friend told me to use clrscr() after declaring the variables and getch(); just before the closing braces of the program. Now, the output stays put until I press enter. I just want to know what exactly the getch() function does…
    I’d love it if you explained. :-)

    • TonyArra

      For anyone else interested in this question:

      clrscr() and getch() are part of the conio.h library (non-standard library that won’t come with most compilers and is not available on Linux).

      clrscr() just clears the console screen.

      getch() is a variant of the standard getchar() function. getch() waits for the user to press any character on the keyboard. The function returns the character, but doesn’t print the character to the screen like getchar() does. It also does not require the user to press the Enter key in order to submit the character (this is convenient because you no longer need to flush the standard input stream).

      On Linux, you can get similar functionality by installing the curse.h library.

  • mohnish01

    I am using visual c++ 2010 and when I type the following code I get this error –

    1>—— Build started: Project: HelloWorld.cpp, Configuration: Debug Win32 ——
    1> HelloWorld.cpp.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    my code is -

    // HelloWorld.cpp.cpp : Defines the entry point for the console application.

    #include “stdafx.h”


    int main()
    { std::cout << "Hello world!" << std::endl;
    return 0;

    Can someone tell me what am I doing wrong?…thanks

  • [...] This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in lesson 0.7 — a few common cpp problems. [...]

  • Nikaido

    I had problems with that 255 message and for some reason even the cin.clear(); code wasn’t helping in Code:Blocks 13.12. I decided to run it through a Linux Terminal and it worked perfectly.

    For those of you who are using Linux:
    1. Open terminal
    2. cd to the folder where the executable is, for example: “cd /home/—/workspace/HelloWorld/bin/Debug”
    3. run the executable: “./HelloWorld”
    4. You should have one line that only says “Hello world!” and then goes back to the command line.

    *Note: I would recommend keeping a terminal window open on your secondary desktop while following this tutorial on your first. That’s what I’m doing.

  • Nexus

    As an alternative (and sometimes better) solution for Problem 3:

    std::cout << "Hello World" << std::endl;

    This is good to use when you are using multiple namespaces (I know a bit of C++, I am just starting from the beginning to make sure I get everything), but for now it may be good just to type:

    using namespace std;

    As they suggest. I just wanted to put that out there so that people know about it before they get too solidified with the idea of using the already suggested solution.

    You can view a short video by about namespaces here:
    It is about 3 minutes long.

  • Captain Obvlious

    Recommending the use of using namespace std; is itself a problem, especially if you are doing at global scope and definitely if you do it in a header file.

You must be logged in to post a comment.