dimanche 4 septembre 2016

How to Access a variable in a filestream loaded in list container

I am the new kid in the block, studying C++. I have loaded a file stream in a list container, using variables. I want to be able to access and change the value of any of those variables. I've been trying for weeks to no avail. Can somebody help?

This is the external text file: flightBoard1.txt

  Delta 3431 Paris JFK
  Usair 2275 EWR London
  Delta 1500 Bonn Milan

This is the main.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <list>

using namespace std;

template<class T, class U, class V>
void changeFlight(list<string> &myFlight, T loc, U value, V newVal);

int main()
{
    string _company;
    int _flight;
    string _origin;
    string _destination;

    list<string> flightBoard;
    stringstream *ssPtr;
    int counter = 0;

    ifstream myFile("flightBoard1.txt");
    while(myFile >> _company >> _flight >> _origin >> _destination ){
        ++counter;
        ssPtr = new stringstream; // you want to put each line in a different slot in the flightBoard list object
        *ssPtr << counter << " " << _company << "\t" << _flight << "\t" << _origin << "\t" << _destination << endl;
        flightBoard.push_back(ssPtr->str()); // You need an arrow, this is a pointer

    }

    list<string>::iterator it;
    for(it = flightBoard.begin(); it != flightBoard.end(); it++){
        cout << *it ;
    }

    int oldFlight, newFlight;
    cout << endl << "Enter old flight number: ";
    cin >> oldFlight;
    cout << "Enter new flight number: ";
    cin >> newFlight;

    changeFlight(flightBoard, ssPtr, oldFlight, newFlight);

    delete ssPtr;
    myFile.close();


    return 0;
}

template<class T, class U, class V>
void changeFlight(list<string> &myFlight, T loc, U value, V newVal){
    list<string>::iterator it;
    cout << endl << "Flight: " << value << " has been changed to: " << newVal << endl;
    for(it = myFlight.begin(); it != myFlight.end(); it++){
        // This is where I am having a problem
        /*if(it -> myFlight -> loc -> value){
            value = newVal;
        }*/ 

    }

}

Aucun commentaire:

Enregistrer un commentaire