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()
{
}
//debugged c++ code (use cin.ignore())
#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;
}
if (choice == 3)
break;
}//while ends here
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.ignore();
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()
{
if (head == NULL)
cout << "No reecord...."
<< endl;
else
{
Node* temp = head;
while (temp !=NULL)
{
cout <<
"Patient's Name: "<<temp->patientName<<"\nAge:
"<<temp->age<<"\nSSN:
"<<temp->SSN<<endl;
temp =
temp->link;
}
}
}
void exitProgram()
{
cout << "Thanks for using this
app.\nExisting....\n";
exit(0);
}
//Output

//If you need any help regarding this solution ......... please leave a comment ..... thanks
This is just a partial C++ code. I am having a problem with the getline(cin, variable)...
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);...
Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it. Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...
I have a C++ code that lets me enter, display and delete a
student record. I need to implement a function that prints the
average grade score of the students I input.
Below is my code and a picture of how my code looks right
now.
#include<iostream>
#include<stdlib.h>
using namespace std;
//Node Declaration
struct node
{
string name;
string id;
int score;
node *next;
};
//List class
class list
{
private:
//head...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be: H create link at head R remove link at head T create link at tail K remove link at tail I remove link at ID S search...
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...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink { string name; double cost; int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() { const int size = 5; Drink drinks[size] = { {"Cola", 0.65, 2}, {"Root Beer", 0.70, 1}, {"Grape Soda", 0.75, 5}, {"Lemon-Lime", 0.85, 20}, {"Water", 0.90, 20} }; cout <<...
How would you get this lab to work with the numbers 37 14 68
47, the book we are using is Data structures using C++ by D.S.
Malik. Please I need help? Just keeps repeating the same
question.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*&
last);
void printList(nodeType*& first);
void insertFront(nodeType*& first);
void insertBack(nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
int main()
{
nodeType...
Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp ○This will contain your node struct○value is type int ○next is type Node* ○(optional) prev is type Node* nodeFunctions.hpp ○This will contain the headers for all the functions you need to display, add, and remove nodes from your list ■displayList displays the entire list from beginning to end. If no elements let the user know the list is...