Help with C++ (again) *sigh*

Started by
16 comments, last by Justaddwater 20 years, 4 months ago
quote:Original post by rohde
quote:Original post by tHiSiSbOb
To which function are you trying to send total????

[edited by - tHiSiSbOb on November 29, 2003 4:24:59 PM]

[edited by - tHiSiSbOb on November 29, 2003 4:27:48 PM]

[edited by - tHiSiSbOb on November 29, 2003 4:28:28 PM]

[edited by - tHiSiSbOb on November 29, 2003 4:29:56 PM]


*LOL* Off-topic: I like that you have ONE sentence wiht NINE words, but that you felt the need to edit FOUR times!!!! Talk about overkill....


"Yeah, I would''ve killed you, but I''m glad I didn''t - the paperwork is a bitch"


haha. I knew that would attract attention. Blame my internet. It only gave me part of the message; and I replied to this part. Then I realised that I messed up, changed it, then posted something else, and realised that I was misunderstanding the question, then just put that sentence. lol.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
Advertisement
Hello Justaddwater,

look at the code you have I would say you passing in a file stream which is at the end. your looping until eof.

use the tellg of the file stream to get the size of the file.
tellg tells you how big the file is.
are if you still looping until eof, then use seekg will go to a offset in the file. in this case seekg(0) will take you back to the beginning of the file.

then allocate your memory and pass the file stream to your loadlist with your new array.

Hope that helps.

edited:

Ok I see what your trying to do. Long day at work on debug thread/socket code slowing down my mind.

Ok you wanted number of lines.
do what you do but call fin.seekg(0) before you call loadlist.
then youshould be fine. at lest formthe stand point your at the begining of fin now.

Lord Bart

[edited by - lord bart on November 29, 2003 6:31:30 PM]
hey,

Thanks for all your ideas, I still cannot get it to work right, I have now put this in main


	ifstream fin;	fin.open (inFile);	book cbook[500];loadlist(cbook, fin);


Then this in the loadlist function;

string temp;	int total =0;	ifstream fin;	fin.open (inFile);	for  (int x = 0; x < 500; x++)	{		while (!fin.eof())		{		getline (fin, temp);		total++;		}


And I sort of get what I want, but not really. I cannt tell how to manipulate that array I have created, or pass a value to it or take one from it.

Below is the entire code as it lasy so far, and any ideas are awesome... I''m glad I have two weeks to get this working

#include <fstream>#include <iostream>#include <string>#include <cmath>#include <iomanip>#include <cctype>using namespace std;const int total = 5;#define inFile "Booklist.txt"#define outFile "Booklist2.txt"void display();void search();void add();int menu(int);class book{	public:		book (); //default constructor		book (string, string, string, string, string, string); //non-default		string BookCode;		string Year, Price;		string FName, LName, Title;		private:		};book::book (){	//cout << "in Default contructor\n";	BookCode = "5555555555";	Year = "2000";	Price = "50";	FName = "John";	LName = "Doe";	Title = "Reading for Dummies";}book::book (string bc, string yr, string pr, string fn, string ln, string ti){	//cout << "In non-default constructor\n";	BookCode = bc;	Year = yr;	Price = pr;	FName = fn;	LName = ln;	Title = ti;}void loadlist (book[], ifstream &);void main (){	ifstream fin;	fin.open (inFile);	book cbook[500];//500 is MAX size of book listloadlist(cbook, fin);	//menu(1);//MENU TURNED OFF 		system ("pause");}int menu (int x)//SKELETON MENU SYSTEM{cout << "Welcome to the Book Cataloging System\n please make your choice below: \n";cout << "(1) Display Catalog.\n";cout << "(2) Search the Catalog. \n";cout << "(3) Add a book to the catalog. \n";cout << "(4) Quit. \n";cin >> x;switch (x){	case 1 : display();		break;	case 2 : search();		break;	case 3: add();		break;	case 4 : exit(1);		break;}return 1;}void display()//PLACEHOLDER{	cout << "DISPLAYING LIST\n";}void search()//SKELETON FOR SEARCH TOOL{	char y;	cout << "Search List by..\n Author (F)irst name, (L)ast name or (B)ook code?\n";	cout << "Enter (F), (L) or (B): " <<endl;	cin >> y;	switch (y)	{	case ''f'' : 		cout << "SEARCHING BY FIRST NAME...\n";		break;	case ''F'' : cout << "SEARCHING BY FIRST NAME...\n";		break;	case ''l'' : cout << "SEARCHING BY LAST NAME...\n";		break;	case ''L'' : cout << "SEARCHING BY LAST NAME...\n";		break;	case ''b'' : cout << "SEARCHING BY BOOK CODE...\n";	    break;	case ''B'' : cout << "SEARCHING BY BOOK CODE...\n";	    break;	default: cout << "INVALID\n";				 break;	}}void add()//PLACEHOLDER FOR FUNCTION{	cout << "ADD A BOOK";}void loadlist (book inout[], ifstream & inF ){	string temp;	int total =0;	ifstream fin;	fin.open (inFile);	for  (int x = 0; x < 500; x++)	{		while (!fin.eof())		{		getline (fin, temp);		total++;		}		}	cout << total;	system ("pause");fin.seekg(0);//Used to GOTO start of file	for (int i = 0; i < total; i++)	{		getline (inF, inout[i].BookCode, ''\t'');		getline (inF, inout[i].FName, ''\t'');		getline (inF, inout[i].LName, ''\t'');		getline (inF, inout[i].Title, ''\t'');		getline (inF, inout[i].Year, ''\t'');		getline (inF, inout[i].Price, ''\n'');											//inF.ignore (1);				cout << inout[i].BookCode;			cout << " " << inout[i].FName;				cout << " " <<inout[i].LName;			cout << " " <<inout[i].Title;			cout << " " <<inout[i].Year;			cout << " " <<inout[i].Price <<endl;	}fin.close ();}


Ok thats it!

I can;t belive how much fun C++ is , although as you can clearly see I''m JUST LEARNING it is a blast!
int* iPtr;
iPtr = new int [total];
//total = total -1;
book cbook[total];

you have this try doing this instead:

// this is the same
int* iPtr;
iPtr = new int [total];
//total = total -1;

// this is different
book *cbook = new book[total];

and make sure you include your header file about book.
Its not a bug, its a feature!!
ARRAYS = BAD!!!!

well kinda, ive been indoctrinated to hate them though, i would say the best way to go about this would be to create a linked list, this means you would need a member of your class or struct to have a member
book* prev;
book* next;

and these pointers would point to the previous and next books in the list, you can simply allocate a book every time u load one, and make the prev point to the one b4 it (this would require a temp book pointer) its really not hard for a c++ 1337 master like me but i mean what can i say? i like to help the weak minded

just so yah know, im kidding, im still a n00b
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Try using:

iPtr = new int (total);

instead of:

iPtr = new int [total];
Thanks for your ideas, I''m still working on getting it to work right, I think I am just not understanding the logic behind what I''m tryign to do becouse NOTHING is working right, heh, I guess back to the book... more reading... anyway I''m still open to any other ideas, I have learned a lot of stuff from just playing with you guys/girls suggestions!
The compiler is (was) giving the message, "expected constant expression" because when you create an array on the STACK (ie, book cbook[total]), the size of the array MUST BE KNOWN AT COMPILE-TIME; that is, the size (total) must be CONSTANT, not variable.

Thus if you want to dynamically allocate an array at runtime (you can''t say how big it is at compile-time), you must allocate it on the HEAP using the "new" operator. This operator returns a POINTER to the new data that you have created.

Eg)
int *a = new int [total];  // create an array of "total" integersfunc(a, total);            // pass the array (and size) to funcdelete [] a;               // free memory (must have for every new)...void func(int *array, int size){  // Go through the array  for (int i = 0; i < size; i ++)    array[i] = ...}


Notice that since the size of the array is not known at compile-time, you almost always have to pass the array SIZE as well as the array pointer to the function.

Also note that for every NEW that you have, there should be a corresponding DELETE when you are done with the data. You''ll get used to this one though and it becomes pretty simple. Just REMEMBER TO DO IT, or you have a memory leak

If you have any further questions, please ask.

This topic is closed to new replies.

Advertisement