【发布时间】:2020-06-24 15:25:54
【问题描述】:
我的班级给我一个 C++ 作业,让我编写一个程序,该程序从用户那里读取学生数据记录,然后该程序会将键入的数据记录按照学生的 GPA 或按照排序顺序排序学生姓氏的字母顺序,或者两者都做,这取决于用户的选择。这个框架是我的教授给我的。
我在使用向量和字符串时遇到了困难,要让它们在我给定的程序中工作。我不断收到以下错误:
没有匹配函数调用‘std::vector::push_back(std::string&)’
没有匹配的函数调用'std::vector::push_back(float&)'
我在下面包含了我的完整代码,如果有人能看一下,我将不胜感激。我知道需要为 v.push_back(); 更改某些内容;工作,但我不确定我应该做的确切改变。我是 C++ 新手,对字符串和向量了解有限。
非常感谢您!
#include <iostream>
#include <cctype>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
struct student
{
string name;
float gpa;
};
std::vector <student> v;
void input_data(std::vector<student> &v);
void output_data();
void swap(student *std1, student *std2);
void sort_gpa_asc();
void sort_gpa_des();
void sort_name();
void show_data();
int num;
int main()
{
char option;
v.push_back(student{"Dana", 3.6});
input_data(v);
cout << "Your options for sorting (key in 'G' to sort students' data in terms of gpa in" << endl
<< "both ascending and descending order; key in 'A' to sort students' data in terms of" << endl
<< "alphabetical order of students' last name; key in 'B' for both types of sorting): ";
cin >> option;
int num;
if (option == 'G')
{
sort_gpa_asc();
sort_gpa_des();
}
else if (option == 'A')
{
sort_name();
}
else if (option == 'B')
{
sort_gpa_asc();
sort_gpa_des();
sort_name();
}
else
cout << "Incorrect option!" << endl;
return 0;
}
void input_data(std::vector<student> &v)
{
string names;
float grade_point_average;
int num;
v.push_back(student{"Dana", 3.6});
cout << "Enter the number of students: ";
cin >> num;
for(int i=0; i < num; i++){
cout << "Enter student's name (write the surname before the first name): ";
cin.get();
getline(cin, names);
v.push_back(names);
}
for(int i=0; i < num; i++){
cout << "Enter student's GPA: ";
cin >> grade_point_average;
v.push_back(grade_point_average);
}
for(int i=0; i < v.size(); i++){
cout << v[i].gpa;
}
for(int i=0; i < v.size(); i++){
cout << v[i].name;
}
cin.get();
cin.get();
}
void swap(student *std1, student *std2)
{
}
void sort_gpa_asc()
{
}
void sort_gpa_des()
{
}
void sort_name()
{
}
void show_data()
{
}
【问题讨论】: