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.clear(); cin.ignore(255, '\\n'); cin.get();
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.
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
|
Index
|
0.6 — Compiling your first program
|
1.1 — Structure of a program
Index
0.6 — Compiling your first program
Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately.
The ide I’m using is Visual C .NET 2002. This ide has problem 1.
To fix the problem I include the following command at the end of the main() function before the return statement.
system(”pause”);
[ Unfortunately, this only works on the Windows operating system. The system() function tells the operating system to execute whatever the parameter is -- in this case, a command named "pause". On Windows, this waits for user input. On other operating systems (such as linux), the pause command does something else entirely. However, if you are only programming on Windows and not distributing your source code, you may find this an easier solution while learning. -Alex ]
Another great way to get a program to pause is to require a user input (cin). Using a menu-type interface that requires the user to select an option for “exiting” works well. I’m only a beginner, but for simple calculators that I’ve created, this works well.
In order to get the screen to pause before exiting the program, does the code:
have any advantages over just using
I have so much to learn. :)
Jason, good question. Here’s a simple program that illustrates why
cin >> myVariableis not the best way to pause:First,
cin >> chIgnoreignores the enter key if the user does not provide any other input. You can verify this by hitting enter when thecin >> chIgnoreline is being executed. On the other hand, cin.get() accepts the enter key as valid input.But perhaps more importantly is the second phenomena that occurs when more characters are placed into the input stream (cin) than are read out. When asked for a letter in the above program, enter more than one letter and watch what happens.
Let’s say you enter “abcde”.
cin >> buf[0]reads the ‘a’ character, but “bcde” are still left in the input stream. Consequently, when the code gets tocin >> chIgnore, it reads the waiting ‘b’ character out of the input stream and doesn’t pause at all!The 3 step process is a little more foolproof. First,
cin.clear()clears any errors the user may have caused by providing invalid input in the past. Second,cin.ignore(255, '\n')gets rid of up to 255 waiting characters in the input stream (or until a \n is encountered), which is almost always going to be enough to clear out the input stream. Finally, we usecin.get()to wait for a character. Since we just cleared the input stream, there are no characters waiting. Consequently, the program will wait for the user to enter something.In 99% of the cases you encounter,
cin >> myVariablewill probably work exactly as you expect. But since the 3 line method is generally more foolproof and isn’t particularly burdensome to implement, I’d say it’s the superior solution.That helps a lot.
Very cool.
I made a comment here about endl, with a different name. Tiomon.. the infamous typo..
I kept getting an error because instead of endl (lowercase L) I put end1 (ONE). I think you should put this up there since even though it’s humiliating I took a “little” while trying to fix it (thanks to Google..) someone else might encounter the same thing I did.
[ That's a great point. I'll add it to the FAQ. I added some additional advice in the answer to this issue. -Alex ]
[...] 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. [...]
If you’re using Dev-C++, you can put on the end of main function (before return 0;) system(”PAUSE”); to don’t close console window immediately. This command pause the program and wait to keypress. But I think that it works only in MS Windows.