如何在c++中创建、访问和利用字符串数组

how to create, access, utilize string arrays in c++

本文关键字:字符串 数组 访问 c++ 创建      更新时间:2023-10-16

我是C++的新手,我想知道如何创建空的字符串数组集,到目前为止,我有这个:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{   
int numOfStudent;
char again;

do
{       
    cout << "How many students are in the class?";
    cin >> numOfStudent;
    while (numOfStudent > 30 || numOfStudent < 5)
    {
        cout << "The number of students must be in the range 5 - 30n";
        cout << "How many students are in the class?";
        cin >> numOfStudent;
    }

    for (int count = 1; count <= numOfStudent; count++)
        cout << "Enter the full name of student " << count << endl;
        cin >> // this is the part where i want to use an array to store
    cout << "Here is the list of students you have entered:n";
        for (int x = 1; x <= numOfStudent; x++)
            cout << x << ".  " // and this is the part where i want to read an array and print out

cout << "nDo you want to continue (y/n)?";
cin >> again;
} while (again == 'Y' || again == 'y');
}

我对C++真的很陌生,任何建议和帮助都将不胜感激。非常感谢。

这看起来像是一个家庭作业问题,但即使是这样,它也是非常基本的,代码答案应该有助于让你进入更复杂的主题:

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
int main()
{
    int numOfStudent;
    char again;
    vector<string> students;
    do
    {
        cout << "How many students are in the class?";
        cin >> numOfStudent;
        while (numOfStudent > 30 || numOfStudent < 5)
        {
            cout << "The number of students must be in the range 5 - 30n";
            cout << "How many students are in the class?";
            cin >> numOfStudent;
        }

        for (int count = 1; count <= numOfStudent; count++)
        {
            string name;
            cout << "Enter the full name of student " << count << endl;
            cin >> name; // this is the part where i want to use an array to store
            students.push_back(name);
        }
            cout << "Here is the list of students you have entered:n";
        for (int x = 1; x <= numOfStudent; x++)
            cout << "Student #"<<x << " is  "<<students.at(x-1)<<"."<<endl; // and this is the part where i want to read an array and print out

            cout << "nDo you want to continue (y/n)?";
        cin >> again;
    } while (again == 'Y' || again == 'y');
}

我使用了你的代码风格,这样你就能理解我的答案。您应该研究正确的命名空间使用(通常不建议使用using namespace std,但对于这样一个小样本来说,这是可以的)和迭代器(我使用了简单的for循环来迭代向量,但for(auto it=students.begin()...)是实现这一点的方法。

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{   
int numOfStudent;
char again;
vector<string> students;
string student;

do
{       
    cout << "How many students are in the class?";
    cin >> numOfStudent;
    while (numOfStudent > 30 || numOfStudent < 5)
    {
        cout << "The number of students must be in the range 5 - 30n";
        cout << "How many students are in the class?";
        cin >> numOfStudent;

    }

    for (int count = 1; count <= numOfStudent; count++)
    {   // all activities on the loop must be in the { }
        cout << "Enter the full name of student " << count << endl;
        cin >>student 
        students.push_back(student);
    }
    cout << "Here is the list of students you have entered:n";
        for (int x = 1; x <= numOfStudent; x++)
            cout << x << ".  "<<students[i] 


cout << "nDo you want to continue (y/n)?";
cin >> again;
} while (again == 'Y' || again == 'y');
}

This should work for you. If it does not work , use a vector iterator to read  student names from the vector.