Hello !

I have array of struct and i want
1] when i run the program to load the data from it.
2] when i make changes to be saved in the text file the same time or when i quit the program

I tried to put data in txt file with the following format but i can only read until the space. I do a lot of search but wasn`t able to find what i look for. Mainly i don`t know how to read strings

A1 false Alexa Trina
A2 falseGeorge Ali
A3 false Comina Riviera

My sample code is:

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

class classroom
{
public:
    string studentClass;
    bool studentBonus;
    string studentName;

    void classroom::createStudent(string sClass, bool bonus, string name);
};

void classroom::createStudent(string sClass, bool bonus, string name)
{
    studentClass=sClass;
    studentBonus=bonus;
    studentName=name;
}

int main()
{
    //number of students
    const int numstudents=10;
    
    //create structure
    classroom student[numstudents];

    //initialize variables
    student[0].createStudent("A1",false,"Alexa Trina");
    student[1].createStudent("A2",false,"George Ali");
    student[2].createStudent("A3",false,"Comina Riviera");

    return 0;//indicate that program end succesfully
}//end main

Thanks for your time!

Recommended Answers

All 5 Replies

The code you posted does not contain anything that attempts to read a text file.

Yeah i know, i made some amptempts but are wrong.

I tried getline(inFile, student[0].studentName);

but is wrong

Post what you tried so that we can help you to correct it.

Ok, here is.

in the data.txt i have

A1 false Wella Trina
A2 false George Ali
A3 false Comina Riviera

My code is

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

class classroom
{
public:
    string studentClass;
    bool studentBonus;
    string studentName;

    void classroom::createStudent(string sClass, bool bonus, string name);
};
void bookStudent(classroom bStudent[]);
void deleteStudent(classroom dStudent[]);
void sortStudent(classroom sStudent[]);
void classroom::createStudent(string sClass, bool bonus, string name)
{
    studentClass=sClass;
    studentBonus=bonus;
    studentName=name;
}

void showMenu(classroom mStudent[])
{
    char option;

    //display the choices of the user
    cout<<"a) Book a student"<<endl;
    cout<<"b) Delete a student"<<endl;
    cout<<"c) Sort students"<<endl;
    cin>> option;

        switch (option)
    {
    case 'a':
        cout<<endl;
        bookStudent(mStudent);
        showMenu(mStudent);
        break;
    case 'b':
        cout<<endl;
        deleteStudent(mStudent);
        showMenu(mStudent);
        break;
    case 'c':
        cout<<endl;
        sortStudent(mStudent);
        showMenu(mStudent);
        break;
    default:
        cout<<endl;
    }
}
void bookStudent(classroom bStudent[])
{
    //declare local variables
    int i=0;

    string bStudentClass;
    bool bStudentBonus;
    string bStudentName;

    //initialize local variables
    bStudentBonus=false;
    

    cin.ignore();
    cout<<"Enter student name"<<endl;
    getline(cin,bStudentName);


    cout<<"Enter seat id: "<<endl;
    cin>>bStudentClass;

    cout<<endl;

    if (bStudentClass=="A1")
        i=0;
    else
    if (bStudentClass=="A2")
        i=1;

    bStudent[i].createStudent(bStudentClass,true,bStudentName);
}
void deleteStudent(classroom dStudent[])
{
    //declare local variables
    int i=0;

    string bStudentClass;
    bool bStudentBonus;
    string bStudentName;

    //initialize local variables
    bStudentBonus=false;

    cout<<"Enter student class "<<endl;
    cin>>bStudentClass;

    cout<<endl;

    if (bStudentClass=="A1")
        i=0;
    else
    if (bStudentClass=="A2")
        i=1;

    dStudent[i].createStudent(bStudentClass,false,"");
}
void sortStudent(classroom sStudent[])
{
     bool doMore;

    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<10-1; i++) {
            if (sStudent[i].studentName > sStudent[i+1].studentName) {
                // exchange elements
                classroom temp = sStudent[i]; sStudent[i] = sStudent[i+1]; sStudent[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);

        for (int j=0;j<10;j++)
        {
            if (sStudent[j].studentBonus==true)
            {
                 cout<<sStudent[j].studentName<<"    "<<sStudent[j].studentClass;
                cout<<endl;
            }
        }
        cout<<endl;
}
int main()
{
    //number of students
    const int numstudents=10;
    
    //create structure
    classroom student[numstudents];

    fstream inFile;
    inFile.open("data.txt", ios::in);

    inFile<<student[0].studentClass<<student[0].studentBonus<<student[0].studentName;
    inFile<<student[1].studentClass<<student[1].studentBonus<<student[1].studentName;
    inFile<<student[2].studentClass<<student[2].studentBonus<<student[2].studentName;

    
    showMenu(student);
    return 0;//indicate that program end succesfully
}//end main

on line 149 you will have to call getline() to read the student name that contains spaces.

If you only want to read the file use ifstream instead of fstream and use >> with ifstream. << is used with ofstream.

inFile>>student[0].studentClass>>student[0].studentBonus;
getline(inFile,student[0].studentName);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.