What is wrong with the following code? Find (at least) five errors. For each err
ID: 3807337 • Letter: W
Question
What is wrong with the following code? Find (at least) five errors. For each error, state the line number , explain the error and show a correction.
#include<iostream>
#include<iomanip>
using namespace std;
class colorCode
{
public:
void setRGB(int);
//accepts an int parameter and
//sets the RGB to the value of the parameter
void setName(string);
//accepts a string parameter and sets the name of the //object
//to the value of the parameter
string getName() const;
//returns the name of the object
int getRGB() const;
//returns the RGB of the object
void changeColor();
// adds 10 to the RGB value
private:
string name;
int RGB;
}
int main()
{
colorCode paintCans[10];
int i;
for (i = 0; i < 10; i++)
paintCans.setRGB[i] = 0;
paintCans[5].setName(GREEN);
paintCan[5].setRGB(192000);
cout << paintCans[5].getName << ' '
<< paintCans[5].getRGB() << endl;
return 0;
}
Explanation / Answer
Error list:
14)}; //error1(include semicolon)
22)paintCans[i].setRGB(192000); //Error2(paintCans...you missed 's')
21)paintCans[i].setName("GREEN"); //error 3(include quotations to green word)
23)cout << paintCans[i].getName() << ' ' << paintCans[i].getRGB() << endl; //error4(getName is a method include parenthesis )
9)string static getName(); //error 5(include static keyword)
20)paintCans[i].setRGB(0); //error 6(include this line in place of old line)
Modified code:
1)#include<iostream>
2)#include<iomanip>
3)using namespace std;
4)class colorCode
5){
6)public:
7)void setRGB(int);
8)void setName(string);
9)string static getName(); //error 5(include static keyword)
10)int getRGB(); //returns the RGB of the object
11)void changeColor(); // adds 10 to the RGB value private:
12)string name;
13)int RGB;
14)}; //error1(include semicolon)
15)int main()
16){
17)colorCode paintCans[10];
18)int i;
19)for (i = 0; i < 10; i++)
20)paintCans[i].setRGB(0); //error 6(include this line in place of old line)
21)paintCans[i].setName("GREEN"); //error 3(include quotations to green word)
22)paintCans[i].setRGB(192000); //Error2(paintCans...you missed 's')
23)cout << paintCans[i].getName() << ' ' << paintCans[i].getRGB() << endl; //error4(getName is a method include parenthesis )
24)return 0;
25)}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.