0% found this document useful (0 votes)
353 views

Exercise: Types of Errors in C++

The document discusses three types of errors in C++: syntax errors, logical errors, and runtime errors. It provides examples of each type of error and exercises to find errors in code samples. Syntax errors occur due to issues like missing semicolons or unbalanced brackets and are visible during compilation. Logical errors do not cause errors but produce wrong results, like infinite loops. Runtime errors occur during program execution and produce error messages, such as for division by zero.

Uploaded by

Sarah Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
353 views

Exercise: Types of Errors in C++

The document discusses three types of errors in C++: syntax errors, logical errors, and runtime errors. It provides examples of each type of error and exercises to find errors in code samples. Syntax errors occur due to issues like missing semicolons or unbalanced brackets and are visible during compilation. Logical errors do not cause errors but produce wrong results, like infinite loops. Runtime errors occur during program execution and produce error messages, such as for division by zero.

Uploaded by

Sarah Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Friday, 11 May 2012

Types of errors in C++

  Syntax error:- This error occurs due to following reason.


                         I.   Not following the grammatical rules used in declaration of identifier.
                        II.   Not declaring an identifier used in program.
                       III.   Not terminating statement by semicolon.
                      IV.   Not providing equal number of opening and closing braces etc.
These errors can be rectified by the user as it is displayed while compiling the program.

Logical error:- This error won’t be displayed on the screen. However it will lead to display wrong results. Example: An infinite loop. This error lead to abnormal termination of a
program or infinite loop.

Runtime error:- This error occurs while running a program by displaying the message listed below.
                          I.   Division by 0.
                         II.  Overflow
                        III.  Underflow

Now lets have an example:-


Q. Write a corrected code of the following program segment by underlining the error corrected.
void main()
{     
      int a,b;
      cin<<a;
b=a;
      cout <<"b=",a;
};

Answer:-
#include<iostream>
void main()
{     
      int a,b;
      cin>>a;
b=a;
      cout <<"b="<<a;
}

Exercise
Q. Find the errors in following programs.
1. #include “iostream”
void main()
{
const PI=3.14;
int r,h;
cout>>”Enter values of r and h’
cin>>r,h;
v=PI*r*r*h;
cout<<’volume=”<<v;
}

2. #include<iostream>
#define p=10000
int main()
{
int r,t
cout<<”Enter r,t;
cin>>r>>t;
      float si= p*r*t/100.0;
      cout>>”Simple interest =”<<SI;
      getch()
}
Posted by Dipesh Palod at 13:58 
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

You might also like