SlideShare a Scribd company logo
Classes and Errors
1
Prepared by
Dr.S.Raja Ratna
2
Classes and Errors
● Error is an illegal operation performed by the user which results in
abnormal working of the program.
● Some of the errors inhibit the program from getting compiled or
executed. Thus errors should be removed before compiling and
executing.
Errors are classified as below:
1. Syntax Errors
2. Runtime Errors
3. Logical Errors
4. Linked Errors
5. Semantic Errors
3
1. Syntax Errors
● Errors that occur when you violate the rules of writing C/C++ syntax are
known as syntax errors. All these errors are detected by compiler and thus
are known as compile-time errors.
● Most frequent syntax errors are:
○ Missing Parenthesis (} )
○ Printing the value of variable without declaring it
#include <iostream> void main() {
cout << "Geeks for geeks!"; // missing semicolon }
The functions associated with stack are:
2. Runtime Errors
● This type of error occurs while the program is running. Because this is
not a compilation error, the compilation will be completed successfully.
● These errors occur due to segmentation fault when a number is divided
by division operator or modulo division operator.
Example: Let us consider an array of length 5 i.e. array[5], but during
runtime, if we try to access 10 elements i.e array[10] then we get
segmentation fault errors called runtime errors. Giving only an array length
of 5
#include <iostream>
void main()
{
int array[5];
}
4
3. Logical Errors
● Even if the syntax and other factors are correct, we may not get the desired
results due to logical issues. These are referred to as logical errors.
Example: In the below example, the for loop iterates 5 times but the output will
be displayed only one time due to the semicolon at the end of for loop. These
kinds of errors are called logical errors.
void main() {
int j;
// Cause of Logical error
for(j=0;j<=5;j++);
{ cout << "Geeks for geeks"; } }
5
4. Linker Errors
● When the program is successfully compiled and attempting to link the
different object files with the main object file, errors will occur. When this error
occurs, the executable is not generated.
● This could be due to incorrect function prototyping, an incorrect header file, or
other factors. If main() is written as Main(), a linked error will be generated.
#include <iostream>
int Main() {
cout << "Geeks for geeks";
return 0;
}
6
7
5. Semantic Errors
When a sentence is syntactically correct but has no meaning, semantic
errors occur. This is similar to grammatical errors. If an expression is
entered on the left side of the assignment operator, a semantic error may
occur.
#include <iostream>
int main()
{
int a = 10, b = 20, c;
a + b = c;
cout << c;
return 0;
}
Example Code
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
stack<int> s;
int c, i;
while (1) {
cout<<"1.Size of the Stack"<<endl;
cout<<"2.Insert Element into the Stack"<<endl;
cout<<"3.Delete Element from the Stack"<<endl;
cout<<"4.Top Element of the Stack"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>c;
8
switch(c) {
case 1:
cout<<"Size of the stack: ";
cout<<s.size()<<endl;
break;
case 2:
cout<<"Enter value to be inserted: ";
cin>>i;
s.push(i);
break;
case 3:
i = s.top();
if (!s.empty()) {
s.pop();
cout<<i<<" Deleted"<<endl;
}
9
else {
cout<<"Stack is Empty"<<endl;
}
break;
case 4:
cout<<"Top Element of the Stack: ";
cout<<s.top()<<endl;
break;
case 5:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0; }
10
Output
1.Size of the Stack
2.Insert Element into the Stack
3.Delete Element from the Stack
4.Top Element of the Stack
5.Exit
Enter your Choice: 1 Size of the stack: 0
1.Size of the Stack
2.Insert Element into the Stack
3.Delete Element from the Stack
4.Top Element of the Stack
5.Exit 11
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Stack
2.Insert Element into the Stack
3.Delete Element from the Stack
4.Top Element of the Stack
5.Exit
Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Stack
2.Insert Element into the Stack
3.Delete Element from the Stack
4.Top Element of the Stack
5.Exit
12

More Related Content

Similar to Classes and Errors.pdf

Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.gerrell
 
Prog1-L3.pptx
Prog1-L3.pptxProg1-L3.pptx
Prog1-L3.pptx
valerie5142000
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
aprilyyy
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
IEEE MIU SB
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
Dr. Syed Hassan Amin
 
DeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latestDeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latest
Atifkhilji
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-c
Md Nazmul Hossain Mir
 
Cinfo
CinfoCinfo
Cinfo
teach4uin
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
Mohamed Abdallah
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
Cherimay Batallones
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Excception handling
Excception handlingExcception handling
Excception handling
simarsimmygrewal
 
C and its errors
C and its errorsC and its errors
C and its errorsJunaid Raja
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions
khawajasharif
 
lecture1 pf.pptx
lecture1 pf.pptxlecture1 pf.pptx
lecture1 pf.pptx
MalikMFalakShairUnkn
 
basic program
basic programbasic program
basic program
NIDA HUSSAIN
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
Richard Clark
 

Similar to Classes and Errors.pdf (20)

Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Prog1-L3.pptx
Prog1-L3.pptxProg1-L3.pptx
Prog1-L3.pptx
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
DeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latestDeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latest
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-c
 
Cinfo
CinfoCinfo
Cinfo
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
Inline function
Inline functionInline function
Inline function
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Excception handling
Excception handlingExcception handling
Excception handling
 
My final requirement
My final requirementMy final requirement
My final requirement
 
C and its errors
C and its errorsC and its errors
C and its errors
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions
 
lecture1 pf.pptx
lecture1 pf.pptxlecture1 pf.pptx
lecture1 pf.pptx
 
basic program
basic programbasic program
basic program
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
 

More from rajaratna4

Memory and Cache Coherence in Multiprocessor System.pdf
Memory and Cache Coherence in Multiprocessor System.pdfMemory and Cache Coherence in Multiprocessor System.pdf
Memory and Cache Coherence in Multiprocessor System.pdf
rajaratna4
 
Multicore processor.pdf
Multicore processor.pdfMulticore processor.pdf
Multicore processor.pdf
rajaratna4
 
Hardware Multithreading.pdf
Hardware Multithreading.pdfHardware Multithreading.pdf
Hardware Multithreading.pdf
rajaratna4
 
Flynn's classification.pdf
Flynn's classification.pdfFlynn's classification.pdf
Flynn's classification.pdf
rajaratna4
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
rajaratna4
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
rajaratna4
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
rajaratna4
 
Abstract Class and Interface.pdf
Abstract Class and Interface.pdfAbstract Class and Interface.pdf
Abstract Class and Interface.pdf
rajaratna4
 

More from rajaratna4 (8)

Memory and Cache Coherence in Multiprocessor System.pdf
Memory and Cache Coherence in Multiprocessor System.pdfMemory and Cache Coherence in Multiprocessor System.pdf
Memory and Cache Coherence in Multiprocessor System.pdf
 
Multicore processor.pdf
Multicore processor.pdfMulticore processor.pdf
Multicore processor.pdf
 
Hardware Multithreading.pdf
Hardware Multithreading.pdfHardware Multithreading.pdf
Hardware Multithreading.pdf
 
Flynn's classification.pdf
Flynn's classification.pdfFlynn's classification.pdf
Flynn's classification.pdf
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
Abstract Class and Interface.pdf
Abstract Class and Interface.pdfAbstract Class and Interface.pdf
Abstract Class and Interface.pdf
 

Recently uploaded

ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 

Recently uploaded (20)

ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 

Classes and Errors.pdf

  • 1. Classes and Errors 1 Prepared by Dr.S.Raja Ratna
  • 2. 2 Classes and Errors ● Error is an illegal operation performed by the user which results in abnormal working of the program. ● Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing. Errors are classified as below: 1. Syntax Errors 2. Runtime Errors 3. Logical Errors 4. Linked Errors 5. Semantic Errors
  • 3. 3 1. Syntax Errors ● Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. All these errors are detected by compiler and thus are known as compile-time errors. ● Most frequent syntax errors are: ○ Missing Parenthesis (} ) ○ Printing the value of variable without declaring it #include <iostream> void main() { cout << "Geeks for geeks!"; // missing semicolon }
  • 4. The functions associated with stack are: 2. Runtime Errors ● This type of error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. ● These errors occur due to segmentation fault when a number is divided by division operator or modulo division operator. Example: Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to access 10 elements i.e array[10] then we get segmentation fault errors called runtime errors. Giving only an array length of 5 #include <iostream> void main() { int array[5]; } 4
  • 5. 3. Logical Errors ● Even if the syntax and other factors are correct, we may not get the desired results due to logical issues. These are referred to as logical errors. Example: In the below example, the for loop iterates 5 times but the output will be displayed only one time due to the semicolon at the end of for loop. These kinds of errors are called logical errors. void main() { int j; // Cause of Logical error for(j=0;j<=5;j++); { cout << "Geeks for geeks"; } } 5
  • 6. 4. Linker Errors ● When the program is successfully compiled and attempting to link the different object files with the main object file, errors will occur. When this error occurs, the executable is not generated. ● This could be due to incorrect function prototyping, an incorrect header file, or other factors. If main() is written as Main(), a linked error will be generated. #include <iostream> int Main() { cout << "Geeks for geeks"; return 0; } 6
  • 7. 7 5. Semantic Errors When a sentence is syntactically correct but has no meaning, semantic errors occur. This is similar to grammatical errors. If an expression is entered on the left side of the assignment operator, a semantic error may occur. #include <iostream> int main() { int a = 10, b = 20, c; a + b = c; cout << c; return 0; }
  • 8. Example Code #include <iostream> #include <stack> #include <string> #include <cstdlib> using namespace std; int main() { stack<int> s; int c, i; while (1) { cout<<"1.Size of the Stack"<<endl; cout<<"2.Insert Element into the Stack"<<endl; cout<<"3.Delete Element from the Stack"<<endl; cout<<"4.Top Element of the Stack"<<endl; cout<<"5.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; 8
  • 9. switch(c) { case 1: cout<<"Size of the stack: "; cout<<s.size()<<endl; break; case 2: cout<<"Enter value to be inserted: "; cin>>i; s.push(i); break; case 3: i = s.top(); if (!s.empty()) { s.pop(); cout<<i<<" Deleted"<<endl; } 9
  • 10. else { cout<<"Stack is Empty"<<endl; } break; case 4: cout<<"Top Element of the Stack: "; cout<<s.top()<<endl; break; case 5: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; } 10
  • 11. Output 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Size of the stack: 0 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit 11
  • 12. Enter your Choice: 2 Enter value to be inserted: 1 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the Stack 2.Insert Element into the Stack 3.Delete Element from the Stack 4.Top Element of the Stack 5.Exit 12