我不能为我的生活理解为什么这不显示每个课程的课程信息。我一直在努力,因为我昨晚在这里询问另一件事。我不知道该做什么或为什么它不起作用,当它显示信息时它只显示它为第二类
#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 info\n";
cout << "Room number\n";
cin >> clss.RoomNum;
cout << "Lecture name\n";
cin >> clss.lname;
cout << "number of chairs in the class\n";
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 << "\n\n\n";
cout << "Class one info\n";
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: no\n";
}
if (clss.availability){
cout << "availability : yes" << endl;
}
else{
cout << "availability : no\n";
}
if (clss.projector){
cout << "projector : yes" << endl;
}
else{
cout << "projector : no\n";
}
}
void classinfo2(Classroom & clss1, vector<string> Stud1){
string answer1;
cout << "Please enter the second classrooms info\n";
cout << "Room number\n";
cin >> clss1.RoomNum;
cout << "Lecture name\n";
cin >> clss1.lname;
cout << "number of chairs in the class\n";
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 << "\n\n\n";
cout << "Class Two info\n";
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: no\n";
}
if (clss1.availability){
cout << "availability : yes" << endl;
}
else{
cout << "availability : no\n";
}
if (clss1.projector){
cout << "projector : yes" << endl;
}
else{
cout << "projector : no\n";
}
}
答案 0 :(得分:1)
void classinfo1(Classroom& clss, vector<string> Stud){
按值传递Stud
。换句话说,它会传递Stud
的副本,该副本仅在classinfo1
内可用。调用代码中的向量不受影响。
如果要编辑调用者的向量,则应该通过引用传递
void classinfo1(Classroom& clss, vector<string>& Stud){
// ^
同样适用于classinfo2
。你可能也应该将你的第二个向量 - test1
传递给它。您当前将相同的向量传递给两个函数,这意味着两个类的详细信息将合并为一个数组。
classinfo2(now, test1);
答案 1 :(得分:0)
您必须通过引用函数classinfo1和classinfo2
来传递向量void classinfo1(Classroom& clss, vector<string> &Stud);
void classinfo2(Classroom & clss1, vector<string> &Stud1);
考虑到在结构StudList
中定义数据成员Classroom
没有任何意义,因为它总是在代码片段中的函数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类型的局部变量会更好。