1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Reading a file into an array(C++)

Discussion in 'Software Development' started by Dmzl_n_dstrz, Feb 9, 2005.

Thread Status:
Not open for further replies.
  1. Dmzl_n_dstrz

    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
     
  2. Shadow2531

    Shadow2531

    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.
     
  3. Shadow2531

    Shadow2531

    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.
     
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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/328570

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice