0
mailin

C++ help?

Recommended Posts

Hey all,

Doubt anyone can help, but I'll try anyway.

I've emailed my prof. 4 times since last night and he hasn't gotten back to me and I'm so stuck.



void loadArray( ifstream, char[][MAX_LENGTH_OF_NAMES]);

loadArray( infile, namesArray );


//----------------------------------------------------------------------
void loadArray( ifstream input, char array[][MAX_LENGTH_OF_NAMES] )
//----------------------------------------------------------------------
{
int j = 0;

while( input != 0)
{
input.getline( array[j], 80, ',' );
cout << "Yo!" << array[j] << endl;
j++;
}

}



Thats the relivent info - but its not reading from the file into my array [:/]

Of course its due tomorrow, and of course this should be simple, but it's not for me I guess...

Any and all help appreciated!!

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Does 'Yo!' ever get printed? Does your program fail with an error message? I won't give you a solution, but I'll point you in the right direction...

...to that end, some random thoughts:

1- Have you instantiated and opened the ifstream? Have you instantiated the array?

2- Your while loop should not be testing to see if the input object is 0/NULL, it should be testing to see if there are more characters to be read:


while(!input.eof()) { //do stuff }

If input is non-null, then nothing in the code you've posted will ever set it to null, so testing it all the time to see if it's null is a bit pointless.

3- You should also check for input failure after reading (using input.fail()).

4- I would mark you down for using MAX_LENGTH_OF_NAMES in your function declaration and 80 in the definition. Constants everywhere, please.

5- What happens if the file contains 10 million lines? You have no check in place to make sure you don't write past the end of the array.

6- Good practice is for the first line in your function should be a check to see if either input parameter is null, and if it is, just return immediately. Defensive programming can save your arse in the real world.

That'll do for now! C++ is a big hard language, so be patient and always pay attention to freeing memory.

Share this post


Link to post
Share on other sites
It works for me... I'm guesing probably not exactly like you're expecting it to from looking at the code, but it does read the lines in from the file. You are passing an open ifstream in aren't you? That's the only thing I had to do to make it read the text file.

Share this post


Link to post
Share on other sites
Apologies in advance for the font mix up in this post - don't feel like editing the array stuff because it conflicts with the site here.

Quote

Does 'Yo!' ever get printed? Does your program fail with an error message? I won't give you a solution, but I'll point you in the right direction...



Thanks for the reply. Yes, "Yo!" gets printed, but only once. No error message is given.

Quote

1- Have you instantiated and opened the ifstream? Have you instantiated the array?



Yes and Yes.

These are the two functions I'm working with for this part.


const MAX_LENGTH_OF_NAMES = 21;
const NUMBER_OF_LAST_NAMES = 50;

char namesArray[NUMBER_OF_LAST_NAMES][MAX_LENGTH_OF_NAMES];
void openInput( ifstream );
void loadArray( ifstream, char[][MAX_LENGTH_OF_NAMES]);

void main()
{
ifstream infile;
openInput( infile );
loadArray( infile, namesArray );

}

//----------------------------------------------------------------------
void openInput( ifstream input )
//----------------------------------------------------------------------
{
input.open("input.txt");

if(input == 0)
{
cerr << "Error opening input file!" << endl;
exit(1);
}

}

//----------------------------------------------------------------------
void loadArray( ifstream input, char array[][MAX_LENGTH_OF_NAMES] )
//----------------------------------------------------------------------
{
int i = 0;

while( input != 0)
{
input.getline( array, 80, ',' );
cout << "Yo!" << array << endl;
i++;
}

}***

Quote


2- Your while loop should not be testing to see if the input object is 0/NULL, it should be testing to see if there are more characters to be read:



Doing that in VC++ 6.0 I get an error message, this was was the only way that worked. Is what I have different than

while( !input.eof()) ?

Quote


If input is non-null, then nothing in the code you've posted will ever set it to null, so testing it all the time to see if it's null is a bit pointless.



I agree. I originally stuck a for loop in the while loop, but the redundancy was a total memory waste. I can't come up with another way to do it.

Quote


3- You should also check for input failure after reading (using input.fail()).



haven't seen that method yet... I'll look into it. Thanks!

Quote


4- I would mark you down for using MAX_LENGTH_OF_NAMES in your function declaration and 80 in the definition. Constants everywhere, please.



Hehe - my professor is the EXACT opposites - we can't use constants.

Quote

5- What happens if the file contains 10 million lines? You have no check in place to make sure you don't write past the end of the array.



So using the for loop in my while loop:



while (input !=0)
{
for ( int i = 0, i < MAX_LENGTH_OF_NAME; i++)
{
input.getline( array, 80, ',' );
cout << "Yo!" << array << endl;
}



Would solve that?


Quote

6- Good practice is for the first line in your function should be a check to see if either input parameter is null, and if it is, just return immediately.



You can see from the other function in this program that I'm already checking for that.

Quote

Defensive programming can save your arse in the real world.



Agreed! :)
Thanks for the advice!!

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Bullshit. Don't make statements you can't back up. >:(

I've written multi-billion-dollar-mission-critical systems using VC++6. It works. Well.

The STL error messages are a fucking nightmare (it expands all templates) but otherwise I'd absolutely no problems with it. Neither did the other six C++ developers on the team.

Microsoft's development tools are, in general, excellent.

Share this post


Link to post
Share on other sites
I have to agree here... I've got 6.0 here at home and the latest and greatest at work. I do most of my C++ on a SUN machine and use VC++ mostly for odd jobs these days but I haven't really had any problems with any of the MS dev tools.

Share this post


Link to post
Share on other sites
VC++ is the only compiler that doesn't let you reuse varaible names even when they are out of scope. This is just one problem in a list of many.

I can back it up because I've used others - but I am required in this course to use VC++ 6.0.

Don't assume.

Thanks for everyones help, the problem is a pass by reference vs. pass by value on the ifstream.

Problem fixed.

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
Quote

VC++ is the only compiler that doesn't let you reuse varaible names even when they are out of scope.


What? Either I'm misunderstanding you, or you're wrong. Yes it does. It wouldn't be a hell of a lot of use as a compiler if it didn't, would it?

Quote

Don't assume.


I'm not assuming. You're just starting to learn C++. You don't know the iostream library. Ergo, you don't yet know enough to be comparing compilers or development environments.
Note: I'm not saying VC++6 is the best thing since sliced bread. But it's pretty damn good.

Share this post


Link to post
Share on other sites
try using a variable declared in a for loop again.

for( int i = 0; i < 30; i++) cout << i << endl;

i is now out of scope.

use i in a for loop in the same function again - it won't work, but will in other compilers.

I'm off to do my JAVA work now..... enjoy bickering over the value of VC++

Jen
Arianna Frances

Share this post


Link to post
Share on other sites
i isn't out of scope until the end of the function that the for loop is in or the enclosing block {} of code to be more accurate. If you declared it within the braces of the for loop it would go out of scope at the closing brace of the for loop, but declaring the variable in the parenthesis of the for loop is pretty much the same thing as declaring it on the line preceding the for(...).
I'm pretty sure most C++ compilers work this way...
;)

Share this post


Link to post
Share on other sites
If you want to get technical, I believe (with about 75% confidence) that it should be out of scope, but that that was only defined in the ANSI '99 C++ standard... and VC++6 was written prior to that standardisation!

I'm not sure how newer C++ compilers behave. It's one of the subtle syntactic differences between Java and C++ that occasionally trip me up. Like the way that public/protected/private differ - always get me when I switch languages :P.

Share this post


Link to post
Share on other sites
Quote

If you want to get technical, I believe (with about 75% confidence) that it should be out of scope, but that that was only defined in the ANSI '99 C++ standard... and VC++6 was written prior to that standardisation!



Hmmm, didn't know that one... I guess I just assumed that because it wasn't enclosed in a set of braces that it was visible from that line of code all the way to the end of the enclosing set of braces.

Share this post


Link to post
Share on other sites
Quote

Hmmm, didn't know that one... I guess I just assumed that because it wasn't enclosed in a set of braces that it was visible from that line of code all the way to the end of the enclosing set of braces.


In the example mailin posted the braces are implicit. The compiler should generate the same code, whether the braces are included or not. Just tested it with gcc 3.2 and the generated assembly code is identical.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0