Hello,

i am just starting to learn c++. Ane hints or tips would be appreciated.
I have a text file that looks something like this

2 fred hind
6 julie frie

Now, i need to take that number, set it to an element of the array,
then store the name in that element.

I dont know hot to pull this off. so far, I have something like this.
------------------

char arr[20];
int num;
string first, last;
fstream inData;
inData open("textfile");
for (int x= 0; x < 20; x++){
inData >> num >> first >> last;
arr[num]= first+last;
}

-------------------------

of course this doesnt work since i cant do this, lol.
However, this is as far as i have gone with the logic.
I can however get the variable num to recieve the integer and have that set the element of the array.

I do not completely understand how i would do the rest.

Any tips would help out a lot/

Thank you.

Ok, I have bben a java programmer for about a year now. I decided to take a C++ class, since i already had java, i didnt have to take the intor course, would have been a waste of time.

Now i know how to to program, i just dont EXACTLy understand how to use fstream so it will do what i want it to.

Can someone please link a good reference or just give me sone tips or hints on how i would take 2 lines like this from a text file

1 jack handy
2 stuart smalley

and put into an array like this
arr[1] = "jack handy";
arr[2] = "stuart smalley;

I can already get the number to set the leement of the array, I am just not too familiar with the c++ string and fstream libraries to do the rest.
Thank you for your help in advance,

That was a pretty close attempt.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   [B]string[/B] arr[5];
   int num;
   string first, last;
   fstream inData("file.txt");
   [B]while ( inData >> num >> first >> last )[/B]
   {
      arr[num]= first+last;
   }
   for ( size_t i = 0; i < sizeof arr / sizeof *arr; ++i )
   {
      cout << "arr[" << i << "] = " << arr[i] << '\n';
   }
   return 0;
}

/* file.txt
1 jack handy
2 stuart smalley
*/

/* my output
arr[0] = 
arr[1] = jackhandy
arr[2] = stuartsmalley
arr[3] = 
arr[4] = 
*/

forgot to thank you for that one up there.

Thank You!

-sean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.