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

C++ Note

The document discusses common errors in C++ programs like missing headers, semicolons, and declarations. It then provides several code snippets with errors and their corresponding outputs or error messages. For example, a snippet is shown missing the std namespace declaration for cout, resulting in the error "cout was not declared in this scope." Another snippet initializes a variable without declaring it first, resulting in the error "b was not declared in this scope."

Uploaded by

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

C++ Note

The document discusses common errors in C++ programs like missing headers, semicolons, and declarations. It then provides several code snippets with errors and their corresponding outputs or error messages. For example, a snippet is shown missing the std namespace declaration for cout, resulting in the error "cout was not declared in this scope." Another snippet initializes a variable without declaring it first, resulting in the error "b was not declared in this scope."

Uploaded by

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

Error

Common types error:


 Header file
 Variable declaration
 Missing Semicolon
 Variable initialization
What’s the errors and warning will be shown those following program?
// #include <iostream> // if missing it.
using namespace std;
int main()
{ int a,b;
a=10;
b=(a);
cout<<b;
return 0;
}
Error: 'cout' was not declared in this scope
#include <iostream>
//using namespace std; // if it missing
int main()
{ int a,b;
a=10;
b=(a);
cout<<b;
return 0;
}
Error: 'cout' was not declared in this scope.
note: suggested alternative:
note: 'std::cout'
#include <iostream>
using namespace std;
// int main() // 1. if it missing , 2.if ‘()’ or ‘)‘ Missing 3. Missing ‘(‘
int a,b; 4. ‘{‘ is missing.
a=10;
for(a=1;a<=10;a++)
cout<<a;
return 0;
}
Error: 1.expected unqualified-id before '{' token.
2. expected primary-expression before 'int'
3. expected initializer before ')' token
4. expected initializer before 'int'

#include <iostream>
using namespace std;
int main()
{ int a;b; V.V.I
a=10;
b=(a);
cout<<b;
return 0;
}
Error: 'b' was not declared in this scope
#include <iostream>
using namespace std;
int main()
{ int a,b,c;
a=10;
b=20
cout<<a+1<<b+1;
return 0;
}
Answer:
Error : expected ‘ ; ‘before ‘cout’.
Warning: unused variable ‘c’
#include <iostream>
using namespace std;
int main()
{ int a,b;
a=10;
b=sqrt(a); work on header file <math.h>
cout<<b;
return 0;}
Answer:
Error: 'sqrt' was not declared in this scope
Output
If output variables has nothing to show actual results
then output for :
Data Output
Int 7208852
Char Null (nothing)
Float 1.0101e-38
Double 2.0513e-307

Example:
#include <iostream>
using namespace std;
int main()
{ int a; Output:2.0513e-307
double b;
a=10;
cout<<b; // nothing have for b to shown as output
return 0;
}
#include <iostream>
using namespace std;
int main ()
{ Output :6
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a- b;
cout << result;
return 0;
}
#include <iostream>
using namespace std;
int main()
{ int a,b=10; Output:10.
a=10+2%2*3;
cout << a << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
cout << "a:";
cout << a; Output: a:4b:7
cout << " b:";
cout << b;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{int a,b; // Output :66 // 57 //57
int c=5;
a=++c; //a=c++;//a=c++;
b=c++; //b=++c;//b=++c;
cout<<a;
cout<<b;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{int a,b;
int c=1,d=2; // Output:2
a=++c; // 2
b=d++;
cout<<a<<"\n";
cout<<b;
return 0;
}
#include <iostream>
using namespace std;
int main()
{ int a=5,b=3,c=6;
if((a==5 && b>a ) && (b==3 || a>c)){

cout << a << endl; } Output: false statement


else {
cout<<" false statement"<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{ int b=10;
switch (b){
case 10:
cout<<"case 10\t :"<<b+1;
break;
case 9:
cout<<"case 9\t :"<<b;
break;
case 8:
cout<<"case 8 \t :"<<b;
break;
default :
cout << "case 10\t :"<< b<<endl;
}
return 0; } Output:case 10 :11
#include <iostream>
using namespace std;
int main()
{ int b;
for( b=1;b<5;b++) { //loop checked :5 times
// Iteration : 4 time .
cout<<b;
}
return 0;
}

By :MUZAHED
ID:153092.
#include <iostream>
using namespace std;
int main()
{
cout<<"first loop\n";
for(int y = 5; y >0; y --)
{cout << y<< "\t"<< y; }
cout<<"\n\nsecond loop\n";
for(int z = 15; z < 10; z ++)
{cout << z << "\n"; }
return 0;
}
Output: first loop
5 54 43 32 21 1
second loop.
#include<iostream>
using namespace std;
int main ()
{
int i, j;
for(i=1;i<=3;i++) //outer loop start
{ cout<<"\ni is now\t"<<i;
for(j=1;j<=4;++j) //inner loop start
{
cout<<" j="<<j; //for 1 space.
} // inner loop end
} return 0; // outer loop end
}
Output:
i is now 1 j=1 j=2 j=3 j=4 // distance 1 space.
i is now 2 j=1 j=2 j=3 j=4
i is now 3 j=1 j=2 j=3 j=4

You might also like