C++ While循环中的If和else语句;对于C++;

C++ While循环中的If和else语句;对于C++;,c++,if-statement,while-loop,C++,If Statement,While Loop,关于while循环中的if&else语句,我有一个问题 我想在我的计划中确立一些东西: 希望用户只输入4个字母字符,不使用数字和符号 将这些字母转换为整数(Ascii val.) 为了在混淆时更容易理解 程序1要求输入4个字母的单词。 用户输入。如果输入至少包含: 一个非字母字符,然后程序向用户请求新的输入 如果输入仅包含字母,程序将检查输入中的字母数量 如果输入没有__;: 4个字母,程序要求用户输入新内容 否则,程序将继续确定每个字母的Int值 好的,这是我目前的程序:*不确定

关于while循环中的if&else语句,我有一个问题

我想在我的计划中确立一些东西:

  • 希望用户只输入4个字母字符,不使用数字和符号
  • 将这些字母转换为整数(Ascii val.)
为了在混淆时更容易理解

程序1要求输入4个字母的单词。 用户输入。如果输入至少包含:

  • 一个非字母字符,然后程序向用户请求新的输入
如果输入仅包含字母,程序将检查输入中的字母数量

如果输入没有__;:

  • 4个字母,程序要求用户输入新内容
否则,程序将继续确定每个字母的Int值


好的,这是我目前的程序:*不确定这是否正确

#include <iostream>

using namespace std;

int main() {
    string input;
    int input0 = input[0];  
    int input1 = input[1];  
    int input2 = input[2];
    int input3 = input[3];
    cout << "\nEnter a 4-letter word (Keep it clean!).\n";
  while(cin>>input){
    cout << endl << input << " has " << input.length() << " letters." << endl; 
        if (int(input[0]) > 64 || int(input[0]) < 91 || int(input[0]) > 96 || int(input[0]) < 123 || 
            int(input[1]) > 64 || int(input[1]) < 91 || int(input[1]) > 96 || int(input[1]) < 123 ||
            int(input[2]) > 64 || int(input[2]) < 91 || int(input[2]) > 96 || int(input[2]) < 123 ||
            int(input[3]) > 64 || int(input[3]) < 91 || int(input[3]) > 96 || int(input[3]) < 123) {
                if (input.length()!=5 && input.length()>3)
                    cout << "\n the int value of the " << input[0] << " is " << int(input[0]) << endl;
                    cout << "\n the int value of the " << input[1] << " is " << int(input[1]) << endl;
                    cout << "\n the int value of the " << input[2] << " is " << int(input[2]) << endl;
                    cout << "\n the int value of the " << input[3] << " is " << int(input[3]) << endl;
                else cout << input << "is not a 4-letter word.\nPlease try again." << endl;
        }
        else cout << input << " contains number(s) and or symbol(s).\nPlease try again." << endl;
  }
}
#包括
使用名称空间std;
int main(){
字符串输入;
int input0=输入[0];
int input1=输入[1];
int input2=输入[2];
int input3=输入[3];
cout>输入){
cout 96|int(输入[2])<123||
int(输入[3])>64|int(输入[3])<91|int(输入[3])>96|int(输入[3])<123{
如果(input.length()!=5&&input.length()>3)

cout应该归咎于这些相当粗糙的声明:

if (input.length()!=5 && input.length()>3)
    cout << "\n the int value of the " << input[0] << " is " << int(input[0]) << endl;
// Not part of if-statement:
    cout << "\n the int value of the " << input[1] << " is " << int(input[1]) << endl;
    cout << "\n the int value of the " << input[2] << " is " << int(input[2]) << endl;
    cout << "\n the int value of the " << input[3] << " is " << int(input[3]) << endl;
else cout << input << "is not a 4-letter word.\nPlease try again." << endl;
您应该在确保输入长度正确后进行测试,否则您将有未定义的行为。因此,让我们这样做:

while( cin>>input )
{
    cout << endl << input << " has " << input.length() << " letters." << endl; 
    if( input.length() != 4 )
    {
        cout << input << "is not a 4-letter word.\nPlease try again." << endl;
    }
    else if( any_of( input.begin(), input.end(), [](char c){ return !isalpha(c); } ) )
    {
        cout << input << " contains number(s) and or symbol(s).\nPlease try again." << endl;
    }
    else
    {
        // I assume you want to exit the loop here.
        break;
    }
}

谢谢你的提示Nicky!它成功地运行了程序,但我得到了一个奇怪的结果:我在被问到的4个字符中加了一个数字[5tar]当它应该要求用户再试时,它仍然给出int值,包括数字int值。这是因为你的逻辑是心智的,完全逻辑的,或者总是会导致<代码>真的< /代码>。请看我的答案,用<代码> ISALPHA/<代码>更好的选择。考虑这个:什么数字大于64,或者是LE。当然,答案是:所有的数字。
while( cin>>input )
{
    cout << endl << input << " has " << input.length() << " letters." << endl; 
    if( input.length() != 4 )
    {
        cout << input << "is not a 4-letter word.\nPlease try again." << endl;
    }
    else if( any_of( input.begin(), input.end(), [](char c){ return !isalpha(c); } ) )
    {
        cout << input << " contains number(s) and or symbol(s).\nPlease try again." << endl;
    }
    else
    {
        // I assume you want to exit the loop here.
        break;
    }
}
if( any_of( input.begin(), input.end(), [](char c){ return !isalpha(c); } ) ) ...