After sorting can`t change bool value

This is a discussion on After sorting can`t change bool value 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

    After sorting can`t change bool value

    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
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Since no one has given you any help so far I will try, although I am a little confused because in your post you mentioned saving/loading data to/from file yet in the code you posted you made absolutely no input/output function calls.

    I'll assume that you have that code somewhere else (but that you have written it), and that you are only having trouble getting the data to/from the file. The problem you are having is that you are using a space to separate the different pieces of information within the file and then confused why the computer doesn't realize that a name in the format 'Bilbo Baggins' is all one chunk of data and not two? You should use some other delimiting character to separate the data.
    Code:
    e.g. Commas, full-stops (or periods if you are American), ;, :, <, >, ^, %, &, *, (, ), #, @, $, !, [, ], {, }, \, /, ?
    All of those characters could be used to separate the different data chunks.

    The other way of doing it (if saving and loading the data in binary format) is to extract and save a specific number of bytes. The problem with that is that it would impose a limit to the length of each data chunk you are saving/loading so exceptionally long name may cause problems. Obviously if you were going to do it that way you would have to either include the length of the name in the file (so that you can then extract that much info) or you don't bother storing the length of the data field and just pad the data with random crap to make up the byte length you are extracting. Either way, if you are going to save/load to/from file then you will need to specify a protocol for that (i.e. a file specification).

    For more information about extraction of data from files research the fstream methods 'getline()' and the C function 'strtok()'. There are probably C++ equivalents of 'strtok()', but I can't think of any off the top of my head.
    Last edited by Swarvy; 10-12-2010 at 07:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this example work for me?
    By xixpsychoxix in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 08:25 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

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