Archived

This topic is now archived and is closed to further replies.

Help with C++ (again) *sigh*

This topic is 5129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, Ok I'm working on a project for school and I seem to be a bit stumped, if anyone could point me in the right direction I would be very grateful. Here is what I'm trying to do 1) Open a file <this works fine> 2) Determine how long my file is <this works fine> 3) Then send that array length to a function to work with- It won't let me send my varible "total" to the function as a array, I know that arrays are supposed to be constant but I understand from my books and professor that using a pointer I can dynamically set that array size, I just can't get it to actually work! I hope this is clear enough what my problem is, thanks again cheers snip pasted below
fin.open (inFile);

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

	int* iPtr;

	iPtr = new int [total];

	//total = total -1;

	book cbook[total];
	loadlist (cbook, fin);
[edited by - justaddwater on November 29, 2003 4:24:13 PM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Anonymous Poster,

I don''t think there is anything wrong with asking for help when your stuck. I didn''t ask anyone to complete my homework for me, just help. I know that you were attempting to be a funny person, and I do commend you on your efforts. If you wish for some help in understanding on how to post with a name instead of Anonymous Poster I''m certain you could also find that useful information on the website you linked.

cheers

Share this post


Link to post
Share on other sites
What errors are you getting? And post the code to the function, too.

--------
"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin
"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotix
the D programming language

Share this post


Link to post
Share on other sites
The reason is because your array of books there is being done at compile time. So total needs to be constant, otherwise you need to do the same thing as the pointer on the line just above and make it with new, so that it will do it at run time.

Share this post


Link to post
Share on other sites
CyberJay- I''m not 100% sure what you mean but I will play with it a bit.

Here is the function loadlist as I have it so far, the part in the for loop "i > 2" is just holding the place of what I would like to say "i > end of file" i.e. "i > total"


void loadlist (book inout[], ifstream & inF )
{

for (int i = 0; i < 2; 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;
}

}


thanks

Share this post


Link to post
Share on other sites
quote:

What errors are you getting? And post the code to the function, too.



Code is in previous post, errors are

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: ''cbook'' : unknown size

All three errors on this line

book cbook[total];


Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 list


loadlist(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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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" integers

func(a, total); // pass the array (and size) to func

delete [] 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.

Share this post


Link to post
Share on other sites