Reading a file into an array(C++)

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Dmzl_n_dstrz

Thread Starter
Joined
Feb 9, 2005
Messages
1
Why is this so hard for me to grasp?? I have to write a program that reads student names into an array of strings. Then read the average of 3 scores into another array. But, for instance, when I read in the name Joe Black, the ouput of my array is:
J
o
e

B
...etc

I think I need to use an additional for loop, but am confused how. And how do I read the name in the file, then read the scores.The scores need to be read as variables(x,y,z) in order to get the average of the 3, right? The file looks like this:
Joe Black
15 26 84
Donald Duck
89 56 74 ...etc
Please help me. I'm on the verge of giving up
 
Joined
Apr 30, 2001
Messages
2,636
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

int main() {
    std::ifstream in("example.txt");
    if (!in) {
        return EXIT_FAILURE;
    }
    
    std::string skip;
    std::string names[100];
    float scores[100][3];
    
    size_t n = 0;
    size_t a = 0;
 [color=navy]
    getline(in,names[n]);
    ++n;
    in >> scores[a][0] >> scores[a][1] >> scores[a][2];
    ++a;
    
    getline(in,skip);
  [/color]  
    getline(in,names[n]);
    ++n;
    in >> scores[a][0] >> scores[a][1] >> scores[a][2];
    ++a;
 
    for (std::size_t i = 0; i < 2 ; ++i) {
        std::cout << names[i] << "\n";
        std::cout << ( ( scores[i][0] + scores[i][1] + scores[i][2] ) / 3 ) << std::endl;
    }

}
That should give you a basic idea without giving too much away.

The part in blue is the part that would go in a loop. (It's just repeated twice because I didn't use a loop and there are 2 names).

Now, the "scores" array doesn't hold the averages. It just holds the scores, but you can see how I printed the averages out, so you can use a loop like that to stuff the averages into an averages array.

Once you show some effort, we'll help more.

Each of the arrays can hold 100 entries. It's up to you to make sure the loop doesn't try to read in more entries.
 
Joined
Apr 30, 2001
Messages
2,636
Also, if you don't want the scores in an array and just want the averages:

Instead of float scores[100[3], have averages[100];

Then you can have x,y,z variables.

float x;
float y;
float z;

In the loop, you can do

in >> x >> y >> z;

Then right after that line in the loop, you could have:

averages[a] = (x + y + z) / 3;


Then when would just print out the entries of averages like you would with names.

Now, if you want the first and last name to be separate, you could do:

names[100][2];

Then in the loop, (instead of using getline at all), you could do:

in >> names[n][0] >> names[n][1] >> x >> y >> z;
averages[a] = (x + y + z) / 3;

That should be enough info for you.

You should really get each line of the file and at least validate the lines that have scores on them before you convert them to floats, but doing in >> x >> y >> z would probably be what is expected.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top