If和else循环中的语句“for C ++

时间:2016-02-23 05:21:04

标签: c++ if-statement while-loop

我有一个关于if&的问题while循环中的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;
  }
}

我有2个错误:

  •   

    错误:在'else'之前预期'}'

  •   

    错误:'else'没有先前的'if'

1 个答案:

答案 0 :(得分:0)

这一套相当粗糙的陈述应该归咎于:

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;

您需要将大括号coutelse中的所有{语句(直至})括起来。

我想对你正在进行的角色测试提出一个观点。看起来你正在检查字符是否是字母,但是使用的是最不易读,最便携的方法。请改用std::isalpha。事实上,你可以用这个来完成整个事情:

if( std::all_of( input.begin(), input.begin() + 4, [](char c){ return (bool)isalpha(c); } ) )
{
    //...
}

您应该在之后进行测试,确保输入的长度正确,否则您将有未定义的行为。让我们这样做:

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;
    }
}

注意我将all_of语句翻转为反向(“任何字符不是字母”),以提高可读性,因为lambda不再需要使用强制转换来进行自动类型推导:

if( any_of( input.begin(), input.end(), [](char c){ return !isalpha(c); } ) ) ...