限制一组内的字符串长度?
Limit string length within a set?
我必须编写一个程序,允许用户输入 最多 20 个名称,每个 最多 40 个字符。
当我在不尝试以任何方式限制字符串长度的情况下编写代码时,它起作用了。但是当我尝试使用 if/else 语句限制字符串长度时,它不起作用。我对 C++ 很陌生,所以它真的是在黑暗中拍摄。我做错了什么?
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void print(const string& name) {
cout << name << endl;
}
int main() {
set<string> ListOfNames;
cout << "Please enter up to 20 names of up to 40 characters each below: " << endl;
for (int i = 1; i <= 20; ++i) {
string name;
cout << i << ". ";
getline(cin, name);
if (name.size() >= 40) {
ListOfNames.insert(name);
}
else break;
cerr << "You entered more than 40 characters. Please try again.";
}
for_each(ListOfNames.begin(), ListOfNames.end(), &print);
return 0;
}
输出:
1. (user inputs name here)
press any key to continue...
已编辑代码:
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void print(const string& name) {
cout << name << endl;
}
int main() {
set<string> ListOfNames;
cout << "Please enter up to 20 names of up to 40 characters each below: " << endl;
for (int i = 1; i <= 20; ++i) {
string name;
cout << i << ". ";
getline(cin, name);
if (name.size() <= 40) {
ListOfNames.insert(name);
}
else
{
cerr << "You entered more than 40 characters. Please try again.";
break;
}
for_each(ListOfNames.begin(), ListOfNames.end(), &print);
return 0;
}
}
好像你说的只有 运行 如果字符串大于 40 且不小于 40
里面if把条件改成name.size() >= 40
并且在 else break after crr 中,两条语句都应该在 {}
中
if (name.size() <= 40) {
ListOfNames.insert(name);
}
else
{
cerr << "You entered more than 40 characters. Please try again.";
break;
}
编写一个单独的函数来获取和验证输入。在该函数中,检查输入的长度是否小于 40 个字符,如果小于则拒绝接受:
std::string get_limited_string(std::string prompt, int max = 40) {
std::string input;
do {
std::cout << prompt;
std::getline(std::cin, input);
} while (input.size() >= max);
return input;
}
我必须编写一个程序,允许用户输入 最多 20 个名称,每个 最多 40 个字符。
当我在不尝试以任何方式限制字符串长度的情况下编写代码时,它起作用了。但是当我尝试使用 if/else 语句限制字符串长度时,它不起作用。我对 C++ 很陌生,所以它真的是在黑暗中拍摄。我做错了什么?
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void print(const string& name) {
cout << name << endl;
}
int main() {
set<string> ListOfNames;
cout << "Please enter up to 20 names of up to 40 characters each below: " << endl;
for (int i = 1; i <= 20; ++i) {
string name;
cout << i << ". ";
getline(cin, name);
if (name.size() >= 40) {
ListOfNames.insert(name);
}
else break;
cerr << "You entered more than 40 characters. Please try again.";
}
for_each(ListOfNames.begin(), ListOfNames.end(), &print);
return 0;
}
输出:
1. (user inputs name here)
press any key to continue...
已编辑代码:
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void print(const string& name) {
cout << name << endl;
}
int main() {
set<string> ListOfNames;
cout << "Please enter up to 20 names of up to 40 characters each below: " << endl;
for (int i = 1; i <= 20; ++i) {
string name;
cout << i << ". ";
getline(cin, name);
if (name.size() <= 40) {
ListOfNames.insert(name);
}
else
{
cerr << "You entered more than 40 characters. Please try again.";
break;
}
for_each(ListOfNames.begin(), ListOfNames.end(), &print);
return 0;
}
}
好像你说的只有 运行 如果字符串大于 40 且不小于 40
里面if把条件改成name.size() >= 40
并且在 else break after crr 中,两条语句都应该在 {}
中if (name.size() <= 40) {
ListOfNames.insert(name);
}
else
{
cerr << "You entered more than 40 characters. Please try again.";
break;
}
编写一个单独的函数来获取和验证输入。在该函数中,检查输入的长度是否小于 40 个字符,如果小于则拒绝接受:
std::string get_limited_string(std::string prompt, int max = 40) {
std::string input;
do {
std::cout << prompt;
std::getline(std::cin, input);
} while (input.size() >= max);
return input;
}