0

Hi i have a task to stores marks for students, a struct is used to store this.

the thing giving me trouble at the moment is how to read and store a floating point number within a struct, the data specifically for the mark is on 1 line of a text file seperated by white space.

I cant seem to find a way to copy floating point numbers from a stream object, appreciate any response.

what i have so far, stores the first record minus the marks fine, anything after that, as i cant figure out how to store the marks doesnt work.

#include <iostream>
#include <fstream>
#include "ass1.h" // to link functions to a main file containing only a text based menu.

using namespace std;

    const int MAX = 200;

        int classSize;
        int completedLabs;
    struct StudentRec
    {

        char familyName[15];
        char givenName[16];
        char computerLogin[5];
        float marks[12];
    };
    StudentRec student[MAX];

    void ReadFile();




    void ReadFile()
{
        char filename[20];


        cout << " Enter filename " ;
        cin >> filename;

        ifstream myfile(filename, ios::in);

       if (myfile.good())
       {

        int numrecords = 0;

        myfile >> classSize;
        myfile >> completedLabs;

        while (!myfile.eof())
        {

            myfile >> student[numrecords].givenName;
            myfile >> student[numrecords].familyName;
            myfile >> student[numrecords].computerLogin;
           // myfile.read (student[numrecords].marks,12, '/n')
            numrecords++;
        }
        myfile.close();
  }
        cout << classSize;
        cout << completedLabs;
        cout << student[0].givenName;
        cout << student[0].familyName;
        cout << student[0].computerLogin;


}

10 5
Dennis Iddis di504
1 1 -1 1 1
Alex Sywlrskbi as579
-1 -1 1 0.5 0
Terry Hdroribj th074
1 1 -1 -1 0.5
Glen Gowfz gg935
1 -1 1 1 0.5
Jake Qivghswcb jq137
-1 -1 1 0.5 1
Quan Jlrgxyl qj244
1 1 1 0.5 0.5
Vu Qcpwxwsf vq077
1 1 1 1 1
Glen Lqntw gl377
0 0 0 1 1
Terry Kowucgql tk422
1 1 1 1 0
Una Yeelkn uy473
0.5 0 1 1 1

Text File format.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Nicko_FaTe_
0

the thing giving me trouble at the moment is how to read and store a floating point number within a struct, the data specifically for the mark is on 1 line of a text file seperated by white space.

Since ye' data is white space delimited, ye' can simply use the overloaded >> extraction operator provided by the <fstream> library:

ifstream infile;
int record = 0;
int index  = 0;

infile.open("text.txt");

if(!infile.is_open())
{
     cout << "\aError, file not found.";
     cout << "\nFile may have been moved, renamed, or deleted...";
     exit(1);
}

while(infile && record < MAX)
{
     //Get marks
     while(infile >> student[record].marks[index] && index < 12)
     {
          index++;
     }

     index = 0;

     //Get family name
     infile >> student[record].familyName;

     //Get given name
     infile >> student[record].givenName;

     //Get computer login
     infile >> student[record].computerLogin;

    //advance to the next student record
    record++;
}

infile.close();

This is all based on the assumption that your text file maintains a standard format that is shown in your example. This is most of the work done for you. See if you can handle and display the data on your own.

Edited by Clinton Portis: n/a

0

just thought i'd write back, thanks for the assistance, however that isn't going to work for me, i have since figured out how to read properly, thanx for the assistance though, got me thinking.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.