Question

I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the problem and how you fixed it. Submit the working source code.

// UserMenu_Solution.cpp : This code contains five errors before it will work as desired. Find those errors,
// document a description of the errors and their fix, and fix them. Try using the debugger to
// step through the program to find the bugs. As you step through, take notice of the information
// you can see.

//This program builds a menu based on switchcase statements to determine where a user wants to go in the program.
// Program options are then to solve the Tower of Hanoi problem, view the user profile, or exit the program.

#include "stdafx.h"
#include <cstdlib>
#include <iostream>

void Tower(int, char, char, char);

int main()
{
   int choice;
   cout << "1. Solve the Tower of Hanoi" << endl;
   cout << "2. View Your Profile" << endl;
   cout << "3. Exit" << endl;
   cout << "Enter your choice : " << endl;
   cin >> choice;
   switch (Choice)
   {
       case 1:
           system("cls");
           int numDiscs;

           cout << "**Tower of Hanoi**\n";
           cout << Enter the number of discs : ;
           cin >> numDiscs;
           cout << "\n\n";
           Tower(numDiscs, 'A', 'B', 'C');
           break;
       case :
           cout << "Username:\t\tPlayer 1" << endl;
           cout << "Gamertag:\t\tImTheBest" << endl;
           cout << "No. Hours Played:\t173" << endl;
           break;
       case 3:
           cout << "Now Exiting." << endl;
           break;
       default:
           cout << "You did not choose anything...so exit this program." << endl;
   }

   return 0;
}


void Tower(int numDiscs, char from, char aux, char to){
   if (numDiscs == 1){
       cout << "\tMove disc 1 from " << from << " to " << to << "\n";
       return;
   }
   else{
       tower(numDiscs - 1, from, to, aux);
       cout << "\tMove disc " << numDiscs << " from " << from << " to " << to << "\n";
       Tower(numDiscs - 1, aux, from, to);
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Error 1

The namespace ‘std’ is not declared.

To fix that, we need to add this statement on top, below the header files:

using namespace std;

Error 2

In switch-case statement, the variable ‘Choice’ is incorrect because, variables are case sensitive and all the letters of ‘choice’ are in lower case.

To fix that, we need to change ‘Choice’ to ‘choice’

switch (choice)

    {

Error 3

In switch case statement, inside the case 1, where it is prompting user to enter the number of discs, the output string is not enclosed in double quotes.

To fix this, we need to enclose this string in double quotes

cout << "Enter the number of discs : ";

Error 4

In switch case statement, in second case, the case value is missing.

To fix this, we need to put case value to 2 as we can see the option 2 is about displaying the profile which is the content of case 2.

Error 5

In Tower function, inside the else part, the function name ‘tower’ is incorrect because the function name is also case sensitive and correct name is “Tower”

To fix this, we need to make it “Tower” in place of “tower”.

Tower(numDiscs - 1, from, to, aux);

Complete code after fixing errors, changes are in bold:

#include "stdafx.h"

#include <cstdlib>

#include <iostream>

using namespace std;

void Tower(int, char, char, char);

int main()

{

       int choice;

       cout << "1. Solve the Tower of Hanoi" << endl;

       cout << "2. View Your Profile" << endl;

       cout << "3. Exit" << endl;

       cout << "Enter your choice : " << endl;

       cin >> choice;

       switch (choice)

       {

       case 1:

              system("cls");

              int numDiscs;

              cout << "**Tower of Hanoi**\n";

              cout << "Enter the number of discs : ";

              cin >> numDiscs;

              cout << "\n\n";

              Tower(numDiscs, 'A', 'B', 'C');

              break;

       case 2:

              cout << "Username:\t\tPlayer 1" << endl;

              cout << "Gamertag:\t\tImTheBest" << endl;

              cout << "No. Hours Played:\t173" << endl;

              break;

       case 3:

              cout << "Now Exiting." << endl;

              break;

       default:

              cout << "You did not choose anything...so exit this program." << endl;

       }

       return 0;

}

void Tower(int numDiscs, char from, char aux, char to){

       if (numDiscs == 1){

              cout << "\tMove disc 1 from " << from << " to " << to << "\n";

              return;

       }

       else{

              Tower(numDiscs - 1, from, to, aux);

              cout << "\tMove disc " << numDiscs << " from " << from << " to " << to << "\n";

              Tower(numDiscs - 1, aux, from, to);

       }

}

Below is the sample output that is generated after fixing all errors:

1. Solve the Tower of Hanoi 2. Uiew Your Profile 3. Exit Enter your choice

**Tower of Hanoi** Enter the number of discs : 3 Move disc 1 from A to C Move disc 2 from A to B Move disc 1 from C to B Move

1. Solve the Tower of Hanoi 2. Uiew Your Profile 3. Exit Enter your cho ice 2 Username Gamertag No Hours Played: Player 1 ImT

Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • I need help in my C++ code regarding outputting the enums in string chars. I created...

    I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Could use some help with this, Instructions: You will need to add an input statement for...

    Could use some help with this, Instructions: You will need to add an input statement for the grade variable to this code. Also add a for loop so that you can enter more than one grade. Code the for loop so that at least 7 grades can be entered. #include <iostream> #include <string> using namespace std; void main() {      char grade;           for (int x = 0; x < 7; x++)      {            cout << "Enter a...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

  • IN C++ Help to fix code Library management program. Error is when i type a letter...

    IN C++ Help to fix code Library management program. Error is when i type a letter into options menu the code breaks. after your edits, the code should still run as normal. CODE: #include<iostream> using namespace std; //structure to store library data class BookDetails{ public: struct library { int code; char title[20]; int status; }; library book_details[100]; }; //structure to store user data class UserDetails:public BookDetails{ public: struct users { int id; char name[20]; int booksNumber; }; users user_details[100]; };...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT