Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting an error in line 21 of main.cpp stating \"uninitialized local varia

ID: 3835944 • Letter: I

Question

I am getting an error in line 21 of main.cpp stating "uninitialized local variable 'temp' used"? code: C4700


Can someone please fix the code so I can see what I did wrong.


File name: classes.h


#ifndef CLASSES_H


#define CLASSES_H


//Base class


class baseShape


{


protected:


int shapeWidth, shapeHeight;


//Public functions of the class


public:


//Class constructor


baseShape( int checkVariable, int h);


//Function to find the area of the shape


int findArea();


};


//Derived class inherited from base class baseShape


class derivedRectangle: public baseShape


{


public:


//Class constructor


derivedRectangle( int checkVariable, int h);


//derived redefinition of the function


int findArea ();


};


//Other derived class inherited from baseShape


class derivedTriangle: public baseShape{


public:


derivedTriangle( int checkVariable, int h);


//redefinition of findArea function


//function overloadin can be used here


int findArea ();


};


#endif


File Name: classes.cpp


#include "stdafx. h"


#include "classes.h"


#include <iostream>


using namespace std;


baseShape::baseShape( int checkVariable, int h)


{


shapeWidth = checkVariable;


shapeHeight = h;


}


//Function to find the area of the shape


int baseShape:: findArea()


{


cout << "Base class area :" <<endl;


return 3;


}


derivedRectangle::derivedRectangle( int checkVariable, int h):


baseShape(checkVariable, h) { }


//derived redefinition of the function


int derivedRectangle::findArea ()


{


cout << "Derived class findArea :" <<endl;


return (shapeWidth * shapeHeight);


}


derivedTriangle :: derivedTriangle( int checkVariable, int h):baseShape(checkVariable, h) { }


//redefinition of findArea function


//function overloadin can be used here


int derivedTriangle::findArea ()


{


cout << "derivedTriangle class findArea :" <<endl;


return (shapeWidth * shapeHeight / 2);


}


File Name: Main.cpp


//Include the header fileData


#include "stdafx.h"


#include <iostream>


#include <iomanip>


#include <fstream>


#include "classes.h"


#include “classes.cpp”


//Using the namespaces


using std::setw;


using namespace std;


void userInteract ();


void pointerTest();


void selection();


void switchCase();


void ArrayElements();


void fileOPerations();


//Function to swap using pass by refference


void swapPassByReference(int *first, int *second)


{


int *temp;


*temp = *first;


* first = *second;


*second = *temp;


}


//function uses a pointer


void pointerTest()


{


int intVar;


cout << " Address of intVar variable: ";


cout << &intVar << endl;


}


void userInteract(){


float input1, input2, p;


cout << "Please Input two numbers: ";


cin >> input1 >> input2;


p = input1*input2;


cout << "Product of your inputs is = " << p;


}


//To use selection statements


void selection()


{


int checkVariable = 100;


if( checkVariable < 20 )


{


cout << "checkVariable is less than 20;" << endl;


}


else


{


cout << "checkVariable is not less than 20;" << endl;


}


cout << "value of checkVariable is : " << checkVariable << endl;


}


//To use array


void ArrayElements()


{


//declare the array


int n[10];


//The for loops


for ( int itr = 0; itr < 10; itr++ )


{


n[itr] = itr + 100;


}


cout << "Element" << setw(13) << "Value is" << endl;


for ( int vi = 0; vi < 10; vi++ )


{


cout << setw( 7 )<< vi << setw( 13 ) << n[ vi ] << endl;


}


}


//Switch for select


void switchCase()


{


char mark = 'D';


switch(mark)


{


case 'A' :


cout << "Excellent!" << endl;


break;


case 'B' :


case 'C' :


cout << "Well done" << endl;


break;


case 'D' :


cout << "You passed" << endl;


break;


case 'F' :


cout << "Failed" << endl;


break;


default :


cout << "Invalid mark" << endl;


}


cout << "Your mark is " << mark << endl;


}


//Read the fileData


void fileOPerations() {


char fileData[100];


ofstream filewrite;


filewrite.open("file.txt");


cout << "File write" << endl;


cout << "Please input your name: ";


cin.getline(fileData, 100);


cin.getline(fileData, 100);


filewrite << fileData << endl;


cout << "Please input your age: ";


cin >> fileData;


cin.ignore();


filewrite << fileData << endl;


filewrite.close();


ifstream infile;


infile.open("file.txt");


cout << "File read" << endl;


infile >> fileData;


cout << fileData << endl;


infile >> fileData;


cout << fileData << endl;


infile.close();


}


//main function


int main() {


int a = 100;


int b = 200;


userInteract();


pointerTest();


//swapPassByReference(&a, &b);


cout<<"Now a ="<<a<<"and b = "<<b<<endl;


selection();


switchCase();


fileOPerations();


//baseShape baseShape(0, 0));


derivedRectangle rec(10,7);


derivedTriangle tri(10,5);


cout<<rec.findArea();


//baseShape = rec;


//baseShape.findArea();


//baseShape = tri;


cout<<tri.findArea();


//cout<<baseShape.findArea();


return 0;


}


Explanation / Answer

File name: classes.h

#ifndef CLASSES_H

#define CLASSES_H

//Base class

class baseShape

{

protected:

int shapeWidth, shapeHeight;

//Public functions of the class

public:

//Class constructor

baseShape( int checkVariable, int h);

//Function to find the area of the shape

int findArea();

};

//Derived class inherited from base class baseShape

class derivedRectangle: public baseShape

{

public:

//Class constructor

derivedRectangle( int checkVariable, int h);

//derived redefinition of the function

int findArea ();

};

//Other derived class inherited from baseShape

class derivedTriangle: public baseShape{

public:

derivedTriangle( int checkVariable, int h);

//redefinition of findArea function

//function overloadin can be used here

int findArea ();

};

#endif

File Name: classes.cpp

#include "stdafx. h"


#include "classes.h"

#include <iostream>

using namespace std;

baseShape::baseShape( int checkVariable, int h)

{

shapeWidth = checkVariable;

shapeHeight = h;

}

//Function to find the area of the shape

int baseShape:: findArea()

{

cout << "Base class area :" <<endl;

return 3;

}

derivedRectangle::derivedRectangle( int checkVariable, int h):

baseShape(checkVariable, h) { }

//derived redefinition of the function

int derivedRectangle::findArea ()

{

cout << "Derived class findArea :" <<endl;

return (shapeWidth * shapeHeight);

}

derivedTriangle :: derivedTriangle( int checkVariable, int h):baseShape(checkVariable, h) { }

//redefinition of findArea function

//function overloadin can be used here

int derivedTriangle::findArea ()

{

cout << "derivedTriangle class findArea :" <<endl;

return (shapeWidth * shapeHeight / 2);

}

File Name: Main.cpp

//Include the header fileData

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>

#include "classes.h"

#include “classes.cpp”

//Using the namespaces

using std::setw;

using namespace std;

void userInteract ();

void pointerTest();

void selection();

void switchCase();

void ArrayElements();

void fileOPerations();

//Function to swap using pass by refference

void swapPassByReference(int *first, int *second)

{

int *temp;

*temp = *first;

* first = *second;

*second = *temp;

}

//function uses a pointer

void pointerTest()

{

int intVar;

cout << " Address of intVar variable: ";

cout << &intVar << endl;

}

void userInteract(){

float input1, input2, p;

cout << "Please Input two numbers: ";

cin >> input1 >> input2;

p = input1*input2;

cout << "Product of your inputs is = " << p;

}

//To use selection statements

void selection()

{

int checkVariable = 100;

if( checkVariable < 20 )

{

cout << "checkVariable is less than 20;" << endl;

}

else

{

cout << "checkVariable is not less than 20;" << endl;

}

cout << "value of checkVariable is : " << checkVariable << endl;

}

//To use array

void ArrayElements()

{

//declare the array

int n[10];

//The for loops

for ( int itr = 0; itr < 10; itr++ )

{

n[itr] = itr + 100;

}

cout << "Element" << setw(13) << "Value is" << endl;

for ( int vi = 0; vi < 10; vi++ )

{

cout << setw( 7 )<< vi << setw( 13 ) << n[ vi ] << endl;

}

}

//Switch for select

void switchCase()

{

char mark = 'D';

switch(mark)

{

case 'A' :

cout << "Excellent!" << endl;

break;

case 'B' :

case 'C' :

cout << "Well done" << endl;

break;

case 'D' :

cout << "You passed" << endl;

break;

case 'F' :

cout << "Failed" << endl;

break;

default :

cout << "Invalid mark" << endl;

}

cout << "Your mark is " << mark << endl;

}

//Read the fileData

void fileOPerations() {

char fileData[100];

ofstream filewrite;

filewrite.open("file.txt");

cout << "File write" << endl;

cout << "Please input your name: ";

cin.getline(fileData, 100);

cin.getline(fileData, 100);

filewrite << fileData << endl;

cout << "Please input your age: ";

cin >> fileData;

cin.ignore();

filewrite << fileData << endl;

filewrite.close();

ifstream infile;

infile.open("file.txt");

cout << "File read" << endl;

infile >> fileData;

cout << fileData << endl;

infile >> fileData;

cout << fileData << endl;

infile.close();

}

//main function

int main() {

int a = 100;

int b = 200;

userInteract();

pointerTest();

//swapPassByReference(&a, &b);

cout<<"Now a ="<<a<<"and b = "<<b<<endl;

selection();

switchCase();

fileOPerations();

//baseShape baseShape(0, 0));

derivedRectangle rec(10,7);

derivedTriangle tri(10,5);

cout<<rec.findArea();

//baseShape = rec;

//baseShape.findArea();

//baseShape = tri;

cout<<tri.findArea();

//cout<<baseShape.findArea();

return 0;

}