Finding Errors – CSKC
Sign In

Search

Solved Problem! #CPP#2997    siteicon   siteicon  

Problem Statement - Finding Errors

Find errors in the following program and write the program with correct syntax.

TC++ #2997

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Solution


int a,b;           //Correct
cout<<“Enter two numbers :”; //Correct
cin>>a<<b; 
cin should always have stream extraction operator (>>)

const int c;
A constant declaration should necessarily be initialised.

c=a+b;
A constant is a read only variable so it can not have any assignment other than while declaration.

cout<<“Sum of given numbers :”<<c<<endl; //correct

The corrected program can be written as follows –

#include <iostream>
 using namespace std;
 int main()
 {
 int a,b;
 cout<<"Enter two numbers :";
 cin>>a>>b;
 const int c=a+b;
 cout<<"Sum of given numbers :"<<c<<endl;
 return 0 ;
 }

This program will run well but it is generally not advisable the constant declaration of such things which are likely to change. So even better program for above would be without constant declaration lie follows.

#include <iostream>
using namespace std;
int main()
{
 int a,b;
 cout<<"Enter two numbers :";
 cin>>a>>b;
 int c=a+b;
 cout<<"Sum of given numbers :"<<c<<endl;
 return 0 ;
}

Notes

  • const declaration is useful for things like value of PI, value of Maximum Marks etc. examples are
    const double PI=3.1416;
    const int MaxMarks=100;

 


Share

Created: 25-Jan-2018 00:38 | Updated: 22-Mar-2026 20:22|