1

下面我有一个代码,我想在其中输入某些值。如果我 cin a [ 它应该忽略它。但是如果用户插入数字而不是 a[它应该将 char 转换为 int 并将其插入到rowselse 函数中。如何将此 char 转换为 int 并将其放入rows

    char ignoreChar;

    is >> ignoreChar;

    if(ignoreChar == '[')
    {
    }

    else
    {
    rows = ignoreChar;
    }
4

5 回答 5

1

如果您适当注意ignoreChar将包含一个数字,您可以通过这种方式轻松地将其转换为整数:

rows = ignoreChar - '0';

std::isdigit()函数可用于检查某个字符是否代表一个数字。

您可以将以下示例程序作为灵感:

#include <iostream>

using namespace std;

int main()
{
    int i = -1; // Equivalent of your "rows"

    char c; // Equivalent of your "ignoreChar"
    cin >> c;

    if (std::isdigit(c))
    {
        i = c - '0';
    }
    else
    {
        // Whatever...
    }

    cout << i;
}
于 2013-02-12T16:13:09.963 回答
0

很抱歉代码比可能长得多。下面是“阶乘计数”的示例代码,其中包含输入符号及其处理。我试图展示不同的可能性。我现在看到的唯一问题是,如果用户在没有输入任何符号的情况下按 Enter 键,则没有任何反应。这是我在这里的第一篇文章,所以对于任何可能的字体、格式和其他废话,我感到非常抱歉。MaxNum = 21 是根据经验为 unsigned long long int 返回类型定义的。

我检查过这样的输入案例:abcde 2. -7 22 21

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>

unsigned long long int CalcFact( int p ) 
    { return p == 0 || p == 1 ? 1 : p*CalcFact(p-1); }

int main()
{
int MaxNum = 21, HowMany = 0, MaxAttempts = 5, Attempts = 0;
char AnyKey[] = "", *StrToOut = "The factorial of ", Num[3] = "";
bool IsOK = 0;

while( !IsOK && Attempts++ < MaxAttempts )
{
    std::cout << "Please enter an integer > 0 and <= 21: ";
    std::cin.width(sizeof(Num)/sizeof(char)); 
    std::cin >> Num;
    for( int Count = 0; Count < strlen(Num); Count++ )
        IsOK = isdigit(Num[Count]) ? true : false;
    if( IsOK )
    {
        if( !(atoi(Num) >= 0 && atoi(Num) <= MaxNum) )
            IsOK = false;
        else
        {
            for( int Count = 0; Count <= MaxNum; Count++ )
                {
                std::cout << StrToOut << Count << std::setfill('.') 
                << std::setw( Count < 10 ? 30 : 29 ) 
                << CalcFact(Count) << std::endl; 
                }
        }
    }
    if( !IsOK && Attempts > 0 )
    {
        std::cin.clear(); 
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}
if( !IsOK ) 
    return -1;
else
    std::cin >> AnyKey;
return 0;
}
于 2013-02-13T13:31:07.303 回答
0

如果他们只输入一个字符,您应该能够键入 cast 它。

int a = (int)whateverNameOfChar;

这会将它转换为它的 ASCII 数字,所以你必须减去 '0'

于 2013-02-12T16:09:42.583 回答
0

要将char您知道是其中之一的a'0'转换为该数值'9'的 an ,您可以简单地执行以下操作:int

rows = ignoreChar - '0';

这是有效的,因为标准保证数字字符具有连续值。

如果您需要确定 是否char是数字,您可以使用std::isdigit(ignoreChar)或 的简单条件ignoreChar >= '0' && ignoreChar <= '9'

于 2013-02-12T16:12:35.190 回答
0

“读取用户输入”有几种不同的方法。

哪一个效果最好,取决于输入的格式/风格。您可以考虑两种不同的格式,“Linebased”和“free form”。自由形式将像 C 源代码一样输入,您可以在其中添加换行符、空格等,只要您喜欢。

Linebased 具有一套格式,其中每一行包含一组给定的输入[不一定是相同数量的输入,但行尾终止该特定输入]。

在自由格式输入中,您必须阅读一个字符,然后确定“这个特定部分的含义是什么”,然后决定要做什么。有时你还需要使用该peek()功能来检查下一个字符是什么,然后根据它来确定要做什么。

在基于行的输入中,您getline()可以读取一行文本,然后将其拆分为您需要的任何格式,例如使用stringstream.

接下来,您必须决定是编写自己的代码(或使用一些基本的标准翻译代码)还是使用stream函数来解析例如数字。编写更多代码将为您提供更好的方法来处理错误,例如“1234aqj”不是数字,而是stream在到达该字符串中的“a”时停止读取。

于 2013-02-12T16:14:11.307 回答