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:
1
|
#include <iostream>
|
Second, add the following code at the end of the main() function (right before the return statement):
1
2
3
|
std::cin.clear(); // reset any error flags
std::cin.ignore(32767, ‘\n’); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user
|
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 and should be avoided.
Note: Visual Studio will not pause at the end of a console application if it is run with debugging (Debug Menu->Start Debugging). If you want it to pause, you can either use the code solution above, or run your program without debugging (Debug Menu->Start Without Debugging).
Problem 2: When compiling with Microsoft Visual C++, you get the following error: “c:vcprojectstest.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 (or more) of your C++ code files does not include the stdafx header as the first line. To fix this problem, simply locate the file(s) producing the error (in the above error, test.cpp is the culprit), and add the following line at the very top of the file(s):
1
|
#include “stdafx.h”
|
Note that for programs with multiple files, every C++ code file needs to start with this line.
Alternatively, you can turn off precompiled headers.
Problem 3: When trying to use cin, cout, or endl, the compiler says cin, cout, or endl is an “undeclared identifier”
Answer 3: First, make sure you have included the following line near the top of your file:
1
|
#include <iostream>
|
Second, make sure cin, cout, and endl are prefixed by “std::”. For example:
1
|
std::cout << “Hello world!” << std::endl;
|
Problem 4: When trying to use endl to end a printed line, the compiler says end1 is an “undeclared identifier”
Answer 4: 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 non-programming fonts.
|
Problem 5: My program compiles but it isn’t working correctly. What do I do?
Answer 5: Debug it! You can find information on how to debug programs in lesson 1, specifically sections 1.11 — Debugging your program (stepping and breakpoints) and 1.11a — Debugging your program (watching variables and the call stack).
|
Problem 7: When I compile my program in Visual Studio 2010, I get an error message about a COFF file being invalid. How do I fix this?
Answer 7: If you see the following error when compiling with Visual Studio 2010:
LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
You’ve encountered a Microsoft OS/compiler incompatibility. It has nothing to do with your code.
The best first option is to download and install Visual Studio 2010 Service Pack 1.
If that does not fix your issue, there are many other good suggestions on this Stack Overflow thread about the various causes and solutions to this problem.
|
Problem 8: When I compile my program, I get an error about unresolved external symbol _main or _WinMain@16
Answer 8: This means your compiler can’t find your main() function. All programs must include a main() function.
There are a few things to check:
a) Does your code include a function named main? b) Is main spelled correctly? c) Is the file containing main part of your project? (if not, either move the main function to one that is, or add the file to your project. See lesson 1.8 — Programs with multiple files for more information about how to do this). d) Is the file containing function main set to compile? (Also see lesson 1.8 — Programs with multiple files for more information about how to do this). |
I have some other problem that I can’t figure out. How can I get an answer quickly?
Answer: As you progress through the material, you’ll undoubtedly have questions or run into unexpected problems. The best and fastest place to get answers to your questions is on a site designed for programming questions and answers, like Stack Overflow. Try posting your question there. Remember to be thorough about what your problem is, and include all relevant information like what OS you’re on and what IDE you’re using.
|
A few common C++ problems
Views:
Category:
0 comments:
Post a Comment