Answered Essay: This code has errors in it and I need to fix them so that it runs properly

Need help debugging a code

This code has errors in it and I need to fix them so that it runs properly

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

int choice;

int amt;

 

do {

cout << “Make a choice from the following menu:” << endl;

cout << ”   1. Add a user” << endl;

cout << ”   2. Delete a user” << endl;

cout << ”   3. Modify a user” << endl;

cout << ”   4. View a user” << endl;

cout << ” 5. Contact a user” << endl;

cin >> choice;

// I want to keep looping if the choice is outside of 1-5,

} while (choice < 0 && choice > 5);

switch (choice) {

case 1: {

cout << “You chose ‘add a user'” << endl;

}

case 2: {

cout << “You chose ‘delete a user'” << endl;

}

case 3: {

cout << “You chose ‘modify a user'” << endl;

}

case 4: {

cout << “You chose ‘view a user'” << endl;

}

case 5: {

cout << “How many users? “;

cin >> amt;

cout << “Enter users:” << endl;

//I want to grab a name for each of the users that

//the user gives me and output it with “Contacting <username>”

for (int i = 0;i <= amt;i++); {

string name;

cin >> name;

cout << “Contacting ” << name << endl;

}

break;

}

default: {

cout << “Unknown choice: ” << choice << endl;

break;

}

}

 

return 0;

}

Expert Answer

 

Explanation::

  • Debugged code in c++ is provided below.
  • Wherever changes are made, comments are provided.
  • Detailed explanation is given about changes and cause of logical errors.
  • Please read all the comments for better understanding of the changes made in code.

Truth Table of AND and OR::

Note:: Need for truth table is provided in code comments. It is used to explain why do-while loop was not looping if user entered wrong input like choice=-1

Code in C++::

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice;
int amt;

do {
cout << “Make a choice from the following menu:” << endl;
cout << ” 1. Add a user” << endl;
cout << ” 2. Delete a user” << endl;
cout << ” 3. Modify a user” << endl;
cout << ” 4. View a user” << endl;
cout << ” 5. Contact a user” << endl;
cin >> choice;
/*
* Now to loop again and again if user enters wrong input then
* replace &&(AND) condition TO ||(OR) condition.
* For example suppose choice=-1 which is wrong,
* while() condition checks first choice<0 -> true
* but choice > 5 -> false . So if && is used then this
* while loop will be false and do-while loop gets
* terminated.
* See images of truth table provided in explanation section.
* So I have replaced && with ||.
* Now for OR at least one parameter has to be true.
* choice < 0 is true so while condition is also true.
*/
} while (choice < 0 || choice > 5);

/*
* switch cases need to have break statements for each of its cases.
* If there are no break statement then all the cases will be
* executed that are without break statement.
* So this is the big change done below.
*/
switch (choice) {
case 1:
cout << “You chose ‘add a user'” << endl;
break;

case 2:
cout << “You chose ‘delete a user'” << endl;
break;
case 3:
cout << “You chose ‘modify a user'” << endl;
break;
case 4:
cout << “You chose ‘view a user'” << endl;
break;
case 5:
cout << “How many users? “;
cin >> amt;
cout << “Enter users:” << endl;
/*
* In for loop initially it was scanning only one name.
* Because there was a semicolon at end of for loop statement as
* for(int i=0;i<=amt;i++);{}
* So i have removed that semicolon.
*/

/*
* Another change made is integer amt stores number of
* names to be taken as input.
* So for loop start with i=0 and suppose amt=5
* Then for loop has to run from i=0 to i=4(both inclusive)
* i.e 0<=i<amt
*/
for (int i = 0;i<amt;i++){
string name;
cin >> name;
cout << “Contacting ” << name << endl;
}
break;

default:
cout << “Unknown choice: ” << choice << endl;
break;
}//switch case ends here

return 0;
}

Output::

Test Case 1::

Test Case 2::

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.