Enter info into command prompt, saves to txt file.

I've been struggling with this for quite a while. I'm new to vectors and to my understanding you need to use them in order to input info into the command prompt for it to save to a txt file.

I would like the user to be able to add stations (to a railway), edit stations and delete stations. By simply writing the Station Name and Station Facilities.

The code below is an attempt at it but isn't working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 void Staff::SelangorStations()
{
	cout << "Select any of the following." << endl;
	cout << "1) Add Station \n2) Change Station Properties \n3) Delete Station \n \n4) Exit\n";

	int choose;
	cin >> choose;

	switch (choose)
	{
	case 1:
	{
		vector<string> SelangorStations;
		ofstream myfile("SelangorStations.txt");
		for (int i = 0; i < SelangorStations.size(); i++)
		{
			myfile << SelangorStations[i];
		}
	}
		break;
	default:
		break;
	}
}


Any help is greatly appreciated.
Last edited on
The vector you create on line 13 is empty, so what should be written to the file ?
Alight, I've changed the approach I've taken, but I've reached another problem I can only add a single Station name and station facility.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void Staff::SelangorStations()
{
	cout << "Select any of the following." << endl;
	cout << "1) Add Station \n2) Change Station Properties \n3) Delete Station \n4) Exit\n";

	int choose;
	cin >> choose;

	string stationName;
	string stationFacilities;

	switch (choose)
	{
	case 1:
	{
		string SelangorStations[2]{};
		cout << "Station Name: ";
		cin >> SelangorStations[0];
		cout << "Station Facilities: ";
		cin >> SelangorStations[1];
		ofstream myfile("SelangorStations.txt");

		for (int i = 0; i < sizeof(SelangorStations) / sizeof(SelangorStations[0]); i++)
		{
			myfile << SelangorStations[i];
		}
		break;
	}


Another thing is I have to be able to change the station facilities (edit) and permanently delete the station if the staff wants to. I don't know how I'm going to implement that.
Sorry for the late reply took a while to figure out what I was doing wrong
I would create a struct and use a vector if you are allowed to.
In this way you can easily delete stations if necessary.
For the input it's best to use a loop.
Hope this makes any sense.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Station
{
  string Name;
  string Facilities;
};

vector<Station> stations;

void AddStations(vector<Station> & stations)
{
  while (true)
  {
    Station station;
    cout << "Station Name (end to quit): ";
    getline(cin, station.Name);
    if (station.Name == "end")
      break;
    cout << "\bStation Facilities: ";
    cin >> station.Facilities;
    stations.push_back(station);
  }
  // Now we have got all the input 
  //and save the vector to the file

  //TODO save file here if the vector is not empty
}

Hi, GhettoBurger.
I’ve given a glance to your railway system project as it appears from your recent posts. I hope your work is getting on fine, but I think you’d better take into higher consideration the suggestion you’ve received to now.
Let me quote jonnin from here:
http://www.cplusplus.com/forum/beginner/220966/
jonnin wrote:
It looks to me like railway class should represent ONE railway, and you need either 3 variables of its type or a vector(3)

I think it’s a great piece of advice, otherwise your code could become really complicated.

Your railways system could be conceived as a container of different railways, i.e. a class which has got a vector of railways.
Those railways could be classes which contain vectors of stations, as well as staff and facilities.
Have a look at what Thomas1965 suggested you above:
Thomas1965 wrote:
I would create a struct and use a vector if you are allowed to.


I don’t want to interfere with you planning; however, if in the future you are stuck, you could try to look at your project from a different point of view.

Here’re some hints:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <limits>
#include "RailSystem.h"

RailSystem crateMockRailSystem();
void waitForEnter();

int main()
{
    RailSystem rs = crateMockRailSystem();
    std::cout << "The route form Selangor1 to Malacca1 is "
              << rs.route("Selangor1", "Malacca1") << '\n';
    waitForEnter();
    return 0;   
}

RailSystem crateMockRailSystem()
{
    RailSystem rs;
    rs.railways.emplace_back("Selangor Railway");
    rs.railways.at(0).stations.emplace_back(std::make_shared<Station>("Selangor1"));
    rs.railways.at(0).stations.emplace_back(std::make_shared<Station>("Shared1"));

    rs.railways.emplace_back("Malacca Railway");
    // copy Selangor Railway --> Shared1
    rs.railways.at(1).stations.push_back(rs.railways.at(0).stations.at(1));
    rs.railways.at(1).stations.emplace_back(std::make_shared<Station>("Malacca1"));

    return rs;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Station.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef STATION_H
#define STATION_H

#include <string>

class Station {
public:
    Station();
    Station(std::string name_arg);
    std::string name;
    int unique_id;
    static int shared_id;
};

#endif // STATION_H 


Station.cpp:
1
2
3
4
5
6
7
8
9
10
#include "Station.h"

int Station::shared_id {};

Station::Station() : unique_id {++shared_id} {}

Station::Station(std::string name_arg) : Station()
{
    name = name_arg;
}


Railway.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef RAILWAY_H
#define RAILWAY_H

#include <memory>
#include <string>
#include <vector>
#include "Station.h"

class Railway {
public:
    Railway();
    Railway(std::string name_arg);
    std::string name;
    int unique_id;
    std::vector<std::shared_ptr<Station>> stations;
    static int shared_id;
};

#endif // RAILWAY_H 


Railway.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "Railway.h"

int Railway::shared_id {};

Railway::Railway() : unique_id(++shared_id)
    {}

Railway::Railway(std::string name_arg) : Railway()
{
    name = name_arg;
}


RailSystem.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef RAIL_SYSTEM_H
#define RAIL_SYSTEM_H

#include <string>
#include <vector>
#include "Railway.h"

class RailSystem {
public:
    std::vector<Railway> railways;
    std::string route(const std::string& from, const std::string& to);
};

#endif // RAIL_SYSTEM_H 


RailSystem.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <algorithm>
#include <memory>
#include "RailSystem.h"

std::string RailSystem::route(const std::string& from, const std::string& to)
{
    auto start = std::find_if(railways.begin(), railways.end(),
                            [&from](const Railway& p) {
                                for(const auto& r : p.stations) {
                                    if(r->name == from) { return true; }
                                }
                                return false;
                            });
    if(start == railways.cend()) {
        return std::string("station " + from + " doesn't exist.");
    }
    auto end = std::find_if(railways.cbegin(), railways.cend(),
                            [&to](const Railway& p) {
                                for(const auto& r : p.stations) {
                                    if(r->name == to) { return true; }
                                }
                                return false;
                            });
    if(end == railways.cend()) {
        return std::string("station " + to + " doesn't exist.");
    }

    // Both stations exist: find a route (*not* optimised).
    std::string route {"no known connection."};
    for(const auto& s1 : start->stations) {
        for(const auto& s2 : end->stations) {
            if(s1 == s2) {
                route = start->name + ' ' + from + " --> " 
                        + start->name + " (station of change) " + s1->name + " --> " 
                        + end->name + ' ' + to;
            }
        }
    }
    return route;
}

Topic archived. No new replies allowed.