Jump to content
  • Advertisement

Archived

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

Justaddwater

Help with C++ (again) *sigh*

This topic is 5441 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
Advertisement
Guest Anonymous Poster
you should try the forums on homeworkdev.net

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!