Is there a way to display all saved objects in vector classes?

一曲冷凌霜 提交于 2020-05-17 05:47:12

问题


I coded this for a bus system but having trouble displaying the objects that have been saved in stud1. I tried using readData but didn't work. The purpose of the code is to 1. receive input(s) in the form of bus info from the user and save them and 2. output all buses input into the system(reposted altered code)

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
string busType, busMake, regNum;
char menu();
int id = 0;
//int staff[50];
int carObjNum, option0;
int temp = 0;
char objArray[5];
string busMake0, busType0, regNum0;

class bus
{
public:
int i;
string busType;
string busMake;
string regNum;
char input();
char transHistory();

bus(string id = "", string name = "", string phone = "") : busMake(id), busType(name), regNum(phone)
{}

bool operator==(const bus & obj)
{
    return (busMake == obj.busMake) && (busType == obj.busType) && (regNum == obj.regNum);
}

/*
 * Write the member variables to stream objects
 */
friend ostream & operator << (ostream &out, const bus & obj)
{
    out << obj.busMake << "\n" <<obj.busType<<"\n"<<obj.regNum<<endl;
    return out;
}
/*
 * Read data from stream object and fill it in member variables
 */
friend istream & operator >> (istream &in,  bus &obj)
{
    in >> obj.busMake;
    in >> obj.busType;
    in >> obj.regNum;
    return in;
}


};

char bus::input(){
cout<<"Enter bus make\n"<<endl;
cin>>busMake0;
cout<<"Enter bus Type\n"<<endl;
cin>>busType0;
cout<<"Enter registration number\n"<<endl;
cin>>regNum;

vector<bus> vec = {};
bus stud1(busMake,busType, regNum);
vec.push_back(stud1);
ofstream out("bus.txt");
out<<stud1;
out.close();
// Open the File
ifstream in("bus.txt");
bus bus1;
in>>bus1;
in.close();

for(bus n : vec) {
           std::cout << n << '\n';
       }
return 0;
}

char bus::transHistory(){
bus stud1;

//Open the file that you just saved.
ifstream out("bus.txt");
//need this function to be able to read what was saved in stud1 at bus::input()
//then after that have all info output to user upon request.
out.close();
return 0;
}

int x;

char menu(){
int option;
cout<<"Welcome to the GTUC repair system\n"<<endl;
cout<<"What would you like to do?\n"<<endl;
cout<<""<<endl;
cout<<"Enter '1' to enter a new repair\n"<<endl;
cout<<"Enter '2' to print total transaction history\n"<<endl;
cin>>option;
option0 = option;
return option;
}

int main()
{
bus decision;
menu();
switch (option0) {
    case 1:
        decision.input();
        menu();
    case 2:
        decision.transHistory();
    default:
        break;

}
return 0;
}

回答1:


The program is very buggy and also the design is wrong. You have sted::vector which is always defined locally and will therefore always hold one element.

Here the major bug fixes that make your code print something.

You need to define a default constructor for bus. Your definition is wrong.

In your input function you read variables into "busMake0" and "busType0". But then you do not use those variables, when creating a bus.

By using a debugger, you will find this problem in 1 minute.

You are using tons of global variables. Don't do that. Your switch statement is not in a loop and has no break.

Many many other design errors.

What you should do: Before starting to write any line of code, please sit there for 1 complete day, think, what should be done, and then how it should be done. Then start coding. Start with writing comments in your source files. Then afterwards, add the code. Do identation. Format your code. Use meaningful variable names. Do never use global variables. Do not use using namespace std;

Please see your code with minimum corrections.

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

string busType, busMake, regNum;
char menu();
int id = 0;
int carObjNum, option0;
int temp = 0;
char objArray[5];
string busMake0, busType0, regNum0;

class bus {
public:
    int i;
    string busType;
    string busMake;
    string regNum;
    char input();
    char transHistory();
    bus() : busMake(""), busType(""), regNum("") {} 
    bus(string id, string name, string phone) : busMake(id), busType(name), regNum(phone) {}

    bool operator==(const bus& obj) {
        return (busMake == obj.busMake) && (busType == obj.busType) && (regNum == obj.regNum);
    }

    /*
     * Write the member variables to stream objects
     */
    friend ostream& operator << (ostream& out, const bus& obj)  {
        out << obj.busMake << "\n" << obj.busType << "\n" << obj.regNum << endl;
        return out;
    }
    /*
     * Read data from stream object and fill it in member variables
     */
    friend istream& operator >> (istream& in, bus& obj) {
        in >> obj.busMake;
        in >> obj.busType;
        in >> obj.regNum;
        return in;
    }
};

char bus::input() {
    cout << "Enter bus make\n" << endl;
    cin >> busMake0;
    cout << "Enter bus Type\n" << endl;
    cin >> busType0;
    cout << "Enter registration number\n" << endl;
    cin >> regNum;

    vector<bus> vec = {};
    bus stud1(busMake0, busType0, regNum);
    vec.push_back(stud1);
    ofstream out("bus.txt");
    out << stud1;
    out.close();

    // Open the File
    ifstream in("bus.txt");
    bus bus1;
    in >> bus1;
    in.close();

    for (bus n : vec) {
        std::cout << n << '\n';
    }
    return 0;
}

char bus::transHistory() {
    bus stud1;

    //Open the file that you just saved.
    ifstream out("bus.txt");
    //need this function to be able to read what was saved in stud1 at bus::input()
    //then after that have all info output to user upon request.
    out.close();
    return 0;
}

int x;

char menu() {
    int option;
    cout << "Welcome to the GTUC repair system\n" << endl;
    cout << "What would you like to do?\n" << endl;
    cout << "" << endl;
    cout << "Enter '1' to enter a new repair\n" << endl;
    cout << "Enter '2' to print total transaction history\n" << endl;
    cin >> option;
    option0 = option;
    return option;
}

int main()
{
    bus decision;
    menu();
    switch (option0) {
    case 1:
        decision.input();
        menu();
    case 2:
        decision.transHistory();
    default:
        break;
    }
    return 0;
}

Sorry to say that I cannot help you further. I do not even understand the task fully.



来源:https://stackoverflow.com/questions/61563090/is-there-a-way-to-display-all-saved-objects-in-vector-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!