In: Computer Science
Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here. This assignment is meant to test your attention to detail and strengthen your debugging skills.
Here is the code.
// Week 4 Assignment-1
// Description: Compile time errors
//----------------------------------
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <fstream> // provides access to file commands
#include <string> // provides access to string commands
#include <vectors> // provides access to std::vector
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// create and initialize variables
string myTextString;
string myFilename;
vector <string> myStrVector;
ifstream inFile;
ofstream outFile;
cout << "enter a file name (without an extension): " << endl;
getline(cin, myFilename);
myFilename += ".txt";
// open an output file
outfile.open(myFilename);
// write to a file
for(int i = 0; i < 3;i++)
{
cout >> "enter a line of text: ";
getline(cin, myTextString);
outFile << myTextString << endl;
}
// close the file
outFile.close();
// open an input file
inFile.open(myFilename);
// read from the file
while (getline(inFile,myTextString))
{
myStrVector.push_back(myTextString);
}
inFile.close();
// use a range-based for loop with a switch statement
for(auto s: myStrVector)
{
cout << s << endl;
char switchFlag = s[0];
switch (switchFlag)
{
case "a":
cout << "Hey, a vowel. The 'a' vowel actually." << endl;
break;
case 'e':
cout << "See vowel. See vowel run. run vowel, run. The 'e' vowel." << endl;
break;
case 'i':
cout << "I know. It's a vowel. The 'i' vowel." << endl;
break;
case 'o':
cout << "Oh! Don't you know, it's the 'o' vowel." << endl;
break;
case 'u':
cout << "Whew! We got a you. Actually, the 'u' vowel." << endl;
break;
case 's':
cout << "Oh great! I love 's's. More 's's, please." << endl;
break;
default
cout << "Nothing interesting here. I would really like to see an 's'." << endl;
break;
}
}
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
Here is compiled code:
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <fstream> // provides access to file commands
#include <string> // provides access to string commands
#include <vector> // provides access to std::vector
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
//--end of global constants---------
//----------------------------------
//**begin main program**************
int main()
{
// create and initialize variables
string myTextString;
string myFilename;
vector <string> myStrVector;
ifstream inFile;
ofstream outFile;
cout << "enter a file name (without an extension): " <<
endl;
getline(cin, myFilename);
myFilename += ".txt";
// open an output file
outFile.open(myFilename.c_str());
// write to a file
for(int i = 0; i < 3;i++)
{
cout << "enter a line of text: ";
getline(cin, myTextString);
outFile << myTextString << endl;
}
// close the file
outFile.close();
// open an input file
inFile.open(myFilename.c_str());
// read from the file
while (getline(inFile,myTextString))
{
myStrVector.push_back(myTextString);
}
inFile.close();
// use a range-based for loop with a switch statement
for(int i = 0 ; i < myStrVector.size() ; i++)
{
string s = myStrVector.at(i);
cout << s << endl;
char switchFlag = s[0];
switch (switchFlag)
{
case 'a':
cout << "Hey, a vowel. The 'a' vowel actually." <<
endl;
break;
case 'e':
cout << "See vowel. See vowel run. run vowel, run. The 'e'
vowel." << endl;
break;
case 'i':
cout << "I know. It's a vowel. The 'i' vowel." <<
endl;
break;
case 'o':
cout << "Oh! Don't you know, it's the 'o' vowel." <<
endl;
break;
case 'u':
cout << "Whew! We got a you. Actually, the 'u' vowel."
<< endl;
break;
case 's':
cout << "Oh great! I love 's's. More 's's, please." <<
endl;
break;
default:
cout << "Nothing interesting here. I would really like to see
an 's'." << endl;
break;
}
}
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
Output: