When I call the methods to print the data stored in the nodes of my doubly linked list, nothing prints except for empty strings and 0's
#include <iostream>
#include <string>
using namespace std;
struct node {
int weight;
string name;
node *nextname, *nextweight;
};
node *namehead = NULL, *weighthead = NULL;
bool isEmpty()
{
if (namehead == NULL && weighthead == NULL)
return true;
else
return false;
}
void addperson(int w, string n)
{
node* newNode = new node;
node *prev, *curr = newNode;
if (isEmpty()) {
namehead = newNode;
weighthead = newNode;
}
else {
curr = prev = namehead;
if (curr->name > n) {
namehead = newNode;
newNode->nextname = curr;
}
else {
do {
if (curr->name <= n) {
prev = curr;
curr = curr->nextname;
}
else
break;
} while (curr != NULL);
prev->nextname = newNode;
newNode->nextname = curr;
}
curr = prev = weighthead;
if (curr->weight > w) {
weighthead = newNode;
newNode->nextweight = curr;
}
else {
do {
if (curr->weight <= w) {
prev = curr;
curr = curr->nextweight;
}
else
break;
} while (curr != NULL);
prev->nextweight = newNode;
newNode->nextweight = curr;
}
}
}
void printname()
{
node* curr = namehead;
do {
cout << curr->name << " - " << curr->weight << endl;
curr = curr->nextname;
} while (curr != NULL);
cout << endl;
}
void printweight()
{
node* curr = weighthead;
do {
cout << curr->name << " - " << curr->weight << endl;
curr = curr->nextweight;
} while (curr != NULL);
cout << endl;
}
int main()
{
int w = 0;
string n;
for (int i = 0; i < 15; i++) {
cout << "Enter weight: ";
cin >> w;
if (w == -1)
break;
cout << "Enter name: ";
cin >> n;
addperson(w, n);
}
printname();
printweight();
return 0;
}
Expected output (By name):
John - 220
Steven - 190
Tyler - 150
Expected output (By weight):
Tyler - 150
Steven - 190
John - 220
CURRENT OUTPUT(Both ways):
" " - 0
" " - 0
" " - 0
EDIT
By taking the suggestions in the comments about actually assigning the values w (weight) and n (name) to the temporary node in the add method, the problem has been fixed. Thank you for the help.
curr->weight=w;
curr->name=n;
Assign passed values into weight and name members into placeholder node in the add method:
curr->weight=w;
curr->name=n;
Related
When I run my program, everything works as expected until the destructor of the LList class. On the line that says delete current;, I get the following error:
BlankConsoleLab.exe has triggered a breakpoint.
I have been trying to solve this issue for a long time. I would appreciate it if perhaps someone could try to point out what I am doing wrong here.
Below is my code for the program. Thank you
Linked List Class
class LList
{
private:
Node* head;
Node* tail;
int size = 0;
public:
LList()
{
head = nullptr;
tail = nullptr;
size = 0;
}
LList(Node* h, Node* t)
{
this->head = h;
this->tail = t;
}
~LList()
{
Node* current = head;
while (current != nullptr) {
Node* next = current->m_next;
delete current;
current = next;
}
}
void print()
{
//temporary node pointer to traverse through the linked list
Node* temp = head;
cout << endl << "<MY LINKED LIST>\n";
while (temp != nullptr)
{
cout << temp->m_data.firstName << " ";
cout << temp->m_data.lastName << " ";
cout << temp->m_data.hrWage << " ";
cout << temp->m_data.hrWork << " " << endl;
temp = temp->m_next;
}
cout << endl;
}
void removeFirst()
{
//case 1: linked list is empty (never enters loop)
//case 2: linked list is not empty
if (head != nullptr)
{
Node* temp = head;
head = head->m_next;
delete temp;
//decrease size tracker of the linked list
size--;
}
}
void removeLast()
{
//case 1: linked list is empty (never enters loop)
//case 2: linked list has one node
if (head->m_next == nullptr)
{
removeFirst();
}
//case 3: linked list has more than one node
else if (head != nullptr)
{
Node* cur = head;
Node* prev = nullptr;
while (cur->m_next != nullptr)
{
prev = cur;
cur = cur->m_next;
}
tail = prev;
tail->m_next = nullptr;
delete cur;
//decrease size tracker of the linked list
size--;
}
}
//void removeAt(int pos)
//{
// //Case 1: input is invalid (less than 1 or greater than size)
// if (pos < 1 && pos > size)
// {
// return;
// }
// //Case 2: input is position 1
// else if (pos == 1)
// {
// removeFirst();
// }
// //Case 3: input is the last position (input equals size)
// else if (pos == size)
// {
// removeLast();
// }
// //Case 4: input is valid, and not 1 or last position (greater than 1 and less than size)
// else if (head != nullptr)
// {
// Node* cur = head;
// Node* prev = nullptr;
// for (int i = 1; i < pos; i++)
// {
// prev = cur;
// cur = cur->m_next;
// }
// prev->m_next = cur->m_next;
// delete cur;
// size--;
// }
//}
Node* swap(Node* lh, Node* rh)
{
Node* temp = rh->m_next;
rh->m_next = lh;
lh->m_next = temp;
return rh;
}
void readBin()
{
ifstream file;
file.open("C:\\Users\\there\\source\\repos\\cst126-lab9-JEmersonLawrance\\BlankConsoleLab\\Employee Data.bin", ios::binary);
if (file)
{
Node* cur = head;
Node* prev = nullptr;
for (int i = 0; i < 4; i++)
{
file.read((char*)&cur->m_data.firstName, sizeof(cur->m_data.firstName));
file.read((char*)&cur->m_data.lastName, sizeof(cur->m_data.lastName));
file.read((char*)&cur->m_data.hrWage, sizeof(cur->m_data.hrWage));
file.read((char*)&cur->m_data.hrWork, sizeof(cur->m_data.hrWork));
prev = cur;
cur = cur->m_next;
}
return;
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
void writeBin()
{
ofstream file;
file.open("Employee Data Output.bin", ios::binary);
if (file)
{
Node* cur = head;
Node* prev = nullptr;
while (cur != nullptr)
{
file.write((char*)&cur->m_data.firstName, sizeof(cur->m_data.firstName));
file.write((char*)&cur->m_data.lastName, sizeof(cur->m_data.lastName));
file.write((char*)&cur->m_data.hrWage, sizeof(cur->m_data.hrWage));
file.write((char*)&cur->m_data.hrWork, sizeof(cur->m_data.hrWork));
prev = cur;
cur = cur->m_next;
}
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
};
Node Class
class Node
{
public:
Employee m_data;
Node* m_next;
Node()
{
m_data.firstName = "";
m_data.lastName = "";
m_data.hrWage = 0;
m_data.hrWork = 0;
m_next = nullptr;
}
Node(Node* next)
{
m_data.firstName = "";
m_data.lastName = "";
m_data.hrWage = 0;
m_data.hrWork = 0;
m_next = next;
}
Node(const Node& copy)
{
m_data.firstName = copy.m_data.firstName;
m_data.lastName = copy.m_data.lastName;
m_data.hrWage = copy.m_data.hrWage;
m_data.hrWork = copy.m_data.hrWork;
}
Node operator = (const Node& copy)
{
m_data.firstName = copy.m_data.firstName;
m_data.lastName = copy.m_data.lastName;
m_data.hrWage = copy.m_data.hrWage;
m_data.hrWork = copy.m_data.hrWork;
return *this;
}
~Node()
{
}
};
Employee Class
struct Employee
{
public:
string firstName;
string lastName;
int hrWage;
int hrWork;
Employee()
{
firstName = "";
lastName = "";
hrWage = 0;
hrWork = 0;
}
Employee(string first, string last, int wage, int work)
{
firstName = first;
lastName = last;
hrWage = wage;
hrWork = work;
}
~Employee()
{
}
void getInput()
{
for (int i = 0; i < 4; i++)
{
cout << "EMPLOYEE # " << i+1 << ":\n" << endl;
cout << "First name: ";
cin >> this[i].firstName;
cout << "Last name: ";
cin >> this[i].lastName;
cout << "Hourly Wage: ";
cin >> this[i].hrWage;
cout << "Hours Worked: ";
cin >> this[i].hrWork;
}
}
void writeBin()
{
ofstream file;
file.open("C:\\Users\\there\\source\\repos\\cst126-lab9-JEmersonLawrance\\BlankConsoleLab\\Employee Data.bin", ios::binary);
if (file)
{
for (int i = 0; i < 4; i++)
{
file.write((char*)&this[i].firstName, sizeof(this[i].firstName));
file.write((char*)&this[i].lastName, sizeof(this[i].lastName));
file.write((char*)&this[i].hrWage, sizeof(this[i]).hrWage);
file.write((char*)&this[i].hrWork, sizeof(this[i]).hrWork);
}
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
};
Main Function
int main()
{
cout << "In this program, LIST will read the user information"
<< " in from a binary file, and output it into a different binary"
<< " file.\n" << endl;
Employee data[4];
data->getInput();
data->writeBin();
//creating linked list
Node fourth;
Node third(&fourth);
Node second(&third);
Node first(&second);
LList LIST(&first, &fourth);
LIST.readBin();
LIST.writeBin();
LIST.print();
return 0;
}
In main(), you are constructing your LIST object with pointers to local Node objects that are created in automatic memory within main()'s call frame. When main() exits, all of its local variables are destroyed automatically. But, when the LIST object is destroyed, its destructor tries to call delete on those Node objects which were not created in dynamic memory with new. Thus, your code exhibits undefined behavior.
(This is a Homework Question)I am having trouble implementing a name sort for a linked list. I have the weight(int) sort working now i just want to sort names in ascending order.
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int c = 0;
struct node
{
char name[20];
int weight;
node *next;
node *prev;
};
node*head = NULL;
node *tail = NULL;
node *start_ptr = NULL;
Here I create the node and insert into the list in sorted position. I want to do the same for names. A name is assigned a weight(For example a persons weight). It has to be in sorted position so when the print function is called it prints the list in sorted order.
void create(int x,char name)
{
node*p = NULL;
node*r = NULL;
node*np = NULL;
np = new node;
np->weight = x;
np->next = NULL;
np->prev = NULL;
if (c == 0)
{
tail = np;
head = np;
p = head;
p->next = NULL;
p->prev = NULL;
c++;
}
else
{
p = head;
r = p;
if (np->weight < p->weight)
{
np->next = p;
p->prev = np;
np->prev = NULL;
head = np;
p = head;
do
{
p = p->next;
} while (p->next != NULL);
tail = p;
}
else if (np->weight > p->weight)
{
while (p != NULL && np->weight > p->weight)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->weight < p->weight)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
} while (p->next != NULL);
}
tail = p;
break;
}
}
}
}
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout << t->weight << "\t";
t = t->next;
}
cout << endl;
}
void print_node()
{
node *temp;
temp = start_ptr;
if (temp == NULL) cout << "Empty List!" << endl;
while (temp != NULL)
{
if (temp == NULL) cout << "Empty List!" << endl;
cout << "Names & weights sorted(ascending) by name. :\n";
cout << "Name : " << temp->name << endl;
cout << "Weight : " << temp->weight << endl;
cout << "Names & weights sorted(ascending) by weight. : \n";
cout << endl;
temp = temp->next;
}
}
int main()
{
int i = 0, n, x;
char names[20];
cout << "Enter the number of people: \n";
cin >> n;
A loop that reads in the weight and the name of that person
while (i < n)
{
cout << "\nEnter Weights: \n";
cin >> x;
cout<<"Enter a Name"<<endl;
cin>>names;
create(x,*names);
i++;
}
cout << "Output: \n";
traverse_head();
system("pause");
return 0;
}
The following code calculates the sum of the elements of the unidirectional list items greater than 3 and smaller than 8 and the result of the sum is changed the beginning of the list.
#include <iostream>
using namespace std;
struct List
{
int num;
List* nItem;
};
int Input()
{
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
void MakeList(List **head, int n)
{
if (n > 0) {
*head = new List;
(*head)->num = Input();
(*head)->nItem = NULL;
MakeList(&(*head)->nItem, n - 1);
}
}
void Print(List* head)
{
if (head != NULL) {
cout << head->num << " ";
Print(head->nItem);
}
}
List* Add_start(List* head, int index, int elm)
{
List* p = new List;
p->num = elm;
p->nItem = NULL;
if (head == NULL) {
head = p;
}
else {
List* current = head;
for (int i = 0; (i < index - 1) && (current->nItem != NULL); i++)
{
current = current->nItem;
}
if (index == 0)
{
p->nItem = head;
head = p;
}
else {
if (current->nItem != NULL) {
p->nItem = current->nItem;
}
current->nItem = p;
}
}
return head;
}
int Sum(List* head)
{
int sum = 0;
List* p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nItem;
}
return sum;
}
void DeleteList(List* head)
{
if (head != NULL) {
DeleteList(head->nItem);
delete head;
}
}
int main()
{
int n = 10;
List* head = NULL;
cout << "Enter 10 number to the list\n" << endl;
MakeList(&head, n);
int sum = Sum(head);
head = Add_start(head, 0, sum);
cout << "\nList: ";
Print(head);
cout << endl;
DeleteList(head);
system("pause");
return 0;
}
How can I do the same operation with a bidirectional list?
Notes:
A bidirectional (or double linked) list, also has a member pointing to the previous node: this is the whole difference between the 2 list types (as a consequence the first element - or the one at the left of the list, will have this member pointing to NULL). So, when such a node is created/inserted into a list, this new member should be set as well (I commented in the code places where this happens), for the new node and for the one following it (if any).
I modified the way of how a list is created - MakeList replaced by _MakeList2 + MakeList2; the underscore(_) in _MakeList2 specifies that it's somehow private (convention borrowed from Python) - it's not very nice, but I thought it's easier this way
I don't have Visual Studio on this computer, so I used gcc. It complained about system function so I had to add #include <stdlib.h>
I renamed some of the identifiers (List -> Node, Add_start -> AddNode, nItem -> nNode) either because the new names make more sense, or their names are consistent
I tried to keep the changes to a minimum (so the solution is as close as possible to your original post)
I enhanced (by adding an additional argument: toRight (default value: 1)) the Print func, so it can iterate the list both ways - I am iterating right to left (for testing purposes) before deleting the list
I corrected some (minor) coding style issues
Here's the modified code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int num;
Node *pNode, *nNode; // Add a new pointer to the previous node.
};
int Input() {
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
Node *_MakeList2(int n, Node *last=NULL) {
if (n > 0) {
Node *node = new Node;
node->num = Input();
node->pNode = last;
node->nNode = _MakeList2(n - 1, node);
return node;
}
return NULL;
}
Node *MakeList2(int n) {
return _MakeList2(n);
}
void Print(Node *head, int toRight=1) {
if (head != NULL) {
cout << head->num << " ";
if (toRight)
Print(head->nNode, 1);
else
Print(head->pNode, 0);
}
}
Node* AddNode(Node *head, int index, int elm) {
Node *p = new Node;
p->num = elm;
p->pNode = NULL; // Make the link between this node and the previous one.
p->nNode = NULL;
if (head == NULL) {
head = p;
} else {
Node *current = head;
for (int i = 0; (i < index - 1) && (current->nNode != NULL); i++) {
current = current->nNode;
}
if (index == 0) {
p->nNode = head;
head->pNode = p; // Make link between next node's previous node and the current one.
head = p;
} else {
if (current->nNode != NULL) {
p->nNode = current->nNode;
}
p->pNode = current; // Make the link between this node and the previous one.
current->nNode = p;
}
}
return head;
}
int Sum(Node* head) {
int sum = 0;
Node *p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nNode;
}
return sum;
}
void DeleteList(Node *head) {
if (head != NULL) {
DeleteList(head->nNode);
delete head;
}
}
int main() {
int n = 10;
Node *head = NULL, *tail = NULL;
cout << "Enter " << n << " number(s) to the list" << endl << endl;
head = MakeList2(n);
int sum = Sum(head);
head = AddNode(head, 0, sum);
cout << endl << "List: ";
Print(head);
cout << endl;
tail = head;
if (tail) {
while (tail->nNode != NULL)
tail = tail->nNode;
cout << endl << "List reversed: ";
Print(tail, 0);
cout << endl;
}
DeleteList(head);
system("pause");
return 0;
}
I can print one string. But when I try to add two strings it only prints the first string? curr is the beginning of the linked list. If I add one country and tell the program to print it will print the country with the information. However if I add two countries, It will only print the first country.
void LinkedList::printList()
{
curr = head;
while (curr)
{
cout << "Country Name: " << curr->name << ", " << "Gold: " << curr->
gold << ", " << "Silver: " << curr->silver << ", " << "Bronze: " << curr->bronze << "\n";
curr = curr->next;
}
}
bool LinkedList::addCountry(string newName, int gold, int silver, int bronze) {
if (head == NULL)// Adding first element
{
head = new struct Country;
if (head == NULL) return false; // could not create head linked list country
head->name = newName;
head->gold = gold;
head->silver = silver;
head->bronze = bronze;
head->next = NULL;
return true;
} else {
curr = head;
while (curr) {
curr = curr->next;
}
curr = new struct Country;
if (curr == NULL)
return false;
curr->name = newName;
curr->gold = gold;
curr->silver = silver;
curr->bronze = bronze;
curr->next = NULL;
return true;
}
}
#Barmak Shemirani is correct. I think if you had a tail member it would be better:
class LindedList
{
public:
LindedList()
{
tail=head=curr=NULL;
};
Country* head;
Country* curr;
Country* tail;
void printList()
{
curr = head;
while (curr)
{
cout << "Country Name: " << curr->name << ", " << "Gold: " << curr->
gold << ", " << "Silver: " << curr->silver << ", " << "Bronze: " << curr->bronze << "\n";
curr = curr->next;
}
};
bool addCountry(string newName, int gold, int silver, int bronze)
{
curr = new Country;
if (curr == NULL)
return false;
curr->name = newName;
curr->gold = gold;
curr->silver = silver;
curr->bronze = bronze;
curr->next = NULL;
if (head == NULL)
{
head = curr;
tail=curr;
} else
{
tail->next=curr;
tail=curr;
}
return true;
};
};
printList is correct. But in addCountry the last element must point to the new element which was just inserted. For example:
bool LinkedList::addCountry(string newName, int gold, int silver, int bronze)
{
Country *newNode = new Country;
newNode->name = newName;
newNode->gold = gold;
newNode->silver = silver;
newNode->bronze = bronze;
newNode->next = NULL;
if (head == NULL)
{
//adding first element:
head = newNode;
}
else
{
//find the last element currently in the list:
Country *last = head;
while (last->next)//<= ***** edited
last = last->next;
//set newNode as the new last element:
last->next = newNode;
}
return true;
}
Also in C++ you can simple write new Country, it doesn't need struct keyword.
I made a simple command line application based on Kaiji Ep. 16 that mimics the game Emperor Card (for our midterms in basic programming). I got stuck on what I think is a simple problem, but I can't seem to solve it on my own. I have this function "winChecker(List *root, Node *head)," that checks what cards are drawn and who wins over who.
And it seems that it returns a wrong return value whenever I draw Citizen and the opponent draws Citizen as well. It should just loop, since Citizen vs Citizen is draw, according to my code.
Can you help me understand what I'm doing wrong here. Also if you see some other mistakes, feel free to point them out. I'm open to learn.
PS: I only used struct for Node and List since we aren't allowed to use Class for our midterms yet. I got 1 header file for each of those.
Source.cpp
#include "Node.h"
#include "List.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
Node *createKaijiDeck(int round);
Node *selectKaijiCard(Node *head, int index);
Node *deleteSelectedKaijiCard(Node *head, int index);
List* createTonegawaDeck(int round);
List *selectTonegawaCard(List *root, int indexT);
List *deleteSelectedTonegawaCard(List *root, int indexT);
bool betCheck(int betLength, int remainingLength);
int prizeCheck(int betLength, int round);
void printDeck(Node * head);
int winCheck(List *root, Node *head, int cardIndex, int randTonegawa);
void gameOver(int cash, int round, int input, int remaining);
int main() {
// Seed the RNG
srand((unsigned int)time(0));
// Initilizing variables
int input, cardIndex, randTonegawa, countTonegawa = 0, cash = 0, remaining = 30;
// Round loop
for (int round = 1; round < 12; round++) {
cout << "===============================================" << endl;
cout << " ROUND " << round << endl;
cout << "===============================================" << endl;
cout << "How much would you like to bet, in milimeters?" << endl;
cout << "(You still have " << remaining << " milimeters left.)" << endl;
cin >> input;
betCheck(input, remaining);
// Match loop
if (betCheck(input, remaining) == true) {
cout << "You can win " << prizeCheck(input, round) << " Yen." << endl << endl;
cout << "Cash currently at hand: " << cash << endl;
Node* head = createKaijiDeck(round);
List* root = createTonegawaDeck(round);
do {
printDeck(head);
cout << "Select a card to play [1 - 5]: ";
cin >> cardIndex;
randTonegawa = (rand() % (5 - countTonegawa));
Node* selectKaijiCardAtIndex = selectKaijiCard(head, cardIndex);
cout << "You chose the card: " << selectKaijiCardAtIndex->cards << endl;
List* selectTonegawaCardAtIndex = selectTonegawaCard(root, randTonegawa);
cout << "Tonegawa chose the card: " << selectTonegawaCardAtIndex->card << endl;
cout << endl;
countTonegawa++;
} while (winCheck(root, head, cardIndex, randTonegawa) == 0);
// Match up checker (Emperor > Citizen > Slave > Emperor)
if (winCheck(root, head, cardIndex, randTonegawa) == 1) {
cash = cash + prizeCheck(input, round);
cout << "Round " << round << " winner is Kaiji." << endl;
cout << "You won " << prizeCheck(input, round) << " Yen!" << endl;
}
else if (winCheck(root, head, cardIndex, randTonegawa) == 2) {
remaining = remaining - input;
cout << "Round " << round << " winner is Tonegawa." << endl;
cout << "The pin moved by " << input << " milimeters!" << endl;
}
}
else if (betCheck(input, remaining) == false)
{
cout << "You lose! You already lost your ear!" << endl;
system("pause");
exit(0);
}
}
return 0;
}
Node *createKaijiDeck(int round) {
Node* head = NULL;
Node* curr = NULL;
Node* prev = NULL;
if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {
curr = new Node;
curr->cards = "Emperor";
prev = curr;
head = curr;
for (int i = 0; i < 3; i++) {
curr = new Node;
curr->cards = "Citizen";
prev->next = curr;
prev = curr;
}
curr = new Node;
curr->cards = "Citizen";
prev->next = curr;
}
if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {
curr = new Node;
curr->cards = "Slave";
prev = curr;
head = curr;
for (int i = 0; i < 3; i++) {
curr = new Node;
curr->cards = "Citizen";
prev->next = curr;
prev = curr;
}
curr = new Node;
curr->cards = "Citizen";
prev->next = curr;
}
return head;
}
Node *selectKaijiCard(Node *head, int indexK) {
for (int i = 0; i < indexK - 1; i++) {
head = head->next;
}
return head;
}
Node *deleteSelectedKaijiCard(Node *head, int indexK) {
Node *curr = NULL;
if (indexK == 1)
{
curr = head;
head = head->next;
delete curr;
return head;
}
Node *deleteCard = head;
for (int i = 0; i < indexK - 1; i++) {
curr = deleteCard;
deleteCard = deleteCard->next;
}
curr->next = deleteCard->next;
delete deleteCard;
return head;
}
List *createTonegawaDeck(int round) {
List *root = NULL;
List *front = NULL;
List *tail = NULL;
if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {
front = new List;
front->card = "Slave";
tail = front;
root = front;
for (int i = 0; i < 3; i++) {
front = new List;
front->card = "Citizen";
tail->next = front;
tail = front;
}
front = new List;
front->card = "Citizen";
tail->next = front;
}
if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {
front = new List;
front->card = "Emperor";
tail = front;
root = front;
for (int i = 0; i < 3; i++) {
front = new List;
front->card = "Citizen";
tail->next = front;
tail = front;
}
front = new List;
front->card = "Citizen";
tail->next = front;
front->next = root;
}
return root;
}
List *selectTonegawaCard(List *root, int indexT) {
for (int i = 0; i < indexT; i++) {
root = root->next;
}
return root;
}
List *deleteSelectedTonegawaCard(List *root, int indexT) {
List *front = NULL;
if (indexT == 0)
{
front = root;
root = root->next;
delete front;
return root;
}
List *deleteTonegawaCard = root;
for (int i = 0; i < indexT; i++) {
front = deleteTonegawaCard;
deleteTonegawaCard = deleteTonegawaCard->next;
}
front->next = deleteTonegawaCard->next;
delete deleteTonegawaCard;
return root;
}
bool betCheck(int betLength, int remainingLength) {
bool flag;
if (betLength <= remainingLength) {
flag = true;
}
else if (betLength > remainingLength) {
flag = false;
}
return flag;
}
int prizeCheck(int betLength, int round) {
int yen;
if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {
yen = betLength * 100000;
}
if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {
yen = betLength * 500000;
}
return yen;
}
void printDeck(Node * head) {
int count = 1;
cout << "===============" << endl;
cout << "Kaiji's cards" << endl;
cout << "===============" << endl << endl;
while (head != NULL) {
cout << count << ". " << head->cards << endl;
head = head->next;
count++;
}
cout << endl;
}
int winCheck(List *root, Node *head, int cardIndex, int randTonegawa) {
int result = 0;
if ((head->cards == "Citizen") && (root->card == "Citizen")) {
result = 0;
}
else if ((head->cards == "Emperor") && (root->card == "Citizen") || (head->cards == "Slave") && (root->card == "Emeperor") || (head->cards == "Citizen") && (root->card == "Slave")) {
result = 1;
}
else if ((root->card == "Emperor") && (head->cards == "Citizen") || root->card == "Slave" && head->cards == "Emperor" || root->card == "Citizen" && head->cards == "Slave") {
result = 2;
}
head = deleteSelectedKaijiCard(head, cardIndex);
root = deleteSelectedTonegawaCard(root, randTonegawa);
return result;
}
void gameOver(int cash, int round, int input, int remaining) {
if (round <= 12 && cash == 20000000 && betCheck(input, remaining) == true) {
cout << "You did not entirely win! You only got " << cash << " Yen in 12 rounds!" << endl;
system("pause");
exit(0);
}
else if (round == 12 && cash < 20000000 && betCheck(input, remaining) == false) {
cout << "You won! You got" << cash << " Yen at Round " << round << endl;
}
}
List.h
#pragma once
#include <string>
using namespace std;
struct List {
string card;
List* next = NULL;
};
Node.h
#pragma once
#include <string>
using namespace std;
struct Node {
string cards;
Node* next = NULL;
Node* prev = NULL;
};
It looks like when you select a card to play, you then delete that card from the deck, then compare the first card of the two decks. That doesn't seem right, surely you want to compare the cards you just selected?
If so, you should be calling winCheck with selectKaijiCardAtIndex and selectTonegawaCardAtIndex. You also want to do this before you delete the cards from the deck as the deleteSelectedKaijiCard function actually deletes the card so you can't use it after that.
This probably mean rearranging your code a bit and not call wincheck in so many places.