how to fix abort() in c++ in visual studio - c++

im fairly new to c++ how can i fix my code to prevent abort() being called. i know the error came from the void readCsvFile(), but i have no idea how to fix it.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int sizeofLinkedList = 0;
struct Car
{
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
Car* nextAddress;
Car* prevAddress;
} *head, * tail;
Car* CreateNewNnode(int id, string title, int price, int year, int mileage, string fuel_type, string transmission,
string engine_size, int doors, string colour, string body_type, string url, string sale_date) //create a newnode - use for any insert
{
Car* newnode = new Car;
newnode->id = id;
newnode->title = title;
newnode->price = price;
newnode->year = year;
newnode->mileage = mileage;
newnode->fuel_type = fuel_type;
newnode->transmission = transmission;
newnode->engine_size = engine_size;
newnode->doors = doors;
newnode->colour = colour;
newnode->body_type = body_type;
newnode->url = url;
newnode->sale_date = sale_date;
newnode->nextAddress = NULL;
newnode->prevAddress = NULL;
return newnode;
}
// Quick sort algorithm for linked list
Car* partition(Car* head, Car* end, Car** newHead, Car** newEnd)
{
Car* pivot = end;
Car* prev = NULL, * cur = head, * tail = pivot;
while (cur != pivot)
{
if (cur->id < pivot->id)
{
if ((*newHead) == NULL)
(*newHead) = cur;
prev = cur;
cur = cur->nextAddress;
}
else
{
if (prev)
prev->nextAddress = cur->nextAddress;
Car* tmp = cur->nextAddress;
cur->nextAddress = NULL;
tail->nextAddress = cur;
tail = cur;
cur = tmp;
}
}
if ((*newHead) == NULL)
(*newHead) = pivot;
(*newEnd) = tail;
return pivot;
}
Car* getTail(Car* cur)
{
while (cur != NULL && cur->nextAddress != NULL)
cur = cur->nextAddress;
return cur;
}
Car* quickSortRecur(Car* head, Car* end)
{
if (!head || head == end)
return head;
Car* newHead = NULL, * newEnd = NULL;
Car* pivot = partition(head, end, &newHead, &newEnd);
if (newHead != pivot)
{
Car* tmp = newHead;
while (tmp->nextAddress != pivot)
tmp = tmp->nextAddress;
tmp->nextAddress = NULL;
newHead = quickSortRecur(newHead, tmp);
tmp = getTail(newHead);
tmp->nextAddress = pivot;
}
pivot->nextAddress = quickSortRecur(pivot->nextAddress, newEnd);
return newHead;
}
void quickSort(Car** headRef)
{
(*headRef) = quickSortRecur(*headRef, getTail(*headRef));
}
// Modified insertIntoASortedList to use quicksort
void insertIntoASortedList(Car* newnode)
{
// Insert new node at the end of the list
if (head == NULL)
{
head = tail = newnode;
}
else
{
tail->nextAddress = newnode;
newnode->prevAddress = tail;
tail = newnode;
}
// Sort the list using quicksort
quickSort(&head);
}
void readCsvFile() {
ifstream file("carlist(1).csv");
string line;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price = 0;
int year;
int mileage;
int doors;
while (getline(file, line)) {
stringstream ss(line);
string tmp;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
getline(ss, tmp, ',');
id = stoi(tmp);
getline(ss, title, ',');
getline(ss, tmp, ',');
price = stoi(tmp);
getline(ss, tmp, ',');
year = stoi(tmp);
getline(ss, tmp, ',');
mileage = stoi(tmp);
getline(ss, fuel_type, ',');
getline(ss, transmission, ',');
getline(ss, engine_size, ',');
getline(ss, tmp, ',');
doors = stoi(tmp);
getline(ss, colour, ',');
getline(ss, body_type, ',');
getline(ss, url, ',');
getline(ss, sale_date, ',');
Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
engine_size, doors, colour, body_type, url, sale_date);
insertIntoASortedList(newnode);
}
}
void SearchBasedOnPrice(int price1, int price2)
{
Car* current = head;
while (current != NULL)
{
if (current->price >= price1 && current->price <= price2)
{
cout << current->id << ". " << current->title << " - " << current->price << " - "
<< current->year << " - " << current->mileage << " - " << current->fuel_type << " - "
<< current->transmission << " - " << current->engine_size << " - " << current->doors << " - "
<< current->colour << " - " << current->body_type << " - " << current->url << " - "
<< current->sale_date << endl;
}
current = current->nextAddress;
}
cout << "List ended here!" << endl;
}
int main()
{
head = NULL;
srand(time(0));
int noOfcar, choice = 1;
int CarID, Year;
string Brand, Type, color;
int p1;
int p2;
cout << "Enter your searching p1: ";
cin >> p1;
cout << "Enter your searching p2: ";
cin >> p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout << endl;
int answer; string word;
cout << "Do you want to search anything from the list or not? 1 - Yes, 0 - No: ";
cin >> answer;
cin.ignore();
while (answer == 1)
{
cout << "Enter your searching p1: ";
cin >> p1;
cout << "Enter your searching p2: ";
cin >> p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout << "Do you want to edit anything? 1 - Yes, 0 - No: ";
cin >> answer;
int CarID;
if (answer == 1)
{
cout << "Enter your car ID: ";
cin >> CarID;
}
cout << "Do you still want to search anything from the list or not? 1 - Yes, 0 - No: ";
cin >> answer;
cin.ignore();
system("pause");
system("cls");
}
return 0;
}
only when i only use getline(ss, tmp, ',');
id = stoi(tmp);, the code can be run, but the rest of the output will be incorrect, but when i use the full code above, the abort() will be called. so how can i fix it
void readCsvFile() {
ifstream file("carlist(1).csv");
string line;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price = 0;
int year;
int mileage;
int doors;
while (getline(file, line)) {
stringstream ss(line);
string tmp;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
getline(ss, tmp, ',');
id = stoi(tmp);
getline(ss, title, ',');
ss >> price;
ss >> year;
getline(ss, tmp, ',');
mileage = stoi(tmp);
getline(ss, fuel_type, ',');
getline(ss, transmission, ',');
getline(ss, engine_size, ',');
ss >> doors;
getline(ss, colour, ',');
getline(ss, body_type, ',');
getline(ss, url, ',');
getline(ss, sale_date, ',');
Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
engine_size, doors, colour, body_type, url, sale_date);
insertIntoASortedList(newnode);
}
}
void readCsvFile() {
ifstream file("carlist(1).csv");
string line;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price = 0;
int year;
int mileage;
int doors;
while (getline(file, line)) {
stringstream ss(line);
string tmp;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
getline(ss, tmp, ',');
id = stoi(tmp);
getline(ss, title, ',');
getline(ss, tmp, ',');
price = stoi(tmp);
getline(ss, tmp, ',');
year = stoi(tmp);
getline(ss, tmp, ',');
mileage = stoi(tmp);
getline(ss, fuel_type, ',');
getline(ss, transmission, ',');
getline(ss, engine_size, ',');
getline(ss, tmp, ',');
doors = stoi(tmp);
getline(ss, colour, ',');
getline(ss, body_type, ',');
getline(ss, url, ',');
getline(ss, sale_date, ',');
Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
engine_size, doors, colour, body_type, url, sale_date);
insertIntoASortedList(newnode);
}
}

Related

how to read data from csv file by using quick sort and display it

Can I know what is the best possible way to solve this issue, as I'm currently new to C++
The result that I expected was something like this
id, title, price, date, mileage, fuel_type, transmission, engine_size, doors, colour, body_type, url, sale_date
3 ,Volkswagen Golf 1.9 TDI 11 Months MOT ,650 ,2000 ,155822 Diesel ,Manual ,1.9 ,4 ,Silver ,Hatchback ,https://www.ebay.co.uk/itm/124907646705?hash=item1d15136ef1:g:8dsAAOSwb-thRawR ,25 Sep 2021
4 ,1999 Volkswagen Golf GTi 1.8 turbo AUM engine V20 British racing green 3 door ,650 ,1999 ,178000 ,Petrol ,Manual ,1.8 ,3 ,Green ,Hatchback ,https://www.ebay.co.uk/itm/114998243091?hash=item1ac66def13:g:7y4AAOSwSEdhL~1m ,25 Sep 2021
But the result I got is like this
3. - 0 - -858993460 - -858993460 - - - - -858993460 - - - -
4. - 1999 - 0 - -858993460 - - - - -858993460 - - - -
5. - 0 - 0 - -858993460 - - - - -858993460 - - - -
6. - 5 - 0 - -858993460 - - - - -858993460 - - - -
I have tried a lot of different way to do it, but I'm still struggling to find a way to print all the input from the csv file properly. As it can read according to the id, yet it can't print all the data from the row specifically.
This is my code
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int sizeofLinkedList = 0;
struct Car
{
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
Car* nextAddress;
Car* prevAddress;
} *head, * tail;
Car* CreateNewNnode(int id, string title, int price, int year, int mileage ,string fuel_type, string transmission,
string engine_size, int doors, string colour, string body_type, string url, string sale_date) //create a newnode - use for any insert
{
Car* newnode = new Car;
newnode->id = id;
newnode->title = title;
newnode->price = price;
newnode->year = year;
newnode->mileage = mileage;
newnode->fuel_type = fuel_type;
newnode->transmission = transmission;
newnode->engine_size = engine_size;
newnode->doors = doors;
newnode->colour = colour;
newnode->body_type = body_type;
newnode->url = url;
newnode->sale_date = sale_date;
newnode->nextAddress = NULL;
newnode->prevAddress = NULL;
return newnode;
}
Car* partition(Car* head, Car* end, Car** newHead, Car** newEnd)
{
Car* pivot = end;
Car* prev = NULL, * cur = head, * tail = pivot;
while (cur != pivot)
{
if (cur->id < pivot->id)
{
if ((*newHead) == NULL)
(*newHead) = cur;
prev = cur;
cur = cur->nextAddress;
}
else
{
if (prev)
prev->nextAddress = cur->nextAddress;
Car* tmp = cur->nextAddress;
cur->nextAddress = NULL;
tail->nextAddress = cur;
tail = cur;
cur = tmp;
}
}
if ((*newHead) == NULL)
(*newHead) = pivot;
(*newEnd) = tail;
return pivot;
}
Car* getTail(Car* cur)
{
while (cur != NULL && cur->nextAddress != NULL)
cur = cur->nextAddress;
return cur;
}
Car* quickSortRecur(Car* head, Car* end)
{
if (!head || head == end)
return head;
Car* newHead = NULL, * newEnd = NULL;
Car* pivot = partition(head, end, &newHead, &newEnd);
if (newHead != pivot)
{
Car* tmp = newHead;
while (tmp->nextAddress != pivot)
tmp = tmp->nextAddress;
tmp->nextAddress = NULL;
newHead = quickSortRecur(newHead, tmp);
tmp = getTail(newHead);
tmp->nextAddress = pivot;
}
pivot->nextAddress = quickSortRecur(pivot->nextAddress, newEnd);
return newHead;
}
void quickSort(Car** headRef)
{
(*headRef) = quickSortRecur(*headRef, getTail(*headRef));
}
// Modified insertIntoASortedList to use quicksort
void insertIntoASortedList(Car* newnode)
{
// Insert new node at the end of the list
if (head == NULL)
{
head = tail = newnode;
}
else
{
tail->nextAddress = newnode;
newnode->prevAddress = tail;
tail = newnode;
}
// Sort the list using quicksort
quickSort(&head);
}
void readCsvFile() {
ifstream file("carlist(1).csv");
string line;
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
while (getline(file, line)) {
stringstream ss(line);
int id;
string title, fuel_type, transmission, engine_size, colour, body_type, url, sale_date;
int price;
int year;
int mileage;
int doors;
ss >> id;
getline(ss, title, ',');
ss >> price;
ss >> year;
ss >> mileage;
getline(ss, fuel_type, ',');
getline(ss, transmission, ',');
getline(ss, engine_size, ',');
ss >> doors;
getline(ss, colour, ',');
getline(ss, body_type, ',');
getline(ss, url, ',');
getline(ss, sale_date, ',');
Car* newnode = CreateNewNnode(id, title, price, year, mileage, fuel_type, transmission,
engine_size, doors, colour, body_type, url, sale_date);
insertIntoASortedList(newnode);
}
}
void SearchBasedOnPrice(int price1, int price2)
{
Car* current = head;
while (current != NULL)
{
if (current->id >= price1 && current->id <= price2)
{
cout << current->id << ". " << current->title << " - " << current->price << " - "
<< current->year << " - " << current->mileage << " - " << current->fuel_type << " - "
<< current->transmission << " - " << current->engine_size << " - " << current->doors << " - "
<< current->colour << " - " << current->body_type << " - " << current->url << " - "
<< current->sale_date << endl;
}
current = current->nextAddress;
}
cout << "List ended here!" << endl;
}
int main()
{
head = NULL;
srand(time(0));
int noOfcar, choice = 1;
int CarID, Year;
string Brand, Type, color;
int p1;
int p2;
cout << "Enter your searching p1: ";
cin >> p1;
cout << "Enter your searching p2: ";
cin >> p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout << endl;
int answer; string word;
cout << "Do you want to search anything from the list or not? 1 - Yes, 0 - No: ";
cin >> answer;
cin.ignore();
while (answer == 1)
{
cout << "Enter your searching p1: ";
cin >> p1;
cout << "Enter your searching p2: ";
cin >> p2;
readCsvFile();
SearchBasedOnPrice(p1, p2);
cout << "Do you want to edit anything? 1 - Yes, 0 - No: ";
cin >> answer;
int CarID;
if (answer == 1)
{
cout << "Enter your car ID: ";
cin >> CarID;
}
cout << "Do you still want to search anything from the list or not? 1 - Yes, 0 - No: ";
cin >> answer;
cin.ignore();
system("pause");
system("cls");
}
return 0;
}
This code is incorrect
ss >> id;
getline(ss, title, ',');
ss >> price;
ss >> year;
ss >> mileage;
Think about this format id,title,price,year,mileage etc. There are four commas there but the above code only reads one comma, so it cannot be right.
Here's some code that should work better
string tmp;
getline(ss, tmp, ',');
id = stoi(tmp);
getline(ss, title, ',');
getline(ss, tmp, ',');
price = stoi(tmp);
getline(ss, tmp, ',');
year = stoi(tmp);
getline(ss, tmp, ',');
mileage = stoi(tmp);
This code reads all the commas, but where an integer is required it reads the data into a string tmp and then uses stoi to convert the string into an integer.

Vectors of Pointers passed into different functions

I'm trying to practice with the usage of the new operator to create objects. I'm having some trouble with adjusting my code to manage the new pointers to the objects that I created.
Here's my code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
struct StudentRecord {
public:
StudentRecord(
string id,
string firstName,
string lastName,
int age,
string phoneNumber,
double gpa
) {
Id = id;
FirstName = firstName;
LastName = lastName;
PhoneNumber = phoneNumber;
Age = age;
Gpa = gpa;
}
void display() {
cout << " Student ID: " << Id << endl;
cout << " First Name: " << FirstName << endl;
cout << " Last Name: " << LastName << endl;
cout << " Phone Number: " << PhoneNumber << endl;
cout << " Age: " << Age << endl;
cout << " GPA: " << Gpa << endl;
cout << endl;
}
string Id;
string FirstName;
string LastName;
string PhoneNumber;
int Age;
double Gpa;
};
void displayStudents(vector<StudentRecord>& students) {
for (auto student : students) {
student.display();
}
}
int main()
{
ifstream inputFile;
inputFile.open("TestFile.csv");
string line = "";
vector<StudentRecord> students;
while (getline(inputFile, line)) {
stringstream inputString(line);
//StudentId, Last Name, FirstName, Age, Phone Number, GPA
string studentId;
string lastName;
string firstName;
int age;
string phone;
double gpa;
string tempString;
getline(inputString, studentId, ',');
getline(inputString, lastName, ',');
getline(inputString, firstName, ',');
getline(inputString, tempString, ',');
age = atoi(tempString.c_str());
getline(inputString, phone, ',');
getline(inputString, tempString);
gpa = atof(tempString.c_str());
students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));
line = "";
}
displayStudents(students);
}
Specifically there's an issue here:
students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));
I know that I need to adjust the displayStudents function to take in a vector of pointers to the object, but I'm not sure how to do this.
students is of type vector<StudentRecord>, therefore, you don't need to call new.
If you wanted to practice new, suggest changing it to vector<StudentRecord*>. Note that it's recommended to use a managed pointer type (e.g. unique_ptr, smart_ptr) instead of 'naked' pointers.
If you'd simply like to make the code work, use:
students.emplace_back(studentId, lastName,firstName, age, phone, gpa));
In either case, you might consider std::move() on locally-generated strings that you only use here, i.e., std::move(studentId), std::move(lastName), ... in the arguments of emplace_back() or push_back().

Reading from files with ':' between each variable. C++

Hey so I have this text file:
1:Vegetarian Dishes:Lettuce:1.99
2:Vegetarian Dishes:Tomato:0.99
3:Drinks:Water:0.5
4:Meat Dishes:Burger:4.99
5:Fish Dishes:Fishes:5
6:Drinks:Coka cola:2.5
7:Drinks:Fanta:1.2
8:Drinks:Absolut:12.99
9:Drinks:Wiskey:9.99
10:Vegetarian Dishes:Lemon:0.99
11:Vegetarian Dishes:Green:2.99
12:Meat Dishes:Pizza:4.99
13:Fish Dishes:Fishes2:5.99
14:Drinks:Milk shake:4.99
15:Vegetarian Dishes:Vegan Sandwich:3.99
and i have objects of Dish class, so I want to read the file and add each dish to a Dish object.
Then i have an array of Dishes. Its supposed to store _itemNo:category:description:price.
Below is my code.
The problem is that it doesn't read the file correctly. Do you see any problems?
Code:
Dish::Dish() { }
Dish::Dish(int itemNo, string category, string description,
double price): _itemNo(itemNo), _category(category),
_description(description), _price(price) { }
void Dish::displayDish(void){
cout << setw(15) << left << _itemNo << setw(30) << left << _category
<< setw(45) << left << _description << _price << endl;
}
DishDb::DishDb(): _nElems(0) { }
void DishDb::addDish(int itemNo, string category, string description,
double price){
_menu[_nElems] = Dish(itemNo, category, description, price);
_nElems++;
}
void DishDb::display(){
int i;
cout <<setw(40) << "MENU" << endl << endl;
cout << "Item No" << setw(15) << "Category" << setw(30)
<< "Description" << setw(40)<< "Price" << endl << endl;
for(i = 0; i < _nElems; i++){
_menu[i].displayDish();
}
}
int main(){
/*Select file name for the bills to be stored in*/
string transFilename;
cout << "Enter today's transaction file name: ";
cin >> transFilename;
/*Load the menu*/
/*Adding each dish object into the _nenu array*/
/*Couldn't make this part object oriented*/
DishDb ddb;
string fileName;
int itemNo;
double price;
string description;
string category;
int i;
int numOfDishes = 0;
cout << "Enter file name: ";
cin >> fileName;
ifstream inFile(fileName);
while(i<15){
inFile >> itemNo;
getline(inFile, category, ':');
getline(inFile, description, ':');
inFile >> price;
numOfDishes++;
Dish(itemNo, category, description, price);
ddb.addDish(itemNo, category, description, price);
i++;
}
ddb.display();
inFile.close();
/* while(inFile >> itemNo && getline(inFile, category, ':') &&
getline(inFile, description, ':') && inFile >> price){
numOfDishes++;
Dish(itemNo, category, description, price);
ddb.addDish(itemNo, category, description, price);
}
*/
cout << endl << "Menu was loaded.";
return 0;
}
I've tried 2 ways of reading the file. With the first i was just checking if the file was beging read correctly but its not.
The following code should work for you:
int main() {
std::ifstream file("dishes.txt");
std::string temp, snum, category, description, sprice;
while (std::getline(file, snum, ':') &&
std::getline(file, category, ':') &&
std::getline(file, description, ':') &&
std::getline(file, sprice)) {
// Do whatever you want with:
// * snum (must convert to integer)
// * category
// * description
// * sprice (must convert to float)
}
}

How to determine if name and lname is not in file

The program, is basically sussposed to iterate a file containing the elements
Harry Keeling (202)806-4830
Frank James (301)123-3459
Arthur Paul (202)865-9090
Todd Shurn (410)560-8909
Richard Okpala 202 388 410
what my current program is outputting the phonenumber if firstname and lastname is present in file but if its nothow do i output phone number isnt here my current code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup_name(ifstream&, string&, string&, string&); // prototype
int main()
{
ifstream myfile;
string name, lastname, phonenumber;
char choice;
do
{
myfile.open("infile.txt");
cout << "What is the First name? " << endl;
cin >> name;
cout << "what is your last name?" << endl;
cin >> lastname;
lookup_name(myfile, name, lastname, phonenumber);
cout << "Do you want to lookup another name<Y/N" << endl;
cin >> choice;
} while (choice == 'Y');
return 0;
}
void lookup_name(ifstream& myfile, string& name, string& lastname, string& phonenumber)
{
string name1, name2, fullname, secondname, dummy;
for (int i = 0; i < 5; i++) {
myfile >> name1 >> name2;
fullname = name1 + name2;
secondname = name + lastname;
if (fullname == secondname) {
myfile >> phonenumber;
cout << phonenumber << endl;
myfile.close();
break;
}
else if (fullname != secondname) {
myfile >> dummy;
phonenumber = dummy;
}
Just return whether you found it or not from your lookup function
int lookup_name(ifstream& myfile, string& name, string& lastname, string& phonenumber)
{
string name1, name2, fullname, secondname, dummy;
for (int i = 0; i < 5; i++) {
myfile >> name1 >> name2;
fullname = name1 + name2;
secondname = name + lastname;
if (fullname == secondname) {
myfile >> phonenumber;
cout << phonenumber << endl;
myfile.close();
return 1;
}
else if (fullname != secondname) {
myfile >> dummy;
phonenumber = dummy;
}
}
return 0;
}
Then use the return value
if ( ! lookup_name(myfile, name, lastname, phonenumber) ) {
cout << "Nope, didn't find it!" << endl;
}

Program skips over std::getline [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 6 years ago.
I have to design a program to allow the user to input information for 5 nodes in a doubly linked list and then sort the doubly linked list alphabetically. It skips over allowing the user to input the address, doesn't print any information besides the name ,and doesn't sort the list alphabetically. What am I doing wrong?
#include <iostream>
#include <string>
struct node
{
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
node *next;
node *prev;
};
bool isEmpty(node *head);
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void searchFor(node *&last, std::string lastName);
void showList(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
temp->next = NULL;
temp->prev = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
if(isEmpty(head))
insertAsFirstElement(head, last, firstName, lastName, address, phoneNum);
else
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
int result = lastName.compare(last->lastName);
if (result < 0)
{
temp->next = head;
head->prev = temp;
temp->prev = NULL;
head = temp;
}
else if ( result > 0)
{
temp->next = NULL;
temp->prev = last;
last->next = temp;
last = temp;
}
}
}
void searchFor(node *&last, std::string findName)
{
node *temp = last;
while (temp != NULL)
{
if (temp->lastName == findName)
{
std::cout << temp->firstName << " " << temp->lastName << " " << temp->address << " " << temp->phoneNum << std::endl;;
}
temp = temp->prev;
}
}
void showList(node *current)
{
if(isEmpty(current))
std::cout << "The list is empty\n";
else
{
std::cout << "The list contains: \n";
while(current != NULL)
{
std::cout << current->firstName << " " << current->lastName << " " << current->address << " " << current->phoneNum << std::endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":\n";
std::cin >> firstName;
std::cout << "Enter the last name of person #" << count << ":\n";
std::cin >> lastName;
std::cout << "Enter the address of person #" << count << ":\n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":\n";
std::cin >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?\n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}
Your issue is that you are mixing cin >> and getline which is problematic in C++ due to trailing newlines and what not.
Good practice is to always use getline and then use a stringstream to split the line up into tokens. For example, I modified your solution to only use getline and string streams (NOTE: You need to #include <sstream> at the top of your file.
You can read more about the issues with mixing cin >> and getline and also other ways to solve them here.
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
std::string phoneNumStr;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":\n";
std::getline(std::cin,firstName); // no use of cin >>
std::cout << "Enter the last name of person #" << count << ":\n";
std::getline(std::cin,lastName); // no use of cin >>
std::cout << "Enter the address of person #" << count << ":\n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":\n";
std::getline(std::cin,phoneNumStr);
std::stringstream s(phoneNumStr); // no use of cin. Using stringstream to break up line and extract it into phoneNum
s >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?\n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}