The exact problem I am having is that my Upload() function isnt working and I dont know why? Code is below and class is attached.
SOURCE:
#include<iostream>
#include<string>
#include<cstddef>
#include<fstream>
#include<vector>
#include<iomanip>
#include"LinkedList.h"
using namespace std;
int main()
{
List list;
fstream inData;
ofstream outData;
string data;
inData.open("Cigars.txt");//For retrieving information from file
outData.open("Cigars.txt");//For saving to the file
list.Upload(inData);
list.Directions();
list.Interface();
list.Save(outData);
return 0;
}
HEADER/CLASS FILE
using namespace std;
struct Node
{
string Brand;//Brand of Cigar
string Name;//Specific name of the cigar
double Price;//Price per stick
double DrawRating;//Rating is 1 - 5
double AromaRating;//Rating is 1 - 5
double TasteRating;//Rating is 1 - 5
double OverallRating;//Rating is the average of Draw, Aroma, and Taste
Node *next;
};
class List
{
public:
List();//Class Constructor
~List();//Class Destructor
void Delete(int x);//Deletes cigar in list
void MakeEmpty();//Deallocates all pointers in the list
void Insert();//Inserts item in list
void Save(ofstream &outData);//Writes the list to the file just before termination of program
void Directions();
void FunctionList();
void Interface();
void GetLength() const;//Returns the length of the list
void Upload(fstream &inData);//Uploads data from file to linked list
bool IsThere(int x);//Returns true or false is the item is in the list or not
private:
Node *head;//Node pointer
int length;//Variable for how long the list has become.
vector<double> Ratings;
double Average(vector<double> &vec);
};
List::List()//Initializes private class variables
{
length = 0;
head = NULL;
}
List::~List()//Deallocates pointers in the linked list(emptying the list).
{
Node *tempPtr;
while(head != NULL)
{
tempPtr = head;
head = head->next;
delete tempPtr;
}
length = 0;
}
void List::Insert()
{
/*
Pre: Nothing has yet been inserted.
Post: Cigar information has been entered successfully into the list
*/
Node *NewPtr;//Declaring a Node pointer
NewPtr = new Node;//Setting a pointer to a new Node created in memory
cout << "Input Brand of Cigar -> ";
cin >> NewPtr->Brand;
cout << "Input Name of Cigar -> ";
cin >> NewPtr->Name;
cout << "Input Price of Cigar -> ";
cin >> NewPtr->Price;
cout << "Input Draw Rating(1 - 5) -> ";
cin >> NewPtr->DrawRating;
Ratings.push_back(NewPtr->DrawRating);
cout << "Input Aroma Rating(1 - 5) -> ";
cin >> NewPtr->AromaRating;
Ratings.push_back(NewPtr->AromaRating);
cout << "Input Taste Rating(1 - 5) -> ";
cin >> NewPtr->TasteRating;
Ratings.push_back(NewPtr->TasteRating);
//Private member function that finds the average of the
//ratings for the overall rating.
NewPtr->OverallRating = Average(Ratings);
//cout << setprecision(2) << NewPtr->OverallRating << endl;
NewPtr->next = head;//Giving the pointer inside the node an address.
head = NewPtr;//head now has the address of NewPtr
length++;
cout << "INPUTTED SUCCESSFULLY\n\n";
}
double List::Average(vector<double> &vec)
{
double answer, total = 0;
for(int i = 0; i < vec.size(); i++)
{
total = total + vec[i];
}
answer = total/(vec.size());
return answer;
}
void List::GetLength()const
{
/*
Pre:
Post: The number of nodes in the list has been given to the user
*/
cout << "Number of Cigars in Database is: " << length << "\n\n";
}
void List::Delete(int x)
{
/*
Pre: Item in the list but not deleted
Post: Item no longer in the list because of deletion
*/
/*
Node *NewPtr = head;
Node *tempPtr;
if(x == head->data)
{
tempPtr = NewPtr;
head = head->next;
}
else
{
while(!(x == (NewPtr->next)->data))
NewPtr = NewPtr->next;
tempPtr = NewPtr->next;
NewPtr->next = (NewPtr->next)->next;
}
length--;
delete tempPtr;
*/
}
bool List::IsThere(int x)
{
/*
Pre:
Post: Returns true if item is in the list. Returns false if the item is not in the list.
*/
/*
Node *tempPtr = head;
while(tempPtr != NULL)
{
if(tempPtr->data == x)
{
return true;
break;
}
else
tempPtr = tempPtr->next;
}
if(tempPtr == NULL)
return false;
delete tempPtr;
*/
return true;
}
void List::MakeEmpty()
{
/*
Pre: List is not empty
Post: List is now empty
*/
/*
Node *tempPtr = head;
while(head != NULL)
{
head = head->next;
delete tempPtr;
length--;
}
*/
}
void List::Directions()
{
cout << "DIRECTIONS:\n\n"
<< "Type in the function you wish to perform in all\n"
<< "capital letters then press ENTER. Place no spaces\n"
<< "in words typed in. Enter EXIT when finished.\n\n"
<< "*** For a list of usable functions type in FUNCTIONLIST ***\n\n";
}
void List::FunctionList()
{
string functions[] = {"INSERT", "DELETE", "PRINT", "GETLENGTH", "ISTHERE"};
for(int i = 0; i < 5; i++)
{
cout << functions[i] << endl;
}
cout << endl;
}
void List::Interface()
{
string function;
while(function != "EXIT")
{
cout << "=> ";
cin >> function;
if(function == "INSERT")
Insert();
else if(function == "FUNCTIONLIST")
FunctionList();
else if(function == "GETLENGTH")
GetLength();
else if (function == "EXIT")
{
}
else
{
cout << "*** FUNCTION DOES NOT EXIST ***\n"
<< " *** TRY AGAIN ***\n";
}
}
}
void List::Save(ofstream &outData)
{
Node *newPtr = head;
while(newPtr != NULL)
{
outData << newPtr->Brand << endl;
outData << newPtr->Name << endl;
outData << newPtr->Price << endl;
outData << newPtr->DrawRating << endl;
outData << newPtr->AromaRating << endl;
outData << newPtr->TasteRating << endl;
newPtr = newPtr->next;
}
outData.close();
}
void List::Upload(fstream &inData)
{
//Uploads contents of file to linked list
string brand, name;
double price, taste, draw, aroma;
while(inData)
{
inData >> brand;
//cout << brand << endl;
inData >> name;
//cout << name << endl;
inData >> price;
//cout << price << endl;
inData >> draw;
//cout << draw << endl;
inData >> aroma;
//cout << aroma << endl;
inData >> taste;
//cout << taste << endl;
}
inData.close();
}

New Topic/Question
Reply



MultiQuote








|