-1

I just need help to see how to populate 3 separate arrays based on the formatting of the input file...

I know if its on a single array and if the data in the file is of one type all you need to do is a for loop. However with 3 I don't know

Here is the format for the file I am reading from

This is the code I have so far, int netIDArray is for storing the first set of numbers string majorArray is for storing the next column, of letters. double gpaArray is for storing the next column of decimal numbers.

#include <iostream>
#include <fstream>

using namespace std;

int netIDArray[15];
string majorArray[15];
double gpaArray[15];

int main()
{
    ifstream file;
    file.open ("studentData1.txt");
    // If error in opening file then return
    if (!file)
    {
      cout << "Error opening file" << endl;
    }

    if(file.is_open())
    {
        string majorArray[15];

        for(int i = 0; i < 5; ++i)
        {
            file >> majorArray[i];
        }
    }

    return 0;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Meno
  • 1
  • 3
  • Why not using a `struct` and an array of this? Or even better a `std::vector` of such struct? – user0042 Dec 05 '17 at 21:06
  • @user0042 i added the code – Meno Dec 05 '17 at 21:09
  • @user0042 I am a beginner at c++, and I don't know what that is.. – Meno Dec 05 '17 at 21:10
  • 2
    You forgot to mention your several obligatory restrictions about that task. I gave you at least 3 alternative solutions, so please clarify what would pass your professors requirements, or ask them directly about that. – user0042 Dec 05 '17 at 22:23

1 Answers1

0

What you probably want to do is

struct MyData {
    int netID;
    std::string major;
    double gpa;
};

std::ostream& operator<<(std::ostream& os, const MyData& myData) {
    os << "netId = " << myData.netId 
       << ", major = " << myData.major
       << ", gpa = " << myData.gpa;
    return os;
};

int main() {
    std::ifstream file("studentData1.txt");
    // If error in opening file then return
    if (!file) {
        std::cout << "Error opening file" << endl;
        exit(1);
    }

    std::vector<MyData> majorArray;
    std::string line;
    while(std::getline(file,line)) {
        std::istringstream iss(line);
        MyData myData;
        if(!(iss >> myData.netID >> myData.major >> myData.gpa)) {
            // Handle parsing errors
        }
        else {
            majorArray.push_back(myData);
        }
    }

    for(const auto& major : majorArray) {
        std::cout << major << std::endl;
    }
    return 0;
}

As you seem to have a restriction not letting you use a struct and single vector as mentioned above, here's a slightly refactored example:

int main() {
    std::ifstream file("studentData1.txt");
    // If error in opening file then return
    if (!file) {
        std::cout << "Error opening file" << endl;
        exit(1);
    }

    std::vector<int> netIDArray;
    std::vector<std:string> majorArray;
    std::vector<double> gpaArray;        
    std::string line;
    while(std::getline(file,line)) {
        std::istringstream iss(line);
        int netId;
        std::string major;
        double gpa;
        if(!(iss >> netID >> major >> gpa)) {
            // Handle parsing errors
        }
        else {
            netIDArray.push_back(netId);
            majorArray.push_back(major);
            gpaArray.push_back(gpa);
        }
    }

    for(auto itNetId = std::begin(netIDArray), 
             itMajor = std::begin(majorArray), 
             itGpa = std::begin(gpaArray);
        itNetId != std::end(netIdArray)) && 
        itMajor != std::end(majorArray) && 
        itGpa != std::end(gpaArray);
        ++itNetId, ++itMajor, ++itGpa) {
           std::cout << "netId = " << *itNetId
                     << ", major = " << *itMajor 
                     << ", gpa = " << *itGpa;       
    }

    return 0;
}

And here's the raw array variant as you seem to be restricted to also:

int main() {
    std::ifstream file("studentData1.txt");
    // If error in opening file then return
    if (!file) {
        std::cout << "Error opening file" << endl;
        exit(1);
    }

    const std::size_t InputSize = 15;
    int netIDArray[InputSize ];
    std:string majorArray[InputSize];
    double gpaArray[InputSize];        
    std::string line;
    for(std::size_t index = 0;
        index < InputSize && std::getline(file,line);
        ++index) {
        std::istringstream iss(line);
        int netId;
        std::string major;
        double gpa;
        if(!(iss >> netID >> major >> gpa)) {
            // Handle parsing errors
        }
        else {
            netIDArray[index] = netId;
            majorArray[index] = major;
            gpaArray[index] = gpa;
        }
    }

    for(auto itNetId = std::begin(netIDArray), 
             itMajor = std::begin(majorArray), 
             itGpa = std::begin(gpaArray);
        itNetId != std::end(netIdArray)) && 
        itMajor != std::end(majorArray) && 
        itGpa != std::end(gpaArray);
        ++itNetId, ++itMajor, ++itGpa) {
           std::cout << "netId = " << *itNetId
                     << ", major = " << *itMajor 
                     << ", gpa = " << *itGpa;       
    }

    return 0;
}
user0042
  • 7,917
  • 3
  • 24
  • 39
  • I cannot use struct for this assignment – Meno Dec 05 '17 at 21:24
  • @Meno What a pity. That's a very silly restriction. I'll provide you with an alternative using three separate vectors. – user0042 Dec 05 '17 at 21:29
  • Is a vector allowed though? :) – rustyx Dec 05 '17 at 21:34
  • No vectors are not allowed – Meno Dec 05 '17 at 21:41
  • @Meno Are you sure to participate a C++ class? I hope your professor has [good reasons to explain these](https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr). Invite him to attend that question please. – user0042 Dec 05 '17 at 21:46
  • @Meno _"No vectors are not allowed"_ I finally provided a solution without using a `std::vector` but fixed size raw arrays. I hope your professor is convenient with that. Care to lookup the [documentation](http://en.cppreference.com/) to read up about the standard library stuff I used, and to add the necessary `#include` statements to get my code proposals finally compiling. – user0042 Dec 05 '17 at 22:10