Question
Find and fix the compile time bugs in the code at the end of this section. Here is the code: // Week 2 Assignment-1 //
Find and fix the compile time bugs in the code at the end of this section. Here is the code:
// Week 2 Assignment-1
// Description: Compile time errors in C Strings, Arrays, std:string,
// struct, enum, pointers, and std::array.
//----------------------------------
//**begin #include files************
#include
#include <> // provides access to std:array
#include
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
const int arraySize = 4; // **there is a subtle bug here (needs "const")
enum MyEnum // Needs to be before the struct that uses it
{
Dog, Cat, Fish, Squirrel
};
struct MyStruct
{
int a;
float b;
string c;
MyEnum d;
};
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// Initialization
char myCString[arraySize] = {0};
char myOtherCString[] = {'Yet another string'};
int myInt[3] = {27, 39, 0, 42};
string myString;
MyStruct aStruct = { 4,3.5,Dog ,"Dogs"};
int x;
int * pX = x;
array
// Storing values in uninitialized variables
myCString[0] = "A";
myString = "A third string";
x = 4;
for (int i = 0; i { Animals[i].a = rand()%10; Animals[i].b = rand()%100/100.0; Animals[i].c = MyEnum(rand()%4); cout << "Enter a name: "; getline(cin, Animals[i].d); } // Display the data cout << "myCString = " << myCString << endl; cout << "myOtherCString = " << myOtherCString << endl; for (int i = 0; i < 4; i++) { cout << "myInt[" << i << "] = " << myInt[i] << endl; } cout << "aStruct data: a = " << aStruct.a << " b = " << aStruct.b << " c = " << aStruct.c << " d = " << aStruct.d << endl; cout << "x = " << x << endl; cout << "value pointed to by pX = " << *pX << endl; for (int i = 0; i< arraySize;i++) { cout << "Animals[" << i << "].a = " << Animals[i].a << endl; cout << "Animals[" << i << "].b = " << Animals[i].b << endl; cout << "Animals[" << i << "].c = " << Animals[i].c << endl; cout << "Animals[" << i << "].d = " << Animals[i].d << endl; } // Wait for user input to close program when debugging. cin.get(); return 0; } //--end of main program------------- //----------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started