有没有一种方法可以显示矢量类中的所有已保存对象? [英] Is there a way to display all saved objects in vector classes?

查看:86
本文介绍了有没有一种方法可以显示矢量类中的所有已保存对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为总线系统编写了此代码,但是无法显示保存在 stud1 中的对象.我尝试使用readData,但是没有用.该代码的目的是:1.以用户总线信息的形式接收输入并保存输入; 2.将所有总线输入输出到系统中(重新发布更改后的代码)

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;
}

推荐答案

该程序存在很多错误,设计也有误. sted::vector始终在本地定义,因此将始终包含一个元素.

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.

您需要为bus定义默认的构造函数.您的定义是错误的.

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

在输入函数中,将变量读取到"busMake0"和"busType0"中.但是,那么在创建总线时就不必使用这些变量.

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

通过使用调试器,您将在1分钟内发现此问题.

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

您正在使用大量的全局变量.不要那样做您的switch语句不在循环中,并且没有break.

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

许多其他设计错误.

您应该做什么:在开始编写任何代码行之前,请坐在那里整整1天,思考,应该做什么,然后应该怎么做.然后开始编码.首先在源文件中编写注释.然后,添加代码.做识别.格式化代码.使用有意义的变量名.不要使用全局变量.不要使用using namespace std;

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.

这篇关于有没有一种方法可以显示矢量类中的所有已保存对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆