point out the type of error in the following program:
#include <iostream>
using namespace std;
int main()
{
int h= 10 ; w = 12
cout<<"Area of rectangle" <<h+w
}
Answers
Answer:
//Program to receive two integer numbers and display their sum
#include <iostream>
using namespace std; int main()
{
int num1, num2, sum;
//variables num1, num2, and sum are declared as integers
cout << "\n Enter Number 1: ";
cin >> num1;
cout << "\n Enter Number 2: "; cin >> num2;
sum = num1 + num2;
cout << "\n The sum of " << num1 << " and " << num2 << " is " << sum;
}
Illustration 9.2: C++ Program to accept any character and display its next character
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "\n Enter a character: ";
cin >> ch;
ch = ch + 1;
cout << "\n The Next character: " << ch;
}
The output produced by the program will be
Enter a character: A
The Next character: B
Explanation:
Answer:
In the given program there are two syntax errors.
Explanation:
Consider the following given instruction
int h= 10 ; w = 12
The correct syntax for this instruction is :
int h=10,w=12;
or
int h=10;
int w=12;
In C++ a ‘;’ indicates end of a single instruction . Hence ( int h=10;) is an instruction and variable w is undefined variable which will raise an syntax error.
Another syntax error will be generated in the following instruction:
cout<<"Area of rectangle" <<h+w
a semicolon ‘;’ is expected at the end of this instruction to complete it.
#SPJ3