nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   Fairly Dumb C++ Question. (http://www.nvnews.net/vbulletin/showthread.php?t=134905)

AustinYQM 06-24-09 06:45 AM

Fairly Dumb C++ Question.
 
Sorry to make this my first post.

I've been reading all over the internet about arrays and I just can't seem to find exactly what I need. All I want is to store strings into an array.

The hope is that I can make an array with strings like infile1.txt, infile2.txt ect ect.

Then use the fstream to open a file based on a varible. So if array[32] = infile32.txt then somewhere down the line I could have a function that would be called something like filetoopen(string).

So I'd have a while loop. that called the filetoopen() function using a varible like string helpfulVariable ; That would cycle through the array.

Is this making any sense?

While looking all the internet I couldn't find how to make an array of strings. Not an array of characters that happen to be words like C. ACTUAL strings. Can anyone point me in the correct direction?

lduguay 06-24-09 07:46 AM

Re: Fairly Dumb C++ Question.
 
CString array[32] = {"infile1.txt","infile2.txt....};

Replace ... by actual string, or better, use a loop to construct the filename.

CString array[32];

for (int i=0; i < 32; i++)
array[i].FormatMessage((_T("infile%1!d!.txt"),i);

See http://msdn.microsoft.com/en-us/libr...43(VS.60).aspx

AustinYQM 06-24-09 08:04 AM

Re: Fairly Dumb C++ Question.
 
Thanks I knew it was something simple. Can't actually use the loop cause its gonna but stuff like torch.txt and wall.txt but it made a simple example. Thank you again.

Edit:

Ok, I have a follow up question. How do I reference it? Assume that I want the array called locationArray. I want to use a new line for each entry of the array for readiability and ability to change it later on. Would this work?

CString locationArray[4]
locationArray[0] = "example0.txt" ;
locationArray[1] = "example1.txt" ;
locationArray[2] = "example2.txt" ;
locationArray[3] = "example3.txt" ;

would that be correct? assuming so would I then do.

string storeVariable = locationArray[2] ;
cout << storeVariable ;

Thanks again for any help. I am very new to arrays.

Zhivago 06-25-09 11:26 PM

Re: Fairly Dumb C++ Question.
 
Is there a specific reason you are mixing and matching std::string and CString?
One is standard C++, the other is a Microsoft-specific class.

More importantly, where is your book? Something is way wrong if you feel the need to be asking these trivial of questions on a forum. I recommend Accelerated C++ by Koenig and Moo - it is a great book for a beginner to C++.

nemecb 06-26-09 01:44 PM

Re: Fairly Dumb C++ Question.
 
This being C++, you almost certainly don't want to be using raw arrays. The idiomatic C++ method is to use containers like vectors and lists. You can find references for using them all over the place, I like using this one.

That said, I agree with Zhivago. You probably want to find yourself a basic C++ intro that would explain all of this to you.


All times are GMT -5. The time now is 07:21 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.