Exercise: Types of Errors in C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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