-3

I am trying to this homework for my c++ class and I have been working on it for a few hours now and am not making any progress. I've done a bunch of searches on my issues and nothing is really working. I've come here hoping to solve my issue.

I have a file called "hw2data.txt" with 25 lines of information and i'm trying to simply output it to the console for now but my homework says I need to do it using an array of Student(my class name) that holds 25 objects In the main function, the program reads from the data file and calls member functions of class Student to set member variables and output the results.

I want to just be able to fully output the text in the file before going into the functions and adding them to the results.

TXT File

10001 Alice Brown       89 85 92 99 90 
10004 Bob Bush          85 76 83 79 83 
10010 Carl Capra        65 57 73 68 76 
10012 David Lieberman   76 68 79 81 58 
10034 John Menchin      100 89 95 93 88 
10056 George Smith      86 78 90 80 95  
10062 Elaine Sanders    85 79 90 80 95  
10078 Jack Cunningham   72 78 82 97 84 
10090 Susie Brown       68 85 80 84 83  
10099 Marvella Garcia   86 92 88 97 98  
10120 Tony Peterson     85 84 83 90 76 
10129 John Jones        75 75 80 84 80 
10131 Mary Evans        60 72 89 86 65  
10146 Nancy Drew        78 80 75 90 85  
10152 Lola Zapeta       89 81 98 89 97  
10155 Duckey Donald     82 60 73 78 55 
10163 Goof Goofy        89 78 75 89 56 
10168 Brave Balto       100 98 93 89 92 
10178 Snow Smitn        93 76 54 83 80 
10184 Alice Wonderful   86 79 87 78 67 
10192 Samina Akthar     85 62 78 45 60 
10207 Simba Green       50 45 35 60 20 
10211 Donald Egger      76 80 83 68 81 
10216 Brown Deer        86 87 89 79 75 
10245 Johny Jackson     96 85 91 83 79 

Student Class File

#include <iostream>
#include <fstream>
#include <string>
#include "Student.h"
using namespace std;

void Student::setID(int tID)
{
    ID = tID;
}

void Student::setFName(string f)
{
    firstName = f;
}

void Student::setLName(string l)
{
    lastName = l;
}

void Student::setScores()
{

}

int Student::getID()
{
    return ID;
}

string Student::getFName()
{
    return firstName;
}

string Student::getLName()
{
    return lastName;
}

int Student::getWeightedTotal()
{
    return 0;
}

int Student::getGrade()
{
    return 0;
}

void Student::printStudent()
{
}

Student::Student()
{
    ID = 0;
    firstName = "";
    lastName = "";

}

Student::Student(int tID, string f, string l)
{
    setID(tID);
    setFName(f);
    setLName(l);


}

Main.cpp File

#include <iostream>
#include <fstream>
#include <string>
#include "Student.h"
using namespace std;

int main() {

    Student students;
    Student sa[25];
    ifstream inFile;
    inFile.open("hw2data.txt");






    system("pause");
    return 0;
}

I've tried multiple ways of getting this to work and the main error i get is "binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)" Please help

saviro
  • 11
  • 1
  • 1
  • 5
  • Here's a quick read that might help: [Input/Output operators overloading in C++](https://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm) – 001 Mar 06 '17 at 02:34
  • 1
    You claim you tried *multiple ways of getting this to work* but what you show here is code that only opens a file. What have you specifically tried? We can help with that but we can't do this homework for you. – mpiatek Mar 06 '17 at 02:37
  • I have tried multiple ways, I am not asking for it to be done for me, I see that same response on many of these stack threads. I have tried using code such as " inFile >> students.getID; " to just display the first part of the file, and also tried using getline but i'm not very familiar with it. What I'm looking for is someone to explain what is needed to print this file to the console using a class object array. I've never done it before and I want someone to help me understand by explaining or showing an example. – saviro Mar 06 '17 at 02:41

2 Answers2

0

Okay, so to output file's content to the screen you can do something like this:

#include <iostream>
#include <string>
#include <fstream>

int main() {
    std::ifstream inFile;
    inFile.open("hw2data.txt");

    for (std::string line; std::getline(inFile, line); ) {
        std::cout << line << '\n';
    }

    return 0;
}

here we call std::getline in a loop to get one line of text from the file into variable line of type std::string.

In your example, when you got your line, you will need to parse it appropriately to get all of the values. You can utilize std::istringstream so something like following code needs to be added in a proper place:

#include <array>
#include <sstream>

std::istringstream stream(line);
int id;
std::array<int, 5> scores;
std::string first_name, last_name;

stream >> id >> first_name >> last_name >> scores[0] >> scores[1] >> scores[2] >> scores[3] >> scores[4];

You also have to take into accout that your file contains 2 lines which should be skipped.

Having all of the data, it should be easy for you to construct Student objects.

Read about std::array, std::string, std::istringstream

mpiatek
  • 1,313
  • 15
  • 16
  • You should probably [grab a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You've got the code to extract all of the variables, just construct Student object calling appropriate constructor and then one setter. – mpiatek Mar 06 '17 at 03:27
  • Ok so I have it printing to my console now, but what I want to do is have each part of each row filled into the right variable of the class, I tried doing ` inFile >> saa[25].setID >> saa[25].setFName >> saa[25].setLName >> saa[25].setScores[0] >> saa[25].setScores[1] >> saa[25].setScores[2] >> saa[25].setScores[3] >> saa[25].setScores[4]; ` but that comes out with errors and i'm not really sure why, it should just read the file and put each part into those variables should it not? – saviro Mar 06 '17 at 03:27
0

Using inFile >> students.getID;, as you mentioned in your comment, will not do anything. Take a look at the getID function: int Student::getID(). It doesn't do anything other than return an integer (let alone have any overloaded operators, which you will learn about later).

If you want to store the information into a Student object, you have two options, use the constructor or use the set functions. It could be a good practice for you to just try reading in 1 student into a single Student object before trying to populate an entire array.

Here is an example that may help you:

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

class Cat 
{
    int weight;
    string favoriteFoods [3];
public:
    void set_weight (int);
    void set_favoriteFoods (string);
};

void Cat::set_weight (int w) 
{
    weight = w;
}

void Cat::set_favoriteFoods (string ff[3]) 
{
    favoriteFoods = ff;
}

int main () 
{
    int catWeight;
    string catFavoriteFoods [3];

    ifstream infile;
    infile.open ("kittyInfo.txt");

    if(infile.is_open())
    {
        infile >> catWeight;
        infile >> catFavoriteFoods[0];
        infile >> catFavoriteFoods[1];
        infile >> catFavoriteFoods[2];
    }

    Cat kitty;
    kitty.set_weight (catWeight);
    kitty.set_favoriteFood(catFavoriteFoods);

    infile.close();

    return 0;
}

Assuming kittyInfo.txt looked something like this:

15     fish    milk    fear
  • This was an excellent example, I am not sure how to put code in this comment but I have got something similar to your example but am now having trouble with getting the 5 scores from the txt file into the class array i created called scores[5] in student.cpp – saviro Mar 06 '17 at 03:43
  • I've updated my example to include an array. It's not the most efficient way to do things, but it's easier to follow. – Rhett Prestwich Mar 06 '17 at 03:53
  • I have just figured it out before you updated it, I am now able to print each piece to the console using the class objects and here is my working cout code `code` cout << students.getID() << endl; cout << students.getFName() << endl; cout << students.getLName() << endl; for (int i = 0; i < 5; i++) cout << score[i] << endl; – saviro Mar 06 '17 at 03:59