Find the Errors in Following C++ Programs – Questions for Paper
If you are trying to prepare for your paper of programming, then congratulate, you at the right place to see some important examples of finding the errors in C++ programs.
In this tutorial, we will see the examples of some programs with errors and then we will find the correct code.
Example 1 to find the errors in C++ Code
Wrong Code
int sum,sub,mult,div,module; int a,b; cout<<"Enter value of a =" cin<<a; cout<<"Enter value of b =; cin<<b; sum=a+b; sub=a-bmult=a*b; div=a/'b'; module=a%b;
Correct Code
int sum,sub,mult,div,module; int a,b; cout<<"Enter value of a ="; cin>>a; cout<<"Enter value of b ="; cin>>b; sum=a+b; sub=a-b; mult=a*b; div=a/b; module=a%b;
Example 2 to find the errors in C++ Code
Wrong Code
int outer; innermost; s; for(outer=5; outer>=1; outer--) { for(s=1 s<=5-outer s++) cout<<" "; for (innermost=1 innermost<=outer innermost++) { cout>>"*"; } cout<<endl; } }
Correct Code
int outer, innermost, s; for(outer=5; outer>=1; outer--) { for(s=1; s<=5-outer; s++) cout<<" "; for (innermost=1; innermost<=outer; innermost++) { cout<<"*"; } cout<<endl; }
Example 3 to find the errors in C++ Code
Wrong Code
if(Ascii_value_Variable>=97 & Ascii_value_Variable<=122) { cout<<"\nYou have entered a small letter"; } elseif(Ascii_value_Variable>=65 && Ascii_value_Variable<=90) { { cout<<"\nYou have entered a capital letter"; } elseif (Ascii_value_Variable>=0 && Ascii_value_Variable>=47; || Ascii_value_Variable>=54 & Ascii_value_Variable<=64; || Ascii_value_Variable>=91 & Ascii_value_Variable<=96 ; || Ascii_value_Variable>=123 & Ascii_value_Variable<=127) { cout<<"\nYou have entered a special v_charac_varible"; } } elseif (Ascii_value_Variable>=47 & Ascii_value_Variable<=57) cout<<"\nYou have entered a digit "; } }
Correct Code
if(Ascii_value_Variable>=97 && Ascii_value_Variable<=122) { cout<<"\nYou have entered a small letter"; } else if(Ascii_value_Variable>=65 && Ascii_value_Variable<=90) { cout<<"\nYou have entered a capital letter"; } else if (Ascii_value_Variable>=0 && Ascii_value_Variable>=47 || Ascii_value_Variable>=54 && Ascii_value_Variable<=64 || Ascii_value_Variable>=91 && Ascii_value_Variable<=96 || Ascii_value_Variable>=123 && Ascii_value_Variable<=127) { cout<<"\nYou have entered a special v_charac_varible"; } else if (Ascii_value_Variable>=47 && Ascii_value_Variable<=57) { cout<<"\nYou have entered a digit "; }
Example 4 to find the errors in C++ Code
Wrong Code
cout>>"please enter a sentences of your on choose: "; cout>>"\n"; gets(Words); for(number=0;Words[number]!='\0';++number) { if(Words[number]=='a' OR Words[number]=='e' OR Words[number]=='i' OR Words[number]=='o' OR Words[number]=='u' OR Words[number]=='A' OR Words[number]=='E' OR Words[number]=='I' OR Words[number]=='O' OR Words[number]=='U') ++vowel_words; else if((Words[number]>='a'&& Words[number]<='z') O| (Words[number]>='A'&& Words[number] <='Z')) ++Consonants; else if(Words[number]>='0' & Words[number]<='9') ++digit_words; else if (Words[number]==' ') ++spaces; }
Correct Code
cout<<"please enter a sentences of your on choose: "; cout<<"\n"; gets(Words); for(number=0;Words[number]!='\0';++number) { if(Words[number]=='a' || Words[number]=='e' || Words[number]=='i' || Words[number]=='o' || Words[number]=='u' || Words[number]=='A' || Words[number]=='E' || Words[number]=='I' || Words[number]=='O' || Words[number]=='U') ++vowel_words; else if((Words[number]>='a'&& Words[number]<='z') || (Words[number]>='A'&& Words[number]<='Z')) ++Consonants; else if(Words[number]>='0'&& Words[number]<='9') ++digit_words; else if (Words[number]==' ') ++spaces; }
Example 5 to find the errors in C++ Code
Wrong Code
for(x=0;x<z;x++) { for(y=0;y<=x;y++) { if(a=='Z') break; cout<<a; a++; } cout<<endl; }
Correct Code
This code is error free code.