View the step-by-step solution to:

Question

NOTE: My question was flagged with the following comment:

"

style="color:rgb(136,136,136);">This wording may indicate that you intend to submit a tutor's work as your own."


THIS PROGRAM IS MY OWN WORK. ALL I ASKED FOR WAS TO A DEBUG A PROBLEM WITH JUST ONE STATEMENT IN MY CODE. IF YOU ARE INCOMPETENT TO DO THAT, THEN YOU SHOULD HAVE SOME DIGNITY AND NOT ACCEPT THE QUESTION INSTEAD OF WRONGLY FLAGGING IT BECAUSE YOU COULDN'T SOLVE IT. I DEBUGGED THE PROBLEM ANYWAYS. HAVE SOME SHAME. IF ANYTHING, I SHOULD BE FLAGGING YOU FOR ABUSING THE SYSTEM."


This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to use getline for that but it doesn't seem to be working. Please debug it and provide an explanation of where I am wrong here.

#include <iostream>

#include <string>

using namespace std;

struct Node

{

  string patientName;

  int SSN, age, smoker, HBP, HFD, points = 0;

  Node* link;

};

Node* head = NULL;

//Function Prototypes

void displayMenu();

void createNewNode(Node* newNode);

void createPatient();

void displayPatient();

void exitProgram();

int main()

{

  int choice;

  while (true)

  {

     displayMenu();

     cout << "Enter a choice from 1 to 3: ";

     cin >> choice;

     switch (choice)

     {

     case 1:

        createPatient();

        break;

     case 2:

        displayPatient();

        break;

     case 3:

        exitProgram();

        break;

     default:

        cout << "Invalid entry. Enter a number between 1 and 7." << endl;

     }

  }

  return 0;

}

void displayMenu()

{

  cout << " Patient Records Manager " << endl << endl;

  cout << "What would you like to do? " << endl << endl;

  cout << "[1] Add a new patient." << endl <<

        "[2] Display Patient data." << endl <<

        "[3] Exit." << endl << endl;

}

void createNewNode(Node *newNode)

{

  //cin.clear();

  cout << "Enter the patient's name: "<< endl;

  getline (cin, newNode->patientName);

  cout << "Enter the last 4 digits of the patient's SSN: ";

  cin >> newNode->SSN;

  cout << "Enter the patient's age: ";

  cin >> newNode->age;

  cout << "Enter patient's smoker status (Type 1 for Smoker and 0 for Non-Smoker): ";

  cin >> newNode->smoker;

  cout << "Enter patient's HBP status (Type 1 for HBP and 0 for NO HBP): ";

  cin >> newNode->HBP;

  cout << "Enter patient's HFD status (Type 1 for HFD and 0 for NO HFD): ";

  cin >> newNode->HFD;

  newNode->link = NULL;

}

void createPatient()

{

  Node* newNode = new Node;

  //If list is empty, create the very 1st Node and point the Head pointer to this 1st node

  if (head == NULL)

  {

     createNewNode(newNode);

     head = newNode;

     newNode->link = NULL;

  }

  else //If list is not empty, create an additional node and insert it at the end of the list

  {

     createNewNode(newNode);

     Node* temp = head;

     while (temp->link != NULL)

        temp = temp->link;

     temp->link = newNode;

     newNode->link = NULL;

  }

  int choice;

  cout << "Would you like to add another patient? (Type 1 for Yes and 0 for No): ";

  cin >> choice;

  if (choice == 1)

     createPatient();

}

void displayPatient()

{

  

}

void exitProgram()

{

  

}

Top Answer

View the full answer
Code running.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question