Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有没有办法在矢量类中显示所有保存的对象?_C++_Class_Stdvector_Ofstream - Fatal编程技术网

C++ 有没有办法在矢量类中显示所有保存的对象?

C++ 有没有办法在矢量类中显示所有保存的对象?,c++,class,stdvector,ofstream,C++,Class,Stdvector,Ofstream,我为一个总线系统编写了这个代码,但是在显示stud1中保存的对象时遇到了问题。我尝试使用readData,但没有成功。本规范的目的是为了1。从用户处接收总线信息形式的输入,并保存它们和2。输出输入到系统中的所有总线(重新提交的更改代码) #包括 #包括 #包括 使用名称空间std; 字符串busType、busMake、regNum; 字符菜单(); int id=0; //国际工作人员[50]; int carObjNum,选项0; 内部温度=0; char-objArray[5]; 字符串b

我为一个总线系统编写了这个代码,但是在显示stud1中保存的对象时遇到了问题。我尝试使用readData,但没有成功。本规范的目的是为了1。从用户处接收总线信息形式的输入,并保存它们和2。输出输入到系统中的所有总线(重新提交的更改代码)

#包括
#包括
#包括
使用名称空间std;
字符串busType、busMake、regNum;
字符菜单();
int id=0;
//国际工作人员[50];
int carObjNum,选项0;
内部温度=0;
char-objArray[5];
字符串busMake0、busType0、regNum0;
班车
{
公众:
int i;
字符串busType;
串母线;
字符串regNum;
字符输入();
char transHistory();
总线(字符串id=”,字符串名称=”,字符串电话=”):总线制造商(id),总线类型(名称),注册表号(电话)
{}
布尔运算符==(常数总线和obj)
{
返回(busMake==obj.busMake)&&&(busType==obj.busType)&&(regNum==obj.regNum);
}
/*
*将成员变量写入流对象
*/

friend ostream&operator程序有很多错误,而且设计也有错误。您有
sted::vector
,它总是在本地定义,因此总是包含一个元素

这里是使您的代码打印某些内容的主要错误修复

您需要为总线定义默认构造函数。您的定义错误

在输入函数中,您将变量读入“busMake0”和“busType0”。但在创建总线时,您不使用这些变量

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

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

许多其他设计错误

你应该做什么:在开始写任何一行代码之前,请坐在那里整整一天,思考应该做什么,然后怎么做。然后开始编码。从在源文件中写注释开始。然后,添加代码。进行标识。格式化代码。使用有意义的变量名。永远不要使用全局变量变量。不要使用
名称空间std;

请查看您的代码和最低限度的更正

#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;
}
#包括
#包括
#包括
使用名称空间std;
字符串busType、busMake、regNum;
字符菜单();
int id=0;
int carObjNum,选项0;
内部温度=0;
char-objArray[5];
字符串busMake0、busType0、regNum0;
班车{
公众:
int i;
字符串busType;
串母线;
字符串regNum;
字符输入();
char transHistory();
总线():busMake(“”),busType(“”),regNum(“”{}
总线(字符串id、字符串名称、字符串电话):busMake(id)、busType(名称)、regNum(电话){}
布尔运算符==(常数总线和obj){
返回(busMake==obj.busMake)&&&(busType==obj.busType)&&(regNum==obj.regNum);
}
/*
*将成员变量写入流对象
*/
friend ostream&operator>obj.regNum;
返回;
}
};
char总线::输入(){
cout-busMake0;
cout-busType0;
cout regNum;
向量vec={};
总线stud1(busMake0、busType0、regNum);
向量推回(stud1);
输出流(“bus.txt”);
out>bus1;
in.close();
用于(总线n:vec){

std::cout什么是
readData
?您确定您在显示对象时遇到问题,而不是保存对象吗?我想显示我在bus.txt中保存的所有对象,但在执行时遇到问题您遇到了什么问题?请提供“未工作”的详细信息.当程序运行时,当触发
case 1
时,用户输入总线信息,然后程序返回菜单。当触发
case 2
后,它应打印所有已输入的总线信息,但为空
case 2
的输出为空,因为
总线中没有打印语句:transHistory()
。实现这一点。我是一名仍在学习编码的学生。我正在学校实习
#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;
}