【发布时间】:2016-10-22 17:58:41
【问题描述】:
我正在处理用户输入的字符串输入,我需要使用 switch 语句来评估每个输入的输入。我下面的代码目前评估用户字符串输入,并使用 ASCII 代码查看它是大写字母、数字还是特殊字符。我现在确定 switch 语句的工作原理以及如何将 If 语句更改为 switch 语句。
for (int i = 0; i < strlength; i++) //for loop used to check the rules of the password inputted by the user
{
cout << "Testing for upper case characters..." << endl; //displays the cout
tmpi=(int) str1[i]; //stoi function making the string input an integer
if ((tmpi >= 65) && (tmpi <= 90)) //checks if there are two upper case characters in the string
{
cout << "Found an uppercase" << endl;
uppercnt++; //adds to the counter of upper case
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Testing for digits..." << endl;
if(tmpi >= 48 && tmpi <= 57) //checks if there are two digits in the string
{
cout << "Found a digit" << endl;
digitcnt++; //adds to the counter of digit
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Testing for special characters..." << endl;
if(tmpi >= 33 && tmpi <= 47 || tmpi >= 58 && tmpi <= 64 || tmpi >= 91 && tmpi <= 96 || tmpi >= 123 && tmpi <= 126) //checks if there are special characters
{
cout << "Found a special char" << endl;
speccnt++; //adds to the counter of special character
state++;
cout << "Now in state q" << state << "..." << endl;
continue;
}
cout << "Character entered was a lower case" << endl;
state++;
cout << "Now in state q" << state << "..." << endl;
} //end for loop
任何建议或示例都会有所帮助,谢谢。
【问题讨论】:
-
任何输入或方向或链接都会有所帮助
-
你真的应该阅读How to Ask。
-
@dandan78 第一次发布任务,我应该改变什么让它更好?
标签: c++ string switch-statement