0% found this document useful (0 votes)
8 views4 pages

CPP Error Practice

The document provides examples of faulty C++ code alongside their corrected versions. Each example highlights common coding errors, such as incorrect operator usage and syntax mistakes. The corrections demonstrate proper coding practices to achieve the intended functionality.

Uploaded by

bofep39598
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

CPP Error Practice

The document provides examples of faulty C++ code alongside their corrected versions. Each example highlights common coding errors, such as incorrect operator usage and syntax mistakes. The corrections demonstrate proper coding practices to achieve the intended functionality.

Uploaded by

bofep39598
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Error Identification Practice

Left side contains faulty code. Right side shows the corrected version.

Faulty Code Correct Code

Example 1 – Faulty Code

#include <iostream> #include <iostream>


using namespace std using namespace std;

int main() int main()


{ {
int a, b, c int a, b, c;
float avg; float avg;

cout << "Enter three numbers: " cout << "Enter three numbers: ";
cin >> a >> b >> c; cin >> a >> b >> c;

avg = a + b + c / 3; avg = (a + b + c) / 3.0;

cout << "Average = " avg << endl; cout << "Average = " << avg << endl;

return 0 return 0;
} }

Example 2 – Faulty Code

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() int main()


{ {
int num; int num;

cout << "Enter a number: "; cout << "Enter a number: ";
cin >> num cin >> num;

if(num % 2 = 0) if(num % 2 == 0)
{
{ cout << "Number is Even" << endl;
cout << "Number is Even" << endl; }
} else
else {
cout << "Number is Odd << endl; cout << "Number is Odd" << endl;
}
return 0;
} return 0;
}

Example 3 – Faulty Code

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() int main()


{ {
int n, sum = 0; int n, sum = 0;

cout << "Enter value of n: "; cout << "Enter value of n: ";
cin >> n; cin >> n;

for(int i = 1; i <= n i++) for(int i = 1; i <= n; i++)


{ {
sum = sum + i sum = sum + i;
} }

cout << "Sum = " << sum << endl cout << "Sum = " << sum << endl;

return 0; return 0;
} }

Example 4 – Faulty Code

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() int main()


{ {
int marks; int marks;

cout << "Enter marks: "; cout << "Enter marks: ";
cin >> marks; cin >> marks;
if(marks >= 80) if(marks >= 80)
cout << "Grade A"; cout << "Grade A";
else if marks >= 60 else if(marks >= 60)
cout << "Grade B"; cout << "Grade B";
else if(marks >= 50) else if(marks >= 50)
cout << "Grade C" cout << "Grade C";
else else
cout << "Fail"; cout << "Fail";

return 0; return 0;
} }

Example 5 – Faulty Code

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() int main()


{ {
int n, fact = 1; int n, fact = 1;

cout << "Enter number: "; cout << "Enter number: ";
cin >> n; cin >> n;

for(int i = 1; i <= n; i--) for(int i = 1; i <= n; i++)


{ {
fact = fact * i; fact = fact * i;
} }

cout << "Factorial = " << fact << endl; cout << "Factorial = " << fact << endl;

return 0 return 0;
} }

Example 6 – Faulty Code

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() int main()


{ {
int a, b; int a, b;
cout << "Enter two numbers: "; cout << "Enter two numbers: ";
cin >> a, b; cin >> a >> b;

if(a > b) if(a > b)


cout << "Largest number = " << a << cout << "Largest number = " << a <<
endl endl;
else else
cout << "Largest number = " << b << cout << "Largest number = " << b <<
endl; endl;

} return 0;
}

You might also like