不显示输入的第二组信息

  • 本文关键字:二组 信息 显示 c++
  • 更新时间 :
  • 英文 :


我不能为我的生活理解为什么这不是显示类信息为我采取的每一个类。自从我昨晚来这里询问另一件事以来,我一直在做这件事。我不知道该怎么做,或者为什么它不工作,当它显示信息时,它只显示它的第二个类

#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct Classroom{
int Studn;
int Studn1;
int RoomNum;
string lname;
string StudList;
int Chairs;
bool window;
bool projector;
bool availability;
};
void classinfo1(Classroom& clss, vector<string> Stud);
void output(Classroom& clss, vector<string> Stud);
void classinfo2(Classroom & clss1, vector<string> Stud1);
void output1(Classroom & clss1, vector<string> Stud1);

int main()
 {
vector<string> test;
vector<string> test1;
Classroom now;
Classroom now1;
float a, b;

classinfo1(now,test);
classinfo2(now, test);
output(now,test);
output1(now, test);

system("pause");
return 0;
 }
 void classinfo1(Classroom& clss, vector<string> Stud){

string answer;

cout << "Please enter the first classrooms infon";
cout << "Room numbern";
cin >> clss.RoomNum;
cout << "Lecture namen";
cin >> clss.lname;
cout << "number of chairs in the classn";
cin >> clss.Chairs;
cout << "How many students is in your class?n";
cin >> clss.Studn;
for (int i = 0; i < clss.Studn; i++){
    cout << "enter student " << i + 1 << endl;
    cin >> clss.StudList;
    Stud.push_back(clss.StudList);
}
cout << "Does the class have a window?" << endl;
cin >> answer;
if (answer == "Yes" || answer == "yes" || answer == "YES"){
    clss.window = true;
}
if (answer == "no" || answer == "No" || answer == "NO"){
    clss.window = false;
}
cout << "is the class available?" << endl;
cin >> answer;
if (answer == "Yes" || answer == "yes" || answer == "YES"){
    clss.availability = true;
}
if (answer == "no" || answer == "No" || answer == "NO"){
    clss.availability = false;
}
cout << "does it have a projector?" << endl;
cin >> answer;
if (answer == "Yes" || answer == "yes" || answer == "YES"){
    clss.projector = true;
}
if (answer == "no" || answer == "No" || answer == "NO"){
    clss.projector = false;
}

 system("cls");
 }
 void output(Classroom& clss, vector<string> Stud){

cout << "nnn";
cout << "Class one infon";
cout << "Room number:" << clss.RoomNum << endl;
cout << "Lecture Name:" << clss.lname << endl;
cout << "Num of Chairs:" << clss.Chairs << endl;
cout << "Num of Students:" << clss.Studn << endl;
cout << "Student names:" << endl;
vector<string>::iterator studorder;
sort(Stud.begin(), Stud.end());
for (studorder = Stud.begin(); studorder < Stud.end(); ++studorder){
    cout << *studorder << endl;
}
if (clss.window){
    cout << "window : yes" << endl;
}
else{
    cout << "window: non";
}
if (clss.availability){
    cout << "availability : yes" << endl;
}
else{
    cout << "availability : non";
}
if (clss.projector){
    cout << "projector : yes" << endl;
}
else{
    cout << "projector : non";
}
 }
void classinfo2(Classroom & clss1, vector<string> Stud1){
string answer1;

cout << "Please enter the second classrooms infon";
cout << "Room numbern";
cin >> clss1.RoomNum;
cout << "Lecture namen";
cin >> clss1.lname;
cout << "number of chairs in the classn";
cin >> clss1.Chairs;
cout << "How many students is in your class?n";
cin >> clss1.Studn;
for (int i = 0; i < clss1.Studn; i++){
    cout << "enter student " << i + 1 << endl;
    cin >> clss1.StudList;
    Stud1.push_back(clss1.StudList);
}
cout << "Does the class have a window?" << endl;
cin >> answer1;
if (answer1 == "Yes" || answer1 == "yes" || answer1 == "YES"){
    clss1.window = true;
}
if (answer1 == "no" || answer1 == "No" || answer1 == "NO"){
    clss1.window = false;
}
cout << "is the class available?" << endl;
cin >> answer1;
if (answer1 == "Yes" || answer1 == "yes" || answer1 == "YES"){
    clss1.availability = true;
}
if (answer1 == "no" || answer1 == "No" || answer1 == "NO"){
    clss1.availability = false;
}
cout << "does it have a projector?" << endl;
cin >> answer1;
if (answer1 == "Yes" || answer1 == "yes" || answer1 == "YES"){
    clss1.projector = true;
}
if (answer1 == "no" || answer1 == "No" || answer1 == "NO"){
    clss1.projector = false;
}

}
 void output1(Classroom & clss1, vector<string> Stud1){

cout << "nnn";
cout << "Class Two infon";
cout << "Room number:" << clss1.RoomNum << endl;
cout << "Lecture Name:" << clss1.lname << endl;
cout << "Num of Chairs:" << clss1.Chairs << endl;
cout << "Num of Students:" << clss1.Studn << endl;
cout << "Student names:" << endl;
vector<string>::iterator studorder1;
sort(Stud1.begin(), Stud1.end());
for (studorder1 = Stud1.begin(); studorder1 < Stud1.end(); ++studorder1){
    cout << *studorder1 << endl;
}
if (clss1.window){
    cout << "window : yes" << endl;
}
else{
    cout << "window: non";
}
if (clss1.availability){
    cout << "availability : yes" << endl;
}
else{
    cout << "availability : non";
}
if (clss1.projector){
    cout << "projector : yes" << endl;
}
else{
    cout << "projector : non";
}
}
void classinfo1(Classroom& clss, vector<string> Stud){

按值传递Stud。换句话说,它传递的是Stud的副本,该副本只能在classinfo1内部使用。调用代码中的vector不受影响。

如果要编辑调用者的vector,则应该通过引用传递

void classinfo1(Classroom& clss, vector<string>& Stud){
//                                             ^

同样适用于classinfo2。您可能还应该将第二个矢量- test1传递给它。当前,您将同一个向量传递给两个函数,这意味着两个类的详细信息被组合到一个数组中。

classinfo2(now, test1);

必须通过对函数classinfo1和classinfo2的引用来传递向量

void classinfo1(Classroom& clss, vector<string> &Stud);
void classinfo2(Classroom & clss1, vector<string> &Stud1);

考虑到在结构Classroom中定义数据成员StudList没有任何意义,因为它总是在代码片段

中的函数classinfo1和classinfo2中被覆盖
for (int i = 0; i < clss.Studn; i++){
    cout << "enter student " << i + 1 << endl;
    cin >> clss.StudList;
    Stud.push_back(clss.StudList);
}

如果从结构中删除该数据成员,并在上面所示的循环中使用std::string类型的局部变量,效果会更好。

最新更新