【问题标题】:Check entered value's first character, if one of the conditions false, ask input again检查输入值的第一个字符,如果其中一个条件为假,请重新输入
【发布时间】:2015-12-19 19:49:48
【问题描述】:

使用c++;

我需要从用户那里获取输入,然后检查条件(见下文),如果其中一个条件为假,则再次要求用户输入(循环),仅使用 iostream 标头。

条件如下:

  1. 输入必须是字母(输入不能是数字、单词、符号)
  2. 输入必须是“h”或“p”或“u”字母(输入不能是其他字母和单词,如“ux”、“xu”、“uu”、“hup”)

我尝试了很多,但我失败了

#include <iostream>

using namespace std;

int main(){

    char type;
    cout<<"enter type: ";
    cin>>type;

        while(cin.fail()==1){
        cout<<"error! invalid type. try again. \ntype:";
        cin.clear();
        cin.ignore();
        cin>>type;
    }

while(cin.fail()==0){
      switch (type) {
        case 'h':
        case 'u':
        case 'p':
                break;
            default:
                cout<<"error!invalid type. try again.\ntype: ";
                cin>>type;
        }
    }

    cout<<"valid type";

    return 0;

}

【问题讨论】:

  • 你能粘贴一些你现在编码的代码吗?
  • 您尝试了什么,又是如何失败的?
  • 您应该发布您已经尝试过的代码、错误以及您期望发生的情况。你做的最后一个。作为提示,您可以尝试使用cin &gt;&gt; c(c 是char)。如果你想逐个字符解析。或者您可以使用getline(cin, str) 获取该人输入的整行并解析该字符串。
  • 我尝试了很多不同的代码(使用 while、for 循环)。现在我发布了我尝试的最后一个
  • 因此,如果其中一个条件为假,程序将要求用户提供另一个输入,然后程序必须再次检查新输入是否符合这两个条件之一?

标签: c++ loops input char iostream


【解决方案1】:

假设您的“types”是单独的空格分隔字符,最简单的方法似乎是读取一个字符串,验证它只有一个字符长,并且该字符具有适当的值,例如:

std::string tmp;
while (std::cin >> tmp
       && (tmp.size() != 1
           || (tmp[0] != 'h' && tmp[0] != 'u' && tmp[0] != 'p'))) {
    std::cout << "ERROR: invalid type try again\n";
}
if (!std::cin) {
    std::cout << "ERROR: reached the end of the stream before reading a valid type\n";
}
else {
    char type = tmp[0];
    // now do something...
}

根据您的需要,您可能想要阅读一整行(使用std::getline())而不是一个单词。

【讨论】:

  • 感谢它的工作。有没有其他方法可以仅使用 iostream 标头(没有流和 sstream 标头)
  • @sinyorcem:当然。您可以只阅读一个char 而不是阅读std::string,然后检查下一个字符是否为非空格。如果是这样,您需要在跳过所有非空格字符后继续阅读,直到找到空格(然后在阅读char时跳过空格)。
【解决方案2】:

要解决此问题,您可以使用字符数组。例如,对于最多 100 个字符的输入,您可以像这样定义类型变量:

char type[100];

此数组将包含字符的 ASCII 代码,如果您使用转义序列,则在最后一个字母之后您的元素将为 null'\0'。如果它的大小为 1 并且第一个字符是字母,则您的输入将是一个字母。所以声明这些简单的函数你的代码会更清晰:

bool isOneCharacter(char* str)
{
    //check if the first character exists and the second one not
    return ((str[0]!='\0')&&(str[1]=='\0'));
}

bool isLetter(char ch)
{
    //check if a character is between ['a'-'z'] or ['A'-'Z']
    return (((ch>='a') && (ch<='z'))|| ((ch >= 'A') && (ch <= 'Z')));
}

bool isHUP(char ch)
{
    return ((ch=='h')||(ch=='u')||(ch == 'p'));
}

了解这些内容,您的主要功能将很容易编写:

int main() {

    char type[100]; //input
    bool isOk; //it will be true only if the input fits the conditions

    do 
    {
        isOk = false; //initial value
        cout << "Type = ";
        cin >> type; 
        //note that the first space will be treat as the end of your input
        if (isOneCharacter(type)) //if type is a single character
        {
            //type contains a simple character at this point
            char charType = type[0];
            //if the input contains a single letter that is 'h', 'u' or 'p'
            if (isLetter(charType) && (isHUP(charType)))
            {
                isOk = true;
            }
        }

        if (!isOk)
        {
            //error message for the user
            cout << "Try again!" << endl;
        }
    } while (!isOk);
}

在这种具体情况下,您不一定需要检查输入是否是字母,因为如果它的值是 'h' 'u' 或 'p' 则自动意味着它是一个字母。如果你的输入是一个字母,而不是 'h' 'u' 或 'p',上面的程序会询问用户一个新的输入,而不检查它是否是一个字母。但是如果你需要通过检查来解决这个问题,我已经为你写了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2016-12-12
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多