Question

In: Computer Science

Find and fix the compile time bugs in the code at the end of this section....

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-------------

//----------------------------------

Solutions

Expert Solution

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:


Related Solutions

Fix the bugs in this matlab program so that it solves. clear clf clc time =...
Fix the bugs in this matlab program so that it solves. clear clf clc time = linspace(0, 5, 100); m = 1; k = 100; c = 1; delta = 0.2; [period, response] = Exmp(m, k, c, delta, time); plot(time, response) grid %Exmp(m, k, c, delta, time) % %________________________________________ function [T, x] = Exmp(m, k, c, delta, t) omega = sqrt(k/m); cC = 2*m*omega; if c>= cC disp('Not an underdamped system') T = 0; x = 0; return; end %...
In this python script find any bugs and errors which would cause the code to crash....
In this python script find any bugs and errors which would cause the code to crash. The code must be re-written correctly. After debugging make a separate list of all the errors you found in the script. contacts_list=[] # global variable, list of contacts_list, one string per contact def pause()     """ pauses program e.g. to view data or message """     input("press enter to continue") def load():     """ populate list with data """          contacts_list.append(('Milo ', '063847489373'))...
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
/* What's wrong with this code? (nothing actually - but let's fix it to use a...
/* What's wrong with this code? (nothing actually - but let's fix it to use a repetition structure) Copy and paste this .txt file into jGrasp, then save it, compile it, run it. Modify it! Bring your code to class at the start of Week 8, with at least 2 output runs with different data. What the program should do is take orders(input) for three (3) octoberfest guests, using a "for" loop. (+1 pt.) Each guest will choose 1 or...
The time it takes for a computer program to compile can be modeled with a Gamma...
The time it takes for a computer program to compile can be modeled with a Gamma distribution with a mean of 1 minute and a variance of 0.5 minutes^2. Find the probability that it takes more than 1 minute for a program to compile.
Need a java code A bug collector collects bugs every day for 7 days. Write a...
Need a java code A bug collector collects bugs every day for 7 days. Write a program that keeps a running total of the number of bugs collected during the 7 days. The program should prompt the user to enter the number of bugs collected for each day. Finally, when the program is finished, the program should display the total number of bugs collected.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT