Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Thread Tools
Old 06-24-09, 06:45 AM   #1
Registered User
Join Date: Jun 2009
Posts: 2
Default 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?
AustinYQM is offline   Reply With Quote
Old 06-24-09, 07:46 AM   #2
Join Date: Oct 2004
Location: Montreal, Canaduh!
Posts: 1,968
Default 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++)

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

Evga X58|I7 965 3.8GHz|Ultra 120 Extreme|Evga GTX 480 SC|2 x Intel X25-M 80GB RAID 0|Intel 520 - 240 GB|WD Black Caviar 1TB|6GB Corsair XMS3 1600C8 |Corsair 1000W PSU|Antec Twelve-Hundred|DELL U3011|Logitech G500|Logitech G19
lduguay is offline   Reply With Quote
Old 06-24-09, 08:04 AM   #3
Registered User
Join Date: Jun 2009
Posts: 2
Default 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.


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.
AustinYQM is offline   Reply With Quote
Old 06-25-09, 11:26 PM   #4
Join Date: Jan 2008
Posts: 184
Default 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++.
Zhivago is offline   Reply With Quote
Old 06-26-09, 01:44 PM   #5
Registered User
Join Date: Jan 2005
Location: Rochester, MN
Posts: 4,018
Default 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.
nemecb is offline   Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -5. The time now is 11:40 PM.

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