External files and streams

Discussion in 'C++' started by inspiration, Mar 25, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    I am having a problem connecting my external file with a stream...I have only one error for the entire program. But just to give the background of it I am to extract data froma .txt file into the program and then input the data into an array of structs called data[]. within the struct there are three members called id, scores, and grade. Im having problems though because when it does execute it only shows 0s instead of the student id and scores in the file. below is my code and the data in the file scores.txt

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    struct studentInfo
    {
    int id;
    int score;
    char grade;
    };
    const int MAX_NUM = 50;
    studentInfo data[MAX_NUM];
    bool successful = false;
    bool readStuData(ifstream, bool&, studentInfo data[]);
    float getAverage(studentInfo data[], float& mean);
    void assignGrades(studentInfo data[], float);
    void reportResults(studentInfo data[]);
    
    int _tmain(int argc, _TCHAR* argv[], float average)
    {
    ifstream inData("C:\\\\Documents and Settings\\Floyd\\My Documents\\scores.txt", ios::in);
    readStuData(inData, successful, data);
    getAverage(data, average);
    cout<<"Student "<<data[0].id<<"test score is "<<data[0].score<<endl;
    assignGrades(data, average);
    reportResults(data);
    return 0;
    }
    
    bool readStuData(ifstream inData, bool& successful, studentInfo data[])
    {
    
    int number;
    
    if(inData.fail())
    {
    cerr << "***ERROR: Cannot open" << inData << endl;
    successful = false;
    return successful;
    return EXIT_FAILURE;
    }
    else
    {
    successful = true;
    return successful;
    inData >> data[0].id >> data[0].score;
    number = 0;
    while (!inData.eof())
    {
    if (number=0, number<MAX_NUM, number++)
    {
    inData >> data[number].id >> data[number].score;
    number++;
    }
    }
    }
    inData.close();
    return successful;
    }
    float getAverage(studentInfo data[], float& mean)
    {
    float sum = 0.0;
    int i;
    
    for (i=0; i<MAX_NUM; i++)
    {
    sum = data[i].score + sum;
    }
    mean = sum / MAX_NUM;
    
    return (mean);
    }
    void assignGrades(studentInfo data[], float average)
    {
    int i;
    for (i=0; i<MAX_NUM; i++)
    {
    if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
    {
    data[i].grade = 'S';
    }
    if (data[i].score > average + 10)
    {
    data[i].grade = 'O';
    }
    if (data[i].score < average - 10)
    {
    data[i].grade = 'U';
    }
    }
    }
    void reportResults(studentInfo data[])
    {
    ofstream out("C:\\\\Documents and Settings\\Floyd\\My Documents\\report.txt");
    out << "TABLE OF STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
    for (int i=0;i<MAX_NUM; i++)
    out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
    }
    
    scores.txt

    123 90
    242 70
    376 97
    485 58
    511 95
    623 87
    798 75
    856 79
    921 85
    955 64
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    Well there's your problem. You're returning from your function before you read anything.
     
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Well I removed the reuten to the end of the function and still I am getting the same error:

    c:\program files\microsoft visual studio 9.0\vc\include\fstream(676) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\program files\microsoft visual studio 9.0\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    Build log was saved at "file://c:\Documents and Settings\Floyd\My Documents\Visual Studio 2008\Projects\passfailexcred\passfailexcred\Debug\BuildLog.htm"
    passfailexcred - 1 error(s), 0 warning(s)
     
  4. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    bool readStuData(ifstream, bool&, studentInfo data[]);

    The copy constructor is private. Presumably to prevent exactly what you're doing here. The solution is to use a reference:

    bool readStuData(ifstream&, bool&, studentInfo data[]);
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Thank you sooo much!!!!!!! Funny how that one simple problem caused like two weeks of troubleshooting! lol
     

Share This Page

  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