Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help, C++ & Linked List Output :(
New Posts  All Forums:Forum Nav:

Help, C++ & Linked List Output :(

post #1 of 3
Thread Starter 
Code:
//Project Three
//Christopher Coughlan
//Hire Purchase Firm

#include <iostream>
#include <string>

using namespace std;

class hire
{
public:

char check;

struct node
{
int acno;
char name[50];
float bal;
char acstatus;
int daypay;
int monthpay;
node *nxt;
};//node

node *temp,*temp2;
node *start_ptr;

hire::hire()
{
check = 'Y';
};

void add()
{

while((check == 'Y') || (check =='y'))
{


node *start_ptr = NULL;

temp = new node;

cout << "Enter Account Number: ";
cin >> temp->acno;
cin.ignore();
cout << "Enter Name: ";
cin.getline(temp->name,50);
cout << "Enter Balance: ";
cin >> temp->bal;
cout << "Enter Account Status: ";
cin >> temp->acstatus;
cout << "Enter Day Of Payment: ";
cin >> temp->daypay;
cout << "Enter Month Of Payment: ";
cin >> temp->monthpay;

temp->nxt = NULL;

if (start_ptr == NULL)
start_ptr = temp;

else
{

temp2 = start_ptr;

while (temp2->nxt != NULL)
{  
temp2 = temp2->nxt;
}

temp2->nxt = temp;
}

cout << "Would You Like To Enter More? (Y or N): ";
cin >> check;

}//while

}//add

void display()
{
node *temp;
temp = start_ptr;
if(temp == NULL)
cout << "The List is Empty!" << endl;
else
{
while (temp != NULL)
{
if (temp == NULL)
cout << "End of list" << endl;
else
{  
cout << "Account Number: " << temp->acno << endl;
cout << "Name: " << temp->name << endl;
cout << "Balance: " << temp->bal << endl;
cout << "Account Status: " << temp->acstatus <<endl;
cout << "Date Of Payment: " << temp->daypay << "/" << temp->monthpay << endl;
cout << endl;
temp = temp->nxt;
}
cout << "End of List!" << endl;
}
}//else


}//display

};//class hire

int main()
{
hire purchase;

purchase.add();
purchase.display();
return 0;

}
For Some reason it wont output, the program just stops responding when I finish the input...where am I going wrong?
post #2 of 3
You are using two start_ptr variables. One is a class member and the other is local to the add(). The local variable is "hiding" the class member.
post #3 of 3
Ok yeah, like spotswood said, you have *start_ptr, *temp, and *temp2 defined in your class, but then you redefine another variable with the same name in each function. So when you use that variable in your function, you are using the local variable, not the class variable. That's the first problem.

The second problem is in the logic of the 'add()' function. These are the lines that cause the error:
Code:
void add()
{

while((check == 'Y') || (check =='y'))
{
node *start_ptr = NULL;
.....
.....
.....
if (start_ptr == NULL)
start_ptr = temp;
}//while

}//add
In other words, every time it loops through that code it sets the start pointer to the node you just added - not to the original node.

To fix it, just remove the variable's definition from there and add this to the class's constructor:
Code:
start_ptr = NULL
That should fix both problems at once.

Speaking of the constructor, since you are implementing it within the class definition itself, you don't need to include the scope. Remove the "hire::" from the beginning, and remove the semicolon after the brackets.
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help, C++ & Linked List Output :(