I am trying to use a linked list to read in data, do calcuations and then display it.
I am new to using linked lists, so right now I'm only trying to display them to see if it works.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <list>
using namespace std;
int main()
{
char d_type;
int d_quant;
double d_price;
struct data
{
char type;
int quant;
double price;
data *next; //ptr to next data set
};
data *head;
head = NULL;
ifstream widgets("widgets3.txt");
if (!widgets)
{
cerr << "Cannot open file" << endl;
}
for (int i=0; i<2; i++)
{
data *newdata;
data *data_ptr;
newdata = new data;
newdata-> type = d_type;
newdata-> quant = d_quant;
newdata-> price = d_price;
newdata-> next = NULL;
if (!head)
head = newdata;
else
{
data_ptr = head;
while (data_ptr-> next)
data_ptr = data_ptr-> next;
data_ptr->next = newdata;
} //closes if(!head)
data *Display_ptr;
Display_ptr = head;
while (Display_ptr)
{
cout << Display_ptr-> type << endl;
cout << Display_ptr-> quant << endl;
cout << Display_ptr-> price <<endl;
Display_ptr = Display_ptr->next;
}
} //closes for loop
widgets.close();
system ("PAUSE");
return 0;
};
This program works, but it loops the same thing over and over, and it doesn't move on to the next selection. I was thinking of putting it in an array, but when I try I get an error.