12 November 2012

A few common C++ problems


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:
1
#include <iostream>
Second, add the following code at the end of the main() function (right before the return statement):
1
2
3
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.
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:
1
#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:
1
#include <iostream>
In any function where you use use cin or cout, include the following line:
1
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. 
source- learncpp.com

1 comment:

  1. https://shubhamggps.blogspot.in/2016/08/id-card-generator-programee-c.html

    ReplyDelete

DON'T SPAM HERE....!

Which of the following programming language is best and easy to learn for you...?

TopBottom Related Posts Plugin for WordPress, Blogger...