将字符串存储到数组中时遇到问题。我尝试了一个多维char数组,但它没有工作。理想情况下,我喜欢多维字符串数组,但每次尝试我都会得到错误
无法将std::string (*)[100] {aka std::basic_string<char> (*)[100]}
转换为std::string*
甚至不明白这意味着什么。我尝试在我的函数 input_new_student 中打印数组,这是我存储字符串的地方,只是为了测试它,没有运气。我的所有阵列都发生了同样的事情。我已经查了一下,但我觉得我很容易忽略一些非常简单的事情,请帮忙。
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
void print_menu();
int get_selection();
std::string get_Name();
float get_GPA();
int get_Year();
void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[]);
void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[]);
void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size);
void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum);
using namespace std;
int main()
{
std::string student_names[100];
float student_GPA[100];
int student_start_year[100];
int ramapo_id[100];
int userChoice;
int index = 0;
int size = 0;
float sum = 0.0;
do
{
print_menu();
userChoice = get_selection();
if (userChoice == 1)
{
input_new_student(student_names, student_GPA, student_start_year, index, ramapo_id);
index++;
}
if (userChoice == 2)
{
print_all(student_names, student_GPA, student_start_year, index, size, ramapo_id);
}
if (userChoice == 3)
{
print_by_year(student_names, student_GPA, student_start_year, index, size);
}
if (userChoice == 4)
{
print_statistics(student_names, student_GPA, student_start_year, index, size, sum);
}
if (userChoice == 5)
{
return 0;
}
} while(userChoice > 0 && userChoice < 4);
return 0;
}
void print_menu()
{
cout << "Please pick from the following menu " << endl;
cout << "1. Add a new student " << endl;
cout << "2. Print all students " << endl;
cout << "3. Print students by year " << endl;
cout << "4. Print student statistics " << endl;
cout << "5. Quit" << endl;
}
int get_selection()
{
int userChoice;
cin >> userChoice;
while (userChoice > 4 || userChoice < 1)
{
cout << "Error: Invalid input, please try again: ";
cin >> userChoice;
}
return userChoice;
}
string get_Name()
{
std::string student_name;
cout << "Please enter the student's name: ";
cin >> student_name;
return student_name;
}
float get_GPA()
{
float student_GPA;
cout << "Please enter the GPA: ";
cin >> student_GPA;
return student_GPA;
}
int get_Year()
{
int student_year;
cout << "Please enter the start Year: ";
cin >> student_year;
return student_year;
}
void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[])
{
//information generation
srand((unsigned)time(0));
int random_integer = rand();
ramapo_id[index] = random_integer;
//information acquisition
student_names[index] = get_Name();
student_GPA[index] = get_GPA();
student_start_year[index] = get_Year();
}
void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[])
{
for (int i = 0; i < size; i++) {
cout << student_names[i] << " - " << ramapo_id[i] << endl;
}
}
void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size)
{
int student_year_identifier;
cout << "Which year would you like to display?: ";
cin >> student_year_identifier;
for (int i = 0; i < size; i++)
{
if (student_year_identifier == student_start_year[i])
{
cout << "There were " << index << "students in that year" << endl;
}
else {
cout << "There were no students in that year" << endl;
}
}
}
void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum)
{
cout << "Total: " << index << endl;
float avg = 0.0;
for (int i = 0; i < size; ++i)
{
sum += student_GPA[i];
}
avg = ((float)sum)/size;
cout << "GPA: " << avg << endl;
}
答案 0 :(得分:1)
我建议你有一个容器结构而不是多个容器:
struct Student_Info
{
std::string name;
double gpa;
unsigned int starting_year;
}
typedef std::vector<Student_Info> Student_Info_Container;
Student_Info_Container database;
// You could also have an array of student information:
static const size_t MAXIMUM_STUDENTS = 32U;
Student_Info_Container student_information[MAXIMUM_STUDENTS];
将信息放入结构支持封装中,使程序更有效。
使用并行阵列时,可能会出现同步问题。例如,学生3的GPA可以在阵列的索引4处。
如果您只限于数组,则只需要将2个参数传递给函数,数组和数组的容量。
答案 1 :(得分:0)
您的代码存在一些问题,例如:
while
个循环(例如,在main
和get_selection
函数中)。cout << "Student added suuccessfully";
函数中的input_new_student
。我做了上述变更,您可以看到(至少)您的代码适用于http://ideone.com/D5x8Eh的选项1
PS:我没有得到编译错误,你应该提一下你正在使用的编译器和标志。
答案 2 :(得分:0)
所以作为一个更新,我无法打印我的数组的原因开始我没有在我的for循环中递增。感谢@milesbudnek,@ dreschjerm。我能够看到我的代码实际上是通过添加消息进行打印,这是在建议之前,但感谢您的建议。这是现在的代码。目前我只需要找到一种方法来查找数组中的值(我将搜索它)并为字符串创建多维数组。
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
void print_menu();
int get_selection();
std::string get_Name();
float get_GPA();
int get_Year();
void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[]);
void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[]);
void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size);
void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum);
using namespace std;
int main()
{
std::string student_names[100];
float student_GPA[100];
int student_start_year[100];
int ramapo_id[100];
int userChoice;
int index = 0;
int size = 0;
float sum = 0.0;
//seed
srand((unsigned)time(0));
do
{
print_menu();
userChoice = get_selection();
if (userChoice == 1)
{
input_new_student(student_names, student_GPA, student_start_year, index, ramapo_id);
index++;
size++;
}
if (userChoice == 2)
{
print_all(student_names, student_GPA, student_start_year, index, size, ramapo_id);
}
if (userChoice == 3)
{
print_by_year(student_names, student_GPA, student_start_year, index, size);
}
if (userChoice == 4)
{
print_statistics(student_names, student_GPA, student_start_year, index, size, sum);
}
if (userChoice == 5)
{
return 0;
}
} while(userChoice > 0 && userChoice < 5);
return 0;
}
void print_menu()
{
cout << "Please pick from the following menu " << endl;
cout << "1. Add a new student " << endl;
cout << "2. Print all students " << endl;
cout << "3. Print students by year " << endl;
cout << "4. Print student statistics " << endl;
cout << "5. Quit" << endl;
}
int get_selection()
{
int userChoice;
cin >> userChoice;
while (userChoice > 5 || userChoice < 1)
{
cout << "Error: Invalid input, please try again: ";
cin >> userChoice;
}
return userChoice;
}
string get_Name()
{
std::string student_name;
cout << "Please enter the student's name: ";
cin >> student_name;
return student_name;
}
float get_GPA()
{
float student_GPA;
do {
cout << "Please enter the GPA: ";
cin >> student_GPA;
} while(!(student_GPA < 4.0 && student_GPA > 0.0));
return student_GPA;
}
int get_Year()
{
int student_year;
do {
cout << "Please enter the start Year: ";
cin >> student_year;
} while(!(student_year > 1972 && student_year < 2015));
return student_year;
}
void input_new_student(std::string student_names[], float student_GPA[], int student_start_year[], int index, int ramapo_id[])
{
//information generation
int random_integer = rand()%900000 + 100000;
ramapo_id[index] = random_integer;
//information acquisition
student_names[index] = get_Name();
student_GPA[index] = get_GPA();
student_start_year[index] = get_Year();
//Notification
cout << endl;
cout << "The student with R# " << random_integer << " was created." << endl;
cout << endl;
}
void print_all(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, int ramapo_id[])
{
cout << endl;
for (int i = 0; i < size; i++) {
cout << student_names[i] << " - " << ramapo_id[i] << endl;
}
cout << endl;
}
void print_by_year(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size)
{
int student_year_identifier;
//to trigger the response that there ARE students in that year
bool trigger;
int student_count = 0;
cout << "Which year would you like to display?: ";
cin >> student_year_identifier;
for (int i = 0; i < size; i++)
{
if (student_start_year[i] == student_year_identifier)
{
bool trigger = true;
student_count++;
}
}
if (trigger = true) {
cout << endl;
cout << "There are " << student_count << " student(s) in that year" << endl;
cout << endl;
}
else {
cout << endl;
cout << "There are no students in that year." << endl;
cout << endl;
}
}
void print_statistics(std::string student_names[], float student_GPA[], int student_start_year[], int index, int size, float sum)
{
//Print Total
cout << "Total: " << index << endl;
//Print GPA average
int smart_kids = 0;
float avg = 0.0;
for (int i = 0; i < size; ++i)
{
sum += student_GPA[i];
}
avg = ((float)sum)/size;
cout << "GPA: " << std::setprecision(3) << avg << endl;
//Print # of students above 2.0
for (int i = 0; i < size; i++)
{
if (student_GPA[i] > 2.0)
{
smart_kids++;
}
}
cout << "Above a 2.0: " << smart_kids << endl;
}