0% found this document useful (0 votes)
5 views3 pages

C++ Header File Corrections Guide

The document outlines corrections to various handouts for CS201, focusing on the proper use of C++ header files, specifically the transition from old-style to new-style headers. It emphasizes that the main function in Dev-C++ must return a value and provides corrections to specific programming errors found in the handouts. Additionally, it addresses incorrect problem statements and programming logic in examples provided in the course materials.

Uploaded by

786scholarships
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C++ Header File Corrections Guide

The document outlines corrections to various handouts for CS201, focusing on the proper use of C++ header files, specifically the transition from old-style to new-style headers. It emphasizes that the main function in Dev-C++ must return a value and provides corrections to specific programming errors found in the handouts. Additionally, it addresses incorrect problem statements and programming logic in examples provided in the course materials.

Uploaded by

786scholarships
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Corrections in CS201 Handouts

Page No. 15

The old-style C++ header files are written with .h such as


iostream.h whereas new-style header file names omit the “.h” suffix
such as iostream. Some headers, such as math.h, were inherited from
the C language. In those cases, the new-style headers prefix a “c” to
the name and omit the “.h”. It is better to use new-style headers
(vector, string, etc). For C library, new-style headers start with ”c”
(cstdio, cstdlib, etc).

The new style C++ header files are all wrapped in a single namespace
called std. The namespace std contains entire C++ standard library.
The using namespace is used to import entire std namespace into
current scope so that items within namespace can be used in
program.

Old style:
#include<iostream.h>

New style:
using namespace std ;
#include<iostream>

Newer version of Dev-C++ supports new style of including header file


so it is better to use new style.

Page No. 18

While using old style of including header file, following warnings at


the bottom of Dev-C++ will appear. It is just a warning and not a
syntax error. You can ignore this warning and run your program. It
will run successfully. You can avoid this warning by using new style of
header files instead of old style with .h extension.
Page No. 84, 85, 129, 133, 139, 173, 178, 353, 380, 462, 483

In Dev C++, main function must return a value. Dev-C++ does not
allow void main() so int main() or main() will be used while using Dev-
C++. Both int main() and main() are similar. If we do not mention
return value type with function name then default return type of such
function will be int.

Page No. 29
In the topic Use of Operators, problem statement is incorrect.

Error:
Write a program that takes a four digits integer from user and shows
the digits on the screen separately i.e. if user enters 7531, it displays
7,5,3,1 separately.

Correction:
Write a program that takes a four digits integer from user and shows
the digits on the screen separately i.e. if user enters 7531, it displays
1,3,5,7 separately.

Page No. 95
In the topic Function Calling, program contains error.

Error:
Function prototype should contain asterisk “*’.

Correction:
#include <iostream.h>
void square(double *);
main()
{
…..}

Page No. 104-105


In the topic Sample Program 1, program contains error.

Error:
Program also accept the negative inputs, program output should be as follows,
Correction:

The total number of integers entered by user is

Page No. 418


Overloading [] operator to create arrays

if( (index = 0) &&


(index < length) )
return aray[index];

Error:
If condition should be like follows.

Correction:
if( (index >= 0) &&
(index < length) )

Common questions

Powered by AI

Transitioning from old-style to new-style C++ headers involves removing the ".h" suffix and using headers within the "std" namespace. For example, instead of including "#include<iostream.h>", the new-style requires "using namespace std; #include<iostream>". Additionally, new-style headers for C library functions prefix a "c" to the name and omit the ".h" (e.g., "cstdio" replacing "stdio.h")

When designing a program that accepts user input, it is important to ensure input validation to prevent nonsensical data. For example, a program should correctly respond to different types of inputs, such as ensuring integer-only inputs are accepted and correctly handling negative inputs if not expected, as noted by the instruction for a program to adjust its output correction when handling invalid conditions.

The namespace "std" encapsulates the entire C++ standard library which includes functionalities such as input/output streams, containers, and algorithms. Using "std" allows developers to avoid naming conflicts by providing a scope for standard library functions and objects. By including "using namespace std;" one can access these elements without prefixing "std::" each time, simplifying the code syntax while improving code organisation and modularity.

Handling compiler warnings in old-style header files is important because these warnings signal potential portability and compatibility issues with modern compilers and standard libraries. Ignoring these warnings may lead to unexpected behavior or reduced code maintainability in evolving C++ environments. By transitioning to new-style headers, programmers can ensure more robust and future-proof code.

Incorrectly using the conditional in array overloading with the '[]' operator can lead to logical errors where the code may incorrectly evaluate or manipulate array indices. For example, using an incorrect condition like "if( (index = 0) && (index < length) )" assigns the index to 0 and always evaluates to false unless the length is 0. The correct condition is "if( (index >= 0) && (index < length) )", ensuring that index checks fall within valid boundaries, thus maintaining array integrity.

In Dev-C++, 'void main()' is not recommended because it does not adhere to the C++ standard that requires 'main' to return an integer value. Therefore, 'int main()' should be used as it correctly specifies the return type for the main function as int, complying with the standard.

Correcting conditional statements in operator overloading directly impacts program logic by ensuring that the operator performs the intended operations within valid bounds. Any error like using "if( (index = 0) && (index < length) )" can incorrectly evaluate conditions, causing wrong indexing and possible runtime errors. Correct conditions ensure logical validity and prevent potential undefined behavior by maintaining correct boundary checks with "if( (index >= 0) && (index < length) )".

New-style C++ headers are preferred because they omit the ".h" suffix, which aligns with the C++ standard library that is encapsulated under the "std" namespace. This uniformity supports better namespace management and compatibility with C++ standards. Moreover, using the new style headers helps in avoiding the warnings that appear when using old-style headers.

Including an asterisk '*' in a function prototype for pointer arguments is crucial as it signifies that the argument is a pointer, allowing the function to receive and manipulate the address of a variable rather than its value. This is essential for operations like modifying the value at the pointer's address, which is necessary for functions such as 'void square(double *x)' to properly perform operations on the referenced data.

The problem statement asking to display digits of a four-digit integer individually was incorrect by suggesting "if user enters 7531, it displays 7,5,3,1 separately." It needs to be revised to "if user enters 7531, it displays 1,3,5,7 separately," which accurately represents digit display in reverse order.

You might also like