Use 2 slashes // when commenting your code. Each line of code should have long c
ID: 3876150 • Letter: U
Question
Use 2 slashes // when commenting your code. Each line of code should have long comments above the section or a short comment inline (on the same line to the right of the line of code). I expect all code that is added/corrected to be commented and explained. Look up information in the textbook or online about how simple logic and syntax errors. There are 15 errors in the C++ program below. Some are logic and some are syntax errors. To complete this assignment, do the following: First, create a Visual C++ project to use for this assignment. Next, copy and paste the entire program into the .cpp file of your Visual C++ project. Then, find as many lines of code that that you can that have an error in them. Next, comment that line of code out, but leave the original code so we can see the mistake. Then, type in a correct line of code "below" the line you are correcting that fixes the error. Next, explain your correction with a correctly placed and formatted C++ comment.
Example:
// include // Missing # to begin the directive #include Lastly, copy and paste your entire .cpp file contents from Visual C++ into the answer box for this assignment.
--------OOP Lesson 0 Assignment Code-------------
// Remember to use "Courier New" for the font in all coding // submissionsso that spacing is maintained correctly
1 // findErrors.cpp
2 / This program has several syntax and logic errors.
3
4 #include iostream
5 using namespace std
6
7 int main
8 {
9 doubl length = 0, // Length of a room in feet
10 width = 0; // Width of a room in feet
11 area == 0; // Area of the room in sq. ft.
12
13 // Get the room dimensions
14 cout << "Enter room length (in feet): "
15 cin >> length;
16
17 cin << "Enter room width (in feet): ";
18 cout >> legth;
19
20 // Compute and display the area
21 width = length + area;
22 cout << "The area of the room is " << area " square feet." << endline;
23
24
25
Explanation / Answer
// findErrors.cpp
2 // / This program has several syntax and logic errors. // Use two sslashes to include comments in the code
3
4// #include iostream The angular brackets '< ' and '>' missing around iostream
#include<iostream>
5 //using namespace std
using namespace std; // Semi colon ';' missing at the end of the statement
6
7 //int main
int main() // Paranthesis is missing , since main is a function
8 {
9 //doubl length = 0, // Length of a room in feet
double length =0; // syntax error , as double is a datatype not doubl
10 width = 0; // Width of a room in feet
11 //area == 0; // Area of the room in sq. ft.
double area=0; // New variable area , should contain a datatype as the statement was previously // terminated . '=' is the assignment operator whereas == is for comparison .
12
13 // Get the room dimensions
14// cout << "Enter room length (in feet): "
cout<<" Enter room length (in feet):"; // ';' missing at the end of statement .
15 cin >> length;
16
17// cin << "Enter room width (in feet): "; // cin is an istream to take input , it cant print values and the cout<<"Enter room width (in feet): "; // brackets used for cin are '>>' . Here cout should be used .
18 // cout >> legth; // cout is ostream object , so cin should be used to take input .
cin>>length; // No variable called legth , it is length .
19
20 // Compute and display the area
21 //width = length + area; // Logical error
area = length * width ; // Area is calculated as product of length and width , and assigned //to area .
22 //cout << "The area of the room is " << area " square feet." << endline; // '<<' ostream objects missing after //cout<<"The area of the room is"<<area<<" square feet. "<<endline; // 'area' . Include '<<' ;
23 return 0; // Since main was defined to return an integer type object .
24} // Syntax error , closing of the main function '{' to be // included .
25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.