我是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 - 30\n";
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 ++的新手,任何建议和帮助都会非常感激。谢谢!
答案 0 :(得分:1)
这看起来像是一个家庭作业问题,但即使是这样,它也是如此基本,以至于代码答案应该有助于您开始更复杂的主题:
#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 - 30\n";
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()...)
是这样做的方式。
答案 1 :(得分:0)
#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 - 30\n";
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.