0
mailin

C++ help?

Recommended Posts

Wow, more fun for the fam!

I found an old copy of Stroustrup's book (third edition, 97) and it does say that if the var is declared in the initializer of the for loop it loses scope at the end of that for loop.

Geez! I'm feelin' geeky tonight.:D

It's a good thing I'm sure I threw away the second edition or I'd probably be digging around in the closet for it...;)

Share this post


Link to post
Share on other sites
Actually what I was trying to say was that the variable being declared in the initializer list of the for loop and not inside the braces of the for loop is why I thought it would be in scope until the end of block surrounding the for loop. I didn't notice she didn't have braces.

Share this post


Link to post
Share on other sites
Ok... with some slight mods...

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 = new ifstream();
openInput( infile );
loadArray( infile, namesArray );

delete infile;
infile = NULL;
}

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

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

}

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

if (input != NULL)
{
do
{
input->getline( array, 80, ',' );
cout << "Yo!" << array << endl;
i++;
}
while (!input->fail());
}
}

this works in VC++6.

The changes I made were to make the infile variable a pointer and changed the function declarations that take it as input so that they expected a pointer instead.

Changed the '.' in all the input calls to be a '->'

Changed the check for whether input didn't exist to be checking to see if it was NULL.

Added a delete to the main to free the memory.

In loadArray, you had i declared, but never used. the input.getline function expects a char* for the first parameter. a char[][] is NOT a char*. You need to index that array to use it as one. So I changed the call to input.getline to say array and also did it in your cout statement.

hmm something goofy is happening with the forum here. that part about the change to array should say array '['i']'.... it's stripping my brackets and the i for some reason.

Share this post


Link to post
Share on other sites
If you are using C++, why aren't you taking a ref to the ifstream, and why aren't you using the standard string object. Also a function that returns nothing and has no exceptions possibly thrown is worthless. You wouldn't want to turn this in to my high school C++ class.

Share this post


Link to post
Share on other sites
Quote

Nice. Thanks. (I couldn't find it on google).



Are we bored at work today Dave ;)

ok let me clear this up once and for all... I have edited to the orginal code and believe that this is best solution.

10 MODE 2

20 COLOUR RND(7)+1

30 PRINT "Oli is a Skygod ";

40 GOTO 30
-----------------------------------------------------------
--+ There are 10 types of people in the world: Those who understand binary, and those who don't.. --+

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