Thread: link lists, pointers, using input\output file

  1. #1
    Registered User
    Join Date
    Oct 2005

    link lists, pointers, using input\output file

    I used a basic input file to input 3 rows of sales data like 300 300 300. I must use link lists and pointers and at the same time use a class to input my data and output it. I need a average fuction that will add the code and do a average,and output everything to the oupt file. I have ran into a block any help would be appreciated.

    #include <iostream>
    #include <cstddef>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    class TotalSales
    	 overloaded operators +, -, /,  and * declared to calculate 2 numbers
    	friend TotalSales operator +(const TotalSales& sale1, const TotalSales& 
    	friend TotalSales operator -(const TotalSales& sale1, const TotalSales& 
    	friend TotalSales operator *(const TotalSales& sale1, const TotalSales& 
    	friend TotalSales operator /(const TotalSales& sale1, int denominator);
    	 Overloaded operators << and >>, inputs and outputs number totals
    	friend istream& operator >>(istream& ins, TotalSales& sale1);
    	friend ostream& operator <<(ostream& outs, TotalSales& sale2);
    	 Declared constructor that intializes the member variable and intializes it
    to zero
    	TotalSales(double jan, double feb, double mar, double 
    totals1);//constructor intializes member variables
    	TotalSales ();  //constructor intialzes member variables to zero
    	 Accessor function returns jantotals, febtotals. martotals, totals.
    	double get_jantotals();
    	double get_febtotals();
    	double get_martotals();
    	double get_totals();
    	 Declared the member variables in private
    	double jantotals;
    	double martotals;
    	double febtotals;
    	double totals;
    	double average();
    	int quarterly();
    	typedef struct Node* lref, lnode;
    	typedef struct Node
    		double data;
    		lref next;
    	total_average(lref& head);
    int main()
      outputs the file
    	ifstream fins;
    	ofstream fout;
    	gives the array a variable name
    	lref head;
    	head = NULL;
    	lref end;
    	lref tail;
    	end = new Node;
    	if (head == NULL)
    		head = end;
    		tail= end;
    		end->next = new Node;
    	//int array_size;
    	//lref size;
    	//size = new Node[array_size];
    	TotalSales amount;   //************ declares the objects being used with 
    the class
"ins.txt");  //opens the input file
    	if (
    		cout <<"Input file failed." << endl;
"outs.txt");  //opens the output file
    	if (  //  will output a error if the output file fails
    		cout << "Output file failed." << endl;
    	gives the decimal point and sets it to 2
    	outputs the average and total info to the output file for display
    	fout << "January" <<setw(15) << "February" << setw(15) << "March" << 
    setw(15) << "Average" << endl;
    		double average(lref head);
    		fins >>  head->data;
    		fout << head->data;
    while (! fins.eof());
    	fout.close();  //closes the output file
    	delete head, end, tail;
    	system("pause"); // pauses the screen after the program runs
    	return 0;

  2. #2
    Registered User
    Join Date
    Mar 2002
    Here's some stuff to chew on.

    Why not use classes/structs more effectively? Start out declaring a Sale class, which is a struct with a data member and a pointer to the sale object called next. (Note: in C++ you don't need to use the keyword typedef before keyword struct in declaration new struct types. In addition don't declare the name an object the same as the name of the struct/class, ie don't have both a class called Node and an object of the Node class called Node. And finally, use names that are helpful. For example, using two pointers called tail and end can be very confusing. I'd consider changing end to current in keeping with the way you used end in your code.)

    Then create a MonthlySales class will be a list of each Sale for that month. Use a method that allows you to enter the Sale data one Sale at a time to the end of the list and a second method to add up all the Sales in the list and keep the total as a member variable. You will also need a member variable that is a pointer to a MonthlySales object, called next if you like; and it would be useful to have other data members like pointers to Sale objects called head, tail, and current.

    Then declare a TotalSales class. This will have a data member that is a list of MonthlySales. So it will need pointers to MonthlySales called head, tail, and current in addtion to methods to add MonthlySales objects to end of the list of Monthly sales, add upt all the totals of the each of the MonthlySales, calculate an average of all the MonthlySales, etc.

    In essence this will create a list of lists and you may end with sytax like this:


    which would be the salesAmount of the currentSale of currentMonthlySales in the list of MonthlySales called totalSales.

    I'd definitely write the MonthlySales class method by method before trying to extend that experience to TotalSales.

    If you're not prohibited from using the STL list class then most of the burden of writing the methods for your own list class goes away, but forcing you to roll your own is a classic exercise to gain experience with both lists and pointers. However, giving you a task that's probably best done by writing a list of lists is a little over the top, IMO; for what that's worth.

    Good luck.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. possible to link and run with .lib file without .dll file?
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 05-25-2006, 09:05 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM