C++ 如何仅接受大写字母作为用户输入?
因此,我试图要求用户只输入大写字母,如果他们输入小写字母或任何其他符号或数字,我想要求他们输入新的输入。我正在努力,但我认为我做错了?请帮帮我C++ 如何仅接受大写字母作为用户输入?,c++,C++,因此,我试图要求用户只输入大写字母,如果他们输入小写字母或任何其他符号或数字,我想要求他们输入新的输入。我正在努力,但我认为我做错了?请帮帮我 #include <iostream> #include <string> using namespace std; int main (){ string teams[100] = {}; int removeLast=0; cout << "please enter ONLY capital l
#include <iostream>
#include <string>
using namespace std;
int main (){
string teams[100] = {};
int removeLast=0;
cout << "please enter ONLY capital letters, (TYPE 'done' to exit)\n";
for (int i=0;i<100;i++){
cin>> teams[i];
if (('A'>= teams[i] && 'Z'<= teams[i] )){ //problem is in this line
cout << "letter must be capital\n";
continue;
}
removeLast++;
if (teams[i] == "done") break;
}
cout << "this is the result you entered\n";
for (int m = 0; m < removeLast-1;m++ ){
cout << teams[m];
}
}
#包括
#包括
使用名称空间std;
int main(){
字符串组[100]={};
int-removeLast=0;
cout团队[i];
如果(('A'>=teams[i]&&&Z'字符串teams[100]={};
如果(('A'>=teams[i]&&&Z'>Z'){
//有一个错误
}
}
//如果它在没有错误的情况下到达这里,则输入符合您所需的条件
除了无法同时进行比较这一明显问题之外,您在程序中声明的类型也存在根本问题
string teams[100] = {};
声明一个包含100个字符串的数组,而不是一个包含100个字符的字符串。这在编译器错误中应该很明显,例如
strarr.cpp:15:17: error: no match for ‘operator>=’ (operand types are ‘char’
and ‘std::string {aka std::basic_string<char>}’)
if (('A'>= teams[i] && 'Z'<= teams[i] )){ //problem is in this line
现在还不清楚您是要强制所有字母都是大写字母还是仅第一个字母。如果您希望所有字母都是大写,则只需在输入的所有字符上包含一个标志并循环,如果不是所有字符都是大写,则将标志设置为false
。例如,您可以执行以下操作:
while (ndx < 100) { /* loop until max index or done */
bool allupper = true; /* flag indicating all uppercase */
if (!(cin >> teams[ndx])) { /* always validate your input */
cerr << "input error or user canceled.\n";
break;
}
if (teams[ndx] == "done") /* check exit condition */
break;
for (auto& c : teams[ndx]) /* simple loop checking each char */
if (!isupper(c) ) /* on failed test */
allupper = false; /* set flag false */
if (allupper) /* validate all uppercase characters */
ndx++; /* only increment if condition satisfied */
else /* handle not a captital */
cout << " error: letter must be capital\n";
}
(注意:虽然使用字符串数组
是完全可以的,但您通常希望使用容器,例如字符串
的向量
,例如向量
让容器根据需要管理您拥有的字符串数量,并免除您执行数组边界的责任)
示例使用/输出
$ ./bin/strarr
please enter ONLY capital letters, (TYPE 'done' to exit)
DOLPHINS
SAINTS
STEELeRS
error: letter must be capital
STEELERS
VIKINGS
done
this is the result you entered
DOLPHINS
SAINTS
STEELERS
VIKINGS
例如,仅检查第一个字符,只需替换上面的第一段代码。如果您还有其他问题,请告诉我。重新发明轮子从来不是一个好的解决方案。请看以下内容:
是的,您使用100个字符串也存在问题。为什么只将它们限制为输入大写字母?为什么不同时使用大写和小写字母,然后将输入转换为所有小写或所有大写字母?teams[i]
不能同时使用='Z'。您需要的条件是!(teams[i]>='A'&&teams[i]'Z')
添加一个while
循环,该循环仅在输入正确输入时退出。否则,请再次询问他们。与您的问题无关,但您不需要字符串数组。一个就足够了,请检查
while (ndx < 100) { /* loop until max index or done */
if (!(cin >> teams[ndx])) { /* always validate your input */
cerr << "input error or user canceled.\n";
break;
}
if (teams[ndx] == "done") /* check exit condition */
break;
else if (isupper (teams[ndx][0])) { /* use isupper() */
ndx++; /* only increment if condition satisfied */
}
else /* handle not a captital */
cout << " error: letter must be capital\n";
}
while (ndx < 100) { /* loop until max index or done */
bool allupper = true; /* flag indicating all uppercase */
if (!(cin >> teams[ndx])) { /* always validate your input */
cerr << "input error or user canceled.\n";
break;
}
if (teams[ndx] == "done") /* check exit condition */
break;
for (auto& c : teams[ndx]) /* simple loop checking each char */
if (!isupper(c) ) /* on failed test */
allupper = false; /* set flag false */
if (allupper) /* validate all uppercase characters */
ndx++; /* only increment if condition satisfied */
else /* handle not a captital */
cout << " error: letter must be capital\n";
}
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main (void) {
string teams[100] = {};
size_t ndx = 0;
cout << "please enter ONLY capital letters, (TYPE 'done' to exit)\n";
while (ndx < 100) { /* loop until max index or done */
bool allupper = true; /* flag indicating all uppercase */
if (!(cin >> teams[ndx])) { /* always validate your input */
cerr << "input error or user canceled.\n";
break;
}
if (teams[ndx] == "done") /* check exit condition */
break;
for (auto& c : teams[ndx]) /* simple loop checking each char */
if (!isupper(c) ) /* on failed test */
allupper = false; /* set flag false */
if (allupper) /* validate all uppercase characters */
ndx++; /* only increment if condition satisfied */
else /* handle not a captital */
cout << " error: letter must be capital\n";
}
cout << "this is the result you entered\n";
for (size_t m = 0; m < ndx; m++) {
cout << teams[m] << '\n';
}
}
$ ./bin/strarr
please enter ONLY capital letters, (TYPE 'done' to exit)
DOLPHINS
SAINTS
STEELeRS
error: letter must be capital
STEELERS
VIKINGS
done
this is the result you entered
DOLPHINS
SAINTS
STEELERS
VIKINGS