C++ Can';我不能把任何东西存储到我的数组中

C++ Can';我不能把任何东西存储到我的数组中,c++,arrays,string,C++,Arrays,String,无法将字符串存储到数组中。我尝试了多维字符数组,但没有成功。理想情况下,我希望使用多维字符串数组,但每次尝试都会出现错误 无法将std::string(*)[100]{aka std::basic_string(*)[100]}转换为std::string* 我甚至不明白那是什么意思。我试着在我的函数input\u new\u student中打印数组,在这里我存储字符串只是为了测试它,但没有成功。同样的事情也发生在我的所有阵列上。我已经查过了,但我觉得我忽略了一些非常简单的事情,请帮忙 #in

无法将字符串存储到数组中。我尝试了多维字符数组,但没有成功。理想情况下,我希望使用多维字符串数组,但每次尝试都会出现错误

无法将
std::string(*)[100]{aka std::basic_string(*)[100]}
转换为
std::string*

我甚至不明白那是什么意思。我试着在我的函数input\u new\u 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;
}
#包括
#包括
#包括
#包括
#包括
无效打印菜单();
int get_selection();
std::string get_Name();
float getgpa();
int get_Year();
无效输入新学生(标准::字符串学生姓名[],浮动学生GPA[],国际学生起始年[],国际索引,国际学生id[]);
全部作废打印(标准::字符串学生姓名[],浮动学生GPA[],国际学生起始年[],国际索引,国际大小,国际拉马波id[]);
按年份作废打印(标准::字符串学生姓名[],浮动学生GPA[],整数学生起始年份[],整数索引,整数大小);
无效打印统计(标准::字符串学生姓名[],浮动学生GPA[],整数学生起始年[],整数索引,整数大小,浮动总和);
使用名称空间std;
int main()
{
std::string student_name[100];
浮动学生平均成绩[100];
int student_start_year[100];
国际拉马波岛id[100];
int用户选择;
int指数=0;
int size=0;
浮动总和=0.0;
做
{
打印菜单();
userChoice=get_selection();
if(userChoice==1)
{
输入新学生(学生姓名、学生GPA、学生起始年、索引、ramapo\u id);
索引++;
}
if(userChoice==2)
{
打印所有(学生姓名、学生GPA、学生起始年、索引、大小、ramapo\u id);
}
if(userChoice==3)
{
按学年打印(学生姓名、学生GPA、学生起始学年、索引、大小);
}
if(userChoice==4)
{
打印统计数据(学生姓名、学生GPA、学生起始年、索引、大小、总和);
}
if(userChoice==5)
{
返回0;
}
}而(userChoice>0&&userChoice<4);
返回0;
}
无效打印菜单()
{

cout您的代码几乎没有问题,例如:

  • 您的代码很难调试,因为您在获取输入时使用无限
    while
    循环(例如在
    main
    get\u selection
    函数中)

  • 您如何知道您的代码是否正常工作?至少,打印一些指示代码正常工作的内容,如
    cout我建议您使用一个结构容器,而不是多个容器:

    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];
    
    struct学生信息
    {
    std::字符串名;
    双gpa;
    起始年份的未签名整数;
    }
    typedef std::向量学生信息容器;
    学生信息容器数据库;
    //您还可以拥有一系列学生信息:
    静态常数最大值=32U;
    学生信息容器学生信息[最多学生];
    
    将信息放入结构中支持封装,使程序更加高效

    对于并行阵列,可能存在同步问题。例如,student 3的GPA可能位于阵列的索引4处


    如果仅限于数组,则只需将两个参数传递给函数,即数组和数组的容量。

    因此,作为更新,我无法打印数组的原因是因为我的for循环中没有开始递增。多亏了@milesbudnek,@dreschjerm。我能够看到我的代码实际上是通过add打印的这是在建议之前,但谢谢你的建议。这是现在的代码。目前我只需要找到一种方法来查找数组中的值(我将搜索),并为字符串创建多维数组

    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    无效打印菜单();
    int get_selection();
    std::string get_Name();
    float getgpa();
    int get_Year();
    无效输入新学生(标准::字符串学生姓名[],浮动学生GPA[],国际学生起始年[],国际索引,国际学生id[]);
    全部作废打印(标准::字符串学生姓名[],浮动学生GPA[],国际学生起始年[],国际索引,国际大小,国际拉马波id[]);
    按年份作废打印(标准::字符串学生姓名[],浮动学生GPA[],整数学生起始年份[],整数索引,整数大小);
    无效打印统计(标准::字符串学生姓名[],浮动学生GPA[],整数学生起始年[],整数索引,整数大小,浮动总和);
    使用名称空间std;
    int main()
    {
    std::string student_name[100];
    浮动学生平均成绩[100];
    int student_start_year[100];
    国际拉马波岛id[100];
    int用户选择;
    int指数=0;
    int size=0;
    浮动总和=0.0;
    //种子
    srand((未签名)时间(0));
    做
    {
    打印菜单();
    userChoice=get_selection();
    if(userChoice==1)
    {
    输入新学生(学生姓名、学生GPA、学生起始年、索引、ramapo\u id);
    索引++;
    大小++;
    }
    if(userChoice==2)
    {
    打印所有(学生姓名、学生GPA、学生起始年、索引、大小、ramapo\u id);
    }
    if(userChoice==3)
    {
    按学年打印(学生姓名、学生GPA、学生起始学年、索引、大小);
    }
    if(userChoice==4)
    {
    打印统计数据(学生姓名、学生GPA、学生起始年、索引、大小、总和);
    }
    if(userChoice==5)
    {
    返回0;
    }
    }而(userChoice>0&&userChoice<5);
    返回0;
    }
    无效打印菜单()
    {
    
    CUT应该避免不止一次调用<代码> SRAND 。考虑创建一个类来包装所有这些数据,使用向量(可能是元组)