Read strings and bool from txt

This is a discussion on Read strings and bool from txt within the C++ Programming forums, part of the General Programming Boards category; Hello ! I have array of struct and i want 1] when i run the program to load the data ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Read strings and bool from txt

    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:
    Code:
    #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!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Well what code do you have to read the file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    I don`t have.

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

    but is wrong

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Ok, here is.

    in the data.txt i have
    Code:
    A1 false Wella Trina
    A2 false George Ali
    A3 false Comina Riviera
    My code is
    Code:
    #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-07-2005, 11:05 PM
  3. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM
  4. strings again
    By larry in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21