Singly linked list , project stops working [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am new to c++ , trying to write singly linked list in c++ , and it's various operations like , print, insert at the end, insert the end, delete etc. I am doing step by step. I have written my first insert method. It is working fine no error , but the first time it goes to the if condition of insert i.e the list is empty and create the first node here the problem is coming is after entering the first node the output screen is closing saying "project has stopped working" . what I am trying to do is after entering one node it should ask "do u wish to add another node?" if users enter "y" it should continue otherwise should go back to main function : Here is my code :
#include <iostream>
using namespace std;
struct node
{
int num;
node *next;
};
class link_list
{
node *head;
node *list;
public:
link_list()
{
head = NULL;
list = NULL;
//cout << list;
}
// void display_options(link_list &b);
void print_list();
void insert();
//void insert_at_beg();
// void insert_at_middle();
};
void link_list::print_list()
{
if (list == NULL)
{
cout << "Empty list" << endl;
//return;
}
else
{
int count = 0;
while (list->next != NULL)
{
list = list->next;
count++;
cout << "Node " << "value " << endl;
cout << count << " " << list->num << endl;
}
}
}
void link_list::insert()
{
// head = new node;
//list = head;
int y;
char a;
do
{
if (head == NULL)
{
head = new node;
cout << "enter the first node" << endl;
cin >> y;
list->num = y;
list->next = NULL;
}
else
{
node * newNode;
list == head;
while (list->next != NULL)
{
list->next = list;
}
newNode = new node;
cin >> y;
newNode->num = y;
list->next = newNode;
newNode->next = NULL;
}
cout << "do u wish to add another node?" << endl;
cin >> a;
} while (a == 'y' || a == 'Y');
}
int main()
{
link_list ll;
char choice;
do{
cout << "select one" << endl;
cout << "press 1 for insert ." << endl;
cout << "press 2 for insert at beginning ." << endl;
cout << "press 3 for insert at the middle ." << endl;
cout << "press 4 delete ." << endl;
cout << "print 5 print the linked list :" << endl;
cout << "print 6 exit :" << endl;
int no;
cin >> no;
switch (no)
{
case 1:
ll.insert();
break;
case 5:
ll.print_list();
case 6:
return 0;
default:
cout << "oops wrong choice" << endl;
}
fflush(stdin);
cout << "Do u wanna make another choice?" << endl;
cin >> choice;
cout << choice << endl;
} while (choice == 'Y' || choice == 'y');
cout << "Thanks!" << endl;
system("pause");
return 0;
}

You need to keep a more careful eye on your compiler warnings, and you need to learn how to use a debugger. There are many problems with this code, but most of them are easy to find by debugging.
This line in link_list::insert is not an assignment, it is a comparison. This would likely be raised as a compiler warning in most modern compilers. You'll probably want to fix that.
line == head
This loop doesn't do anything useful:
while (list->next != NULL)
{
list->next = list;
}
If list starts off as non-null, all this code will do is loop forever, repeatedly setting list->next to list. You probably wanted the operation to be the other way around.
list = list->next;
which will get you the last element in the list.
Still in the same function, this is broken:
head = new node;
cout << "enter the first node" << endl;
cin >> y;
list->num = y;
list->next = NULL;
because you haven't initialised list. Using head instead of list is probably what you wanted.
This is silly:
newNode = new node;
cin >> y;
because you don't prompt for the node value.
Once these changes are made, the code appears to function, at least for insertion and printing, but I'm not going to investigate further.
Perhaps this might be better moved to https://codereview.stackexchange.com/

head = new node;
cout << "enter the first node" << endl;
cin >> y;
list->num=y; //<-- problem
You are using list without initializing it. You should write
list = head;
before the statement list->num=y;
This will fix the bug for first time insertion. but there are several other problems in you code while inserting subsequently, which is pointed out by other answers. Modified code will be
void link_list::insert()
{
int y;
char a;
do
{
if (head == NULL)
{
head = new node;
cout << "enter the first node" << endl;
cin >> y;
head->num = y;
head->next = NULL; // no need to list as a member at all
}
else
{
node* list = head;
while (list->next != NULL)
{
list= list->next;
}
list->next= new node;
cin >> y;
list->next->num = y;
list->next->next = NULL;
}
cout << "do u wish to add another node?" << endl;
cin >> a;
} while (a == 'y' || a == 'Y');
}
void link_list::print_list()
{
if (head == NULL)
{
cout << "Empty list" << endl;
//return;
}
else
{
int count = 0;
node* list=head;
while (list->next != NULL)
{
count++;
cout << "Node " << "value " << endl;
cout << count << " " << list->num << endl;
list = list->next;
}
}
}

Related

Queue linked list not printing it's contents

I am trying to get input of a queue with the linked list implementation. The issue is that the contents of the queue aren't being printed. I tried debugging but it says that in the function displayCar that pointer p is null regardless. I can't tell what's wrong with why pointer p is NULL. Is there a missing reference when I am trying to push from the carInput function?
#include <iostream>
#include <queue>
using namespace std;
class record
{
public:
string ownerID, plateNumber;
record* next;
};
void push(string ownerID1, string plateNumber1, record **head, record **tail) {
record *n = new record();
n->ownerID = ownerID1;
n->plateNumber = plateNumber1;
n->next = NULL;
if (*head == NULL) {
*head =*tail= n;
}
else {
(*tail)->next = n;
*tail = n;
}
}
void pop(record** head, record** tail) {
record* p = *head;
while (*head != NULL) {
*head = (*head)->next;
free(p);
p = *head;
}
if (*head == NULL)
{
*tail = NULL;
}
}
void carInput(record *head, record *tail) {
char choice = 'Y';
string ownTemp, plateTemp;
while (choice == 'Y') {
cout << "Enter Owner Name: ";
cin >> ownTemp;
cout << "Enter Plate Number: ";
cin >> plateTemp;
push(ownTemp,plateTemp,&head,&tail);
cout << "Press [Y] for next input: ";
cin >> choice;
}
}
void displayCar(record* head, record *tail) {
record* p = head;
cout << "List Of Cars: \n";
int i = 1;
while (p!= NULL) {
cout << i << ". Owner Name: " << p->ownerID << endl;
cout << i << ". Plate Number: " << p->plateNumber<< endl;
pop(&head,&tail);
i++;
}
}
void serviceCar(record *head,record*tail) {
record* p = head;
string plateTemp;
int i = 0, time = 0;
char choice = 'Y';
cout << "Enter Plate Number:";
cin >> plateTemp;
while (p!= NULL) {
if (p->plateNumber == plateTemp) {
cout << "There is [" << i << "] car in queue before your turn. Estimated time in queue: " << time;
}
else {
i++;
time = time + 45;
}
pop(&head,&tail);
}
}
int main() {
record* head = NULL;
record*tail = NULL;
cout << ":: Car Record::\n\n";
carInput(head,tail);
displayCar(head,tail);
serviceCar(head, tail);
}
I don't know why you are punishing yourself with code like this when you are in C++ and there are plenty easier ways to do the same, but I'll try to help anyway by underlining the main problems:
1). The main reason that you must be struggling is that in the first push, even after *head =*tail= n;, the *head->next is still NULL and later when you'll try to iterate from the head, as you do in pop, *head = (*head)->next; you will get nothing.
2). if you want to do pop, you should delete one element for each call, not the whole collection - so you need if instead of while.
You have while where you using pop for each iteration and in the pop you also have the while, so think about it.
Also, you should be returning the value to display it easily - or change the way you trying to cout p in displayCar.
3). When you want to display the collection you just have to iterate through the collection instead of deleting all the elements, which will leave you empty collection after one display. You just need to iterate and display them, not to delete, something like that:
record* p = *head;
int i = 0;
while (p != NULL) {
cout << i << ". Owner Name: " << p->ownerID << endl;
cout << i << ". Plate Number: " << p->plateNumber<< endl;
p = p->next;
i++;
}
There are some other points that should be mentioned, but I think that's enough to get the code right direction - anyway, my advice would be to try to take a good look for simple linked-list how it's done and then try Queue linked-list, or just check already written examples and then try it by yourself. GeeksForGeeks
Your carInput receives pointers by value, and modifying those pointers has no effect on the pointers you pass to it.
Thus, main's head and tail are always null.
(You solved this with the pushing and popping functions, but failed to apply the same principle here.)
void carInput(record **head, record **tail) {
char choice = 'Y';
string owner, plate;
while (choice == 'Y') {
cout << "Enter Owner Name: ";
cin >> owner;
cout << "Enter Plate Number: ";
cin >> plate;
push(owner, plate, head, tail);
cout << "Press [Y] for next input: ";
cin >> choice;
}
}
You need to combine this fix with the fixes pointed out in the comments.

Can you check why removing an Item from a linked list is not working in this code? [duplicate]

This question already has an answer here:
How do I properly delete nodes of linked list in C++
(1 answer)
Closed 2 years ago.
I've been asked by a friend to help him with an exercise, basically the idea is like below.
Car number = fr50000 Car owner = AlexNelson Parking time = 3.5 hours.
He was told not to use stuff like string or getline, so it's simple app just to learn the idea of working with linked lists.
So I made this program. When I'm calling the remove() function the first time, it says the list is empty at the beginning (as it should do). But the second and third time, it's saying that the car is removed, but when I call the display() function, the car is still there (it didn't remove from the list)
Can you tell me what's wrong with my code?
#include <iostream>
using namespace std;
int length = 0;//variable of how many items in the list
struct node
{
char carNumber[15];
char carOwner[20];
float parkingTime;
node *link;
};
typedef struct node node;
node *head;//the begining of a list;
bool isempty()
{
if (length == 0)
return true;
else
return false;
}
void insert()
{
if (isempty())
{
head = new node;
cout << "Enter car number: ";
cin >> head->carNumber;
cout << "Enter Car owner: ";
cin >> head->carOwner;
cout << "Enter parking time: ";
cin >> head->parkingTime;
head->link = NULL;
length++;
}
else
{
node *p = head;
node *pnext = new node;
while (true)
{
if (p->link == NULL)
{
p->link = pnext;
break;
}
p = p->link;
}
cout << "Enter car number: ";
cin >> pnext->carNumber;
cout << "Enter Car owner: ";
cin >> pnext->carOwner;
cout << "Enter parking time: ";
cin >> pnext->parkingTime;
pnext->link = NULL;
length++;
}
}
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p;
p = head;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
p = p->link;
cout << "Car removed\n";
return;
}
p = p->link;
}
cout << "Car was not found, check the number\n";
}
void display()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
cout << "Car Number\t\tCar Owner\t\tParking Time\n";
node *p = head;
while (p != NULL)
{
cout << p->carNumber << "\t\t" << p->carOwner << "\t\t" << p->parkingTime << " Hours\n";
p = p->link;
}
}
int main()
{
string number;
display();
insert();
insert();
insert();
display();
remove();
display();
insert();
remove();
display();
}
Your remove() function is not actually removing (or destroying) a node from the list. You need to update the link of the previous node in the list to point to the next node in the list. And you need to update the head too, if removing the 1st node in the list.
Try this instead:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p = head;
node *prev = NULL;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
if (p == head)
head = p->link;
if (prev)
prev->link = p->link;
delete p;
cout << "Car removed\n";
return;
}
prev = p;
p = p->link;
}
cout << "Car was not found, check the number\n";
}
Alternatively:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node **p = &head;
while (*p != NULL)
{
if (strcmp((*p)->carNumber, carnumber) == 0)
{
node *n = *p;
*p = (*p)->link;
delete n;
cout << "Car removed\n";
return;
}
p = &(p->link);
}
cout << "Car was not found, check the number\n";
}
p = p->link; You're modifying the value of the local variable p, not the list. You need to modify the previous node's link field.
In the loop in remove, you are doing p = p->link, when p points to the node you want to remove. But you actually need to update the link field of the node that is pointing to p.
Here's a simple way of doing that:
node **p = &head;
while(*p)
if (strcmp((*p)->carNumber, carnumber) == 0)
break;
else p = &(*p)->link;
if (*p)
{
cout<<"Car removed\n";
delete std::exchange(*p, (*p)->link);
}
else cout << "Car was not found, check the number\n";
If you can't use std::exchange, then you can replace that with:
auto h = *p;
*p = (*p)->link);
delete h;

Difficulties implementing sorted Doubly Linked list

I'm taking a class in c++ and one of our assignments is to make an ADT for a linked list and do the implementations.
I've done well so far. But now I'm supposed to add the elements in order.
The code below is my ADD-function and what i've accomplished with this code is to sort them GIVEN that there are only 2 elements in the list. (I'm working with the problem from the basics, and refine my code after).
The while-loop on line 11 is my attempt to iterate (and int t are for debugging)
The problem i have is that my program crash without error (cmd stops working) if i try to add a number greater than 5.
The strange thing here is that if i remove my while-loop, it works. I can't find out whats wrong. So i hope for a little help
Main.cpp:
int main() {
SortedDoublyLinkedList<int> *list = new SortedDoublyLinkedList<int>(5, nullptr, nullptr);
int counter = 0;
while (counter < 2) {
int add;
cout << "Enter number: ";
cin >> add;
list->add(add);
counter++;
cout << counter << endl;
}
cout << list->removeLast()->getData()<< endl;
cout << list->removeLast()->getData()<< endl;
cout << list->removeLast()->getData()<< endl;
return 0;
}
SortedDoublyLinkedList.cpp
template<class T>
DoublyLinkedNode<T> *SortedDoublyLinkedList<T>::add(T val) {
if (isEmpty()) {
head = new DoublyLinkedNode<T>(val);
head->setPrevious(nullptr);
head->setNext(nullptr);
} else {
DoublyLinkedNode<T> *newEl = new DoublyLinkedNode<T>(val);
DoublyLinkedNode<T> *temp = head;
int t = 0;
while(temp->getData() != nullptr){
temp = temp->getNext();
t++;
cout << t << endl;
}
if(newEl->getData() > head->getData()){
newEl->setPrevious(head);
newEl->setNext(nullptr);
head->setNext(newEl);
cout <<"IF"<<endl;
}else{
DoublyLinkedNode<T> *temp = head;
head = newEl;
newEl->setPrevious(nullptr);
newEl->setNext(temp);
temp->setPrevious(head);
cout << "El" << endl;
}
}
numberOfElements++;
return head;
}
This bit of the loop (which I guess is trying to print out the items)... is wrong. The while part loops over the data while it works on elements
Your broken code :
while(temp->getData() != nullptr){
temp = temp->getNext();
t++;
cout << t << endl;
}
Somewhat more functional :
while(temp != nullptr){
cout << t << ":" << temp->getData() << endl;
temp = temp->getNext();
++t;
}
It doesn't solve your sorting problem, but as you said above, you are going iteratively, so I leave that for you to work out.

Error in my c++ code using linkedlist "Stop working" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
my code stop working in that test case, i think that the error in function Checktables but i'm not sure and i can't fix the error please help me to tun this code correctly.
image of a test case and the error
this is a cpp file with main .cpp
#include"Header.h"
string Name;
string namess;
customer::customer()
{
name = "";
gsize = status = 0;
next = NULL;
}
customer::customer(string name1, int gsize1, int status1)
{
name = name1;
gsize = gsize1;
status = status1;
next = NULL;
}
waitinglist::waitinglist()
{
chairnum =50 ;
totalcustomers = tables = 0;
head = tail = NULL;
}
waitinglist::waitinglist(int val)
{
chairnum = 50;
totalcustomers = 0;
tables = 0;
head = tail = NULL;
}
void waitinglist::change()
{
customer*temp ;
temp = head;
cout << "enter the name: ";
cin >> namess;
while (temp != NULL)
{
if (namess == temp->name)
{
if (temp->status==2)
{
temp->status=1;
cout << "done! " << endl ;
break ;
}
}
else if (namess != temp->name)
{
temp = temp->next;
}
}
if (temp == NULL)
{
cout << "can't found! " << endl;
}
}
void waitinglist::newcustomer()
{
customer*tmp = new customer;
cout << "enter the name: "; cin >> tmp->name;
customer*tmpo=new customer;
tmpo=head ;
while (tmpo != NULL)
{
if (tmp->name != tmpo->name)
{
tmpo = tmpo->next;
}
else if (tmp->name == tmpo->name)
{
cout<<"The Name already exist! " << endl ;
cout << "enter the name: "; cin >> tmp->name;
tmpo=head;
}
}
cout << "enter the group number: "; cin >> tmp->gsize;
cout << "enter the status: "; cin >> tmp->status;
if (head == NULL) // linkedlist is empty
{
head = tail = tmp;
totalcustomers++;
}
else
{
tail->next = tmp;
tail=tail->next;
totalcustomers++;
}
}
void waitinglist::checktables()
{
float c=5.00;
customer*temp=head;
customer*found;
cout<<"enter number of tables: ";
cin >> tables ;
while (tables>=1 && temp!=NULL)
{
int x;
float y;
y=((temp->gsize)/c);
x=(temp->gsize)/c;
if (tables<y)
{
temp=temp->next;
}
else if (tables>=y)
{
if (x==y)
{
tables=tables-x ; // Correct Table!
cout<<temp->name<<endl;
}
else if (x!=y)
{
tables=tables-(x+1);
cout<<temp->name<<endl;
}
found=temp ;
delete found; // Discard
break ;
}
}
}
void waitinglist::displayall()
{
customer *tmp;
tmp = head;
if (tmp == NULL)
{
cout << "Empty!";
}
while (tmp != NULL)
{
cout << "Name: " << tmp->name <<endl;
cout << "group number: " << tmp->gsize << endl;
tmp = tmp->next;
}
cout << endl;
}
void waitinglist::diplaycustomer()
{
customer*tmp;
tmp = head;
cout << "enter the name: ";
cin >> Name;
while (tmp != NULL)
{
if (Name == tmp->name)
{
cout << "the name : " << tmp->name << endl;
cout << "the group size = " << tmp->gsize << endl;
cout << "the status = " << tmp->status << endl;
break;
}
else if (Name != tmp->name)
{
tmp = tmp->next;
}
}
if (tmp == NULL)
{
cout << "can't found!" << endl;
}
}
int main()
{
int choice;
string name1 = "";
int gsize1 = 0;
int status1 = 0;
waitinglist mylist;
cout << "Note: 1 in status means the customer not here and 2 means the customer is here.\n";
cout << "Select your option.\n\n";
cout << "(1) Add a new Customer.\n";
cout << "(2) Display information based on Name.\n";
cout << "(3) List all Names.\n";
cout << "(4) to change the status. \n" ;
cout << "(5) Check tables by name. \n";
cout << "(6) quit. \n";
do
{
cout << "\n";
cout << "Enter your choice: --> ";
cin >> choice;
if (1 <= choice && choice <= 5)
{
switch (choice)
{
case 1:
mylist.newcustomer();
break;
case 2:
mylist.diplaycustomer();
break;
case 3:
mylist.displayall();
break;
case 4:
mylist.change() ;
break;
case 5 :
mylist.checktables();
break;
default:
cout << "Invalid choice. Enter again.\n\n";
break;
}
}
else if (choice>6)
{
cout << "Invalid choice. Enter again.\n\n";
break;
}
} while (choice != 6);
return 0;
}
and this is the header file .h
#include<iostream>
#include<string>
using namespace std;
class customer
{
public:
string name;
int gsize;
int status;
customer* next;
customer();
customer(string,int,int);
};
class waitinglist
{
public:
int tables; //number of occupied tables
int chairnum;
int totalcustomers;
customer*head,*tail;
waitinglist();
waitinglist(int);
void newcustomer();
void diplaycustomer();
void displayall();
void change () ;
void checktables();
};
One error is that your checktables function corrupts your linked list structure by calling delete on one of the nodes:
found = temp;
delete found; // Discard
What you've just done in those lines above is to have a linked list with a broken (invalid) link in it. Any functions that now traverses the list (like displaytables) will now hit the broken link, and things start to go haywire.
To delete a node from a linked list, you have to not just call delete, but adjust the link in waitinglist that used to point to that deleted node and have it point to the next node after the deleted one.
Think of it like a real chain -- if one of the links in the chain needs to be removed, you have to physically remove it, and hook the link before it to the next good link. You didn't do this step.
I won't write the code for that, but this is what you should have seen much earlier in the development of your program. Better yet would have been to write a singly-linked list class that adds and removes nodes correctly first. Test it, and then once it can add and remove nodes successfully without error, then use it in your larger program.

Segfault with initializing an int

/*Matt Boler
meb0054
hw5.cpp
Compile with gcc in cygwin
*/
#import <iostream>
#import <string>
#import <sstream>
#import <cstdlib>
#import <climits>
#import <assert.h>
using namespace std;
//#define UNIT_TESTING
struct TriviaNode
{
string question;
string answer;
int points;
TriviaNode *next;
};
typedef TriviaNode* NodePtr;
//Input: (1) root is the linked list to be added to
// (2) Question is the question for the node to ask
// (3) Answer is the answer to the node's question
// (4) Points is the point value of the node
//This adds a node to the end of the list
void appendNode(NodePtr& root, string question, string answer, int points);
//Input: (1) root is the linked list to get the length of
//Output: Returns the number of nodes in the linked list
//This calculates the number of nodes in a list
int getListLength(NodePtr& root);
//Input: (1) root is the node to start the list from
//This generates a hardcoded trivia list with 3 predefined questions and answers
void generateHardCodedList(NodePtr& root);
//Input: (1) root is the linked list containing questions to be asked
// (2) numQuestions is the number of questions to be asked from the list
//Output: returns o if answered correctly and 1 if answered incorrectly
//This asks the user a question
int askQuestion(NodePtr& root, int numQuestions);
int main()
{
#ifdef UNIT_TESTING
NodePtr head;
cout << "*** This is a debugging version ***" << endl;
cout << "Unit Test Case 1: Ask no questions. The program should give a warning" <<endl;
askQuestion(head, 0);
cout << "Test Case passed..." << endl;
generateHardCodedList(head);
cout << "Unit Test Case 2.1: Ask one question. The tester enters an incorrect answer" << endl;
assert(askQuestion(head, 1) == 1);
cout << "Test Case passed..." << endl;
cout << "Unit Test Case 2.2: Ask one question. The tester enters a correct answer" << endl;
assert(askQuestion(head, 1) == 0);
cout << "Test Case passed..." << endl;
cout << "Unit Test Case 3.1: Ask all questions. The tester enters incorreect answers" << endl;
assert(askQuestion(head, 1) == 1);
assert(askQuestion(head, 2) == 1);
assert(askQuestion(head, 3) == 1);
cout << "Test Case passed..." << endl;
cout << "Unit Test Case 3.2: Ask all questions. The tester enters correect answers" << endl;
assert(askQuestion(head, 1) == 0);
assert(askQuestion(head, 2) == 0);
assert(askQuestion(head, 3) == 0);
cout << "Test Case passed..." << endl;
cout << "Unit Test Case 4: Ask 5 questions in the linked list" << endl;
askQuestion(head, 5);
cout << "*** END OF THE DEBUGGING VERSION ***" << endl;
#else
{
cout << "Welcome to Matt Boler's Trivia Quiz Game!" << endl;
string userContinue = "";
string no = "No";
NodePtr head;
int numQuestions = 3;
generateHardCodedList(head);
while(userContinue.compare(no) != 0)
{
string question, answer;
int score;
cout << "Enter a question:";
getline(cin, question);
cout << "Enter an answer:";
getline(cin, answer);
cout << "Enter award points:";
cin >> score;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Continue? (Yes/No)" << endl;
getline(cin, userContinue);
numQuestions++;
}
//ANYTHING PAST HERE IN THE ELSE BLOCK FAILS VIA SEGFAULT OR JUST NOT RUNNING. NO IDEA WHY
int score = 0;
NodePtr cur = head;
for(int x = 1; x < numQuestions; x++)
{
cur = cur->next;
if(askQuestion(head, x) == 0)
{
score += cur-> points;
}
}
cout << "Your score is: " << score << endl;
}
#endif
return 0;
}
void appendNode(NodePtr& root, string q, string ans, int pts)
{
NodePtr cur;
NodePtr pre;
cur = new TriviaNode;
assert(cur != NULL);
cur->question = q;
cur->answer = ans;
cur->points = pts;
cur->next = NULL;
if (root == NULL)
root = cur;
else
{
pre = root;
while (pre->next != NULL)
{
pre = pre->next;
}
pre->next = cur;
}
}
void generateHardCodedList(NodePtr& root)
{
string q1 = "How long was the shortest war on record? (Hint: how many minutes)";
string ans1 = "38";
int pts1 = 100;
string q2 = "What was the Bank of America's original name? (Hint: Bank of Italy or Bank of Germany)";
string ans2 = "Bank of Italy";
int pts2 = 50;
string q3 = "What is the best-selling video game of all time? (Hint: Call of Duty or Wii Sports)";
string ans3 = "Wii Sports";
int pts3 = 20;
appendNode(root, q1, ans1, pts1);
appendNode(root, q2, ans2, pts2);
appendNode(root, q3, ans3, pts3);
}
int askQuestion(NodePtr& root, int numQuestions)
{
NodePtr cur;
cur = root;
if(numQuestions > getListLength(root))
{
cout << "Warning: there aren't that many questions in the list" << endl;
}
else if(numQuestions < 1)
{
cout << "Warning: the number of trivia to be asked must be greater than or equal to one" << endl;
}
else
{
for(int i = 1; i < numQuestions; i++)
{
cur = cur->next;
}
cout << "Question: " << cur->question << endl;
cout << "Player answer: ";
string player_answer;
getline(cin, player_answer);
cin.clear();
if (player_answer == cur->answer)
{
cout << "Your answer is correct. You recieve " << cur->points << " points." << endl;
return 0;
}
else
{
cout << "Your answer is wrong. The correct answer is: " << cur->answer << endl;
}
}
return 1;
}
int getListLength(NodePtr& root)
{
if(root == NULL)
{
return 0;
}
int count = 0;
NodePtr cur = root;
while(cur != NULL)
{
cur = cur->next;
++count;
}
return count;
}
This is a program to add trivia questions to a linked list in c++. Anything after the while loop causes an error. Specifically, initializing an int causes a segfault; trying to print to console with cout refuses to print anything out.
In the current version of the code your generateHardCodedList pases the root pointer to appendNode functions without initializing it. The calling code did not initialize root (called head in main), generateHardCodedList does not initialize it either, so appendNode receives a garbage pointer as root. After that all bets are off: your program is already broken.
I don't know why in your case it crashes so much later in the code, but it doesn't matter anyway. Your generateHardCodedList call is already broken.
Either initialize your head pointer with nullptr before passing it to generateHardCodedList, or better initialize it to nullptr inside generateHardCodedList, before trying to call appendNode.