Reading data from file to one-dimensional array

I have not programmed for many many months so I am really rusty and I do apologize in advance if I am slow. My question is how do I read data into a one dimensional array when the .data file looks like this?

6 Jim Brown
17 Yogi Bear
3 Scott Reader
...
...

The number is there contestant number and I need to make there contestant number there array slot number. I know arrays start with 0 and there is no contestant 0 so I need to subtract 1 from there number to index and add one to find the right slot for when the user is asked to pull up info on the contestant.

I know you can read data into an array like this:

1
2
for (index = 0; index < 10; index++)
   cin >> list[index];


But that is by inputting the info by hand or keyboard.

I have searched the archives and similar questions have been asked but they were either two-dimensional or had the same variable type.

Thanks in advance.
You can use a std::ifstream just like std::cin. For instance:

1
2
3
4
5
6
7
8
9
std::ifstream ifs("file.txt");

int n;
std::string s;

while(ifs >> n >> s)
{
    list[n - 1] = s;
}

You might want to do some range checking, because bad input will cause an out of bounds error.
+1 filipe but there is a small problem with your code. The names have spaces in them so you can't use >> to read them in, you need std::getline().
1
2
3
4
5
6
7
8
9
std::ifstream ifs("file.txt");

int n;
std::string s;

while(ifs >> n && std::getline(ifs, s))
{
    list[n - 1] = s;
}
Well noticed, I missed that. Thanks for the correction.
Thanks so much. I will try it out and see how it works for me.
Thanks that worked but only so much. I do not know functions all that well yet because I am used to doing everything in main(). I think i need to change the function type to void or change something on what the function returns or its just not reading data into the array right. Does it matter where functions are placed in a .cpp file anyway?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int contestantInfo(int contestantNumber, string& contestantName)
{
   string list[ARRAY_SIZE];
   
   std::ifstream inFile("contestantname.data");

   while(inFile >> contestantNumber && std::getline(inFile, contestantName))
   {
       list[contestantNumber - 1] = contestantName;
   }
  
  cout << contestantNumber << " " << contestantName << endl;
  
return contestantNumber;
}


Note: ARRAY_SIZE is a constant of 20.

The output of this after it is called just displays a zero and goes back to the menu to reselect which option to choose from main().


Which option would you like?:
1 - Display all data for a contestant
2 - Display total catch weight for fish type
3 - Display biggest fish caught for fish type
4 - Quit
Choice is: 1
What contestant would you like to see?: 11
0
Which option would you like?:
1 - Display all data for a contestant
2 - Display total catch weight for fish type
3 - Display biggest fish caught for fish type
4 - Quit
Choice is: 4
bash-2.04$

In contestantname.data contestant 11 is not in the 11th spot but that should not matter and the name is my name.

Thanks in advance for the help.
I think the problem is that you declare your array as local to your function. So when your function exits, the array will be destroyed.
I would pass an array into the function.
1
2
3
4
5
6
7
8
9
10
11
12
// pass the array into the function from outside
void contestantInfo(string list[ARRAY_SIZE])
{
   std::ifstream inFile("contestantname.data");

   int contestantNumber;
   string contestantName;
   while(inFile >> contestantNumber && std::getline(inFile, contestantName))
   {
       list[contestantNumber - 1] = contestantName;
   }
}


Also you aught to check to make sure that the numbers you read in are not larger than the array.
Last edited on
Topic archived. No new replies allowed.