【问题标题】:Char to Int - C++字符到整数 - C++
【发布时间】:2015-07-23 20:07:01
【问题描述】:

我知道已经回答了有关它的问题,但我已经阅读了大部分问题,但仍然无法解决我的问题。 我有一个程序可以读取笔记,将它们保存在列表中,并为用户提供删除、更改或选择特定笔记的选项。

我正在使用这个结构:

struct List {
    char title [101];
    char text  [501];
    int cont; //code of the note.
    struct List* next;

};typedef List list;

我卡在选择点上,如果用户输入 *,它必须返回所有注释,如果用户输入数字,它必须只返回相应的注释。

到目前为止,我只有这个:

List* select (List *l, int v) {
   List *p = l;
   for (p = l; p != NULL; p = p -> next){
        if( p -> cont == v){
        cout << "\nTitle: " << p -> title << "\n";
        cout << "Text: " << p -> text <<  "\n";
        cout << "Code: " << p -> cont << "\n" << "\n";
        }
   }

如何读取 char 中的符号并将其转换为 int,以将其与注释的代码进行比较。

对不起,如果我写错了,我是巴西人,我没有写作经验。

编辑: 非常感谢你们,它真的帮了我很多,现在我要完成我的工作了! :D

【问题讨论】:

  • 目前如何从用户那里获取输入?注释的有效代码是否有界限?
  • 使用这个能解决你的问题吗:cplusplus.com/reference/cstdlib/atoi ?
  • 因为我是在程序的基础上,所以我仍然对是否更改输入法有疑问,现在我在int main中使用了一个简单的cin条目,以及代码每个音符由一个数字组成,每添加一个音符就递增,因此它从 0 到 1、2 等开始。
  • 对于 atoi 函数,我已经尝试过,在一个不同的测试程序中,只有一个数字的输入和返回,但是由于我必须将一个符号作为 char 程序返回给我一个错误代码,甚至不是ascii中的代码,我知道这很奇怪,但它发生了
  • @OliviaThebaldiGarcia 你可能不得不改变你的输入法。在读取 char 并希望将其转换为 int 时,您的选择非常有限,因为它与其说是一种转换,不如说是一种比较。您只能比较和提取相应的等价物。

标签: c++ list struct char int


【解决方案1】:

如果你真的只需要charint 类型,你可以试试这个:

char buf[10]; // Assuming that you won't have more than 10 characters in your number
char *endp;
cin >> buf; // This may cause you problems if the input string is more than 9 characters long!

从这里开始,您可以使用 user4581301 的答案:

if ( (buf[0] == '*') && (buf[1] == '\0') )
{
 // Print all your notes here
}

code =  strtol(but, // the string converted to characters
               &endp, // where in the string the number ended. 
               10); // the base of the number, base 10 in this case for       
                    // decimal. 16 if you want to input in hex. 
                    // Unlikely in this case.
if (*endp == '\0') // If the number didn't end didn't end at the end 
                   // of the string, the number is invalid
{
    // print note for code
}

【讨论】:

  • cin.get(buf, sizeof(buf)); 将读取大小限制为缓冲区的长度
  • 还需要将if (buf[0] == "*") 调整为if (buf[0] == '*') 并应测试buf[1] 是否为null 以禁止输入“*12345”等...
  • 相应地更新了我的答案。
【解决方案2】:

试试这个:

以字符串形式获取输入

string str;
cin >> str;

如果字符串是*

if (str == "*")
{
    // print out all notes
}

否则,尝试使用strtol 将字符串转换为数字。 Strtol 提供比 atol 更好的错误检查,因为它会告诉您整个字符串是否已转换,如果没有,您可以去哪里查看。

char * endp; 
long code;

code =  strtol(str.c_str(), // the string converted to characters
               &endp, // where in the string the number ended. 
               10); // the base of the number, base 10 in this case for       
                    // decimal. 16 if you want to input in hex. 
                    // Unlikely in this case.
if (*endp == '\0') // If the number didn't end didn't end at the end 
                   // of the string, the number is invalid
{
    // print note for code
}

关于strtol(str.c_str(), &amp;endp, 10); 和测试endp 的简要说明。

这实际上不起作用。 endp 最终指向一个在您检查时可能无效的内存位置。确实,您需要将 char 数组从字符串中取出并放入可以保证范围的东西中。

上述警告基于旧数据或错误数据是不正确的。谢谢@TamásSzabó。让未来的代码编写更简单一些。

没有std::string版本几乎一样:

char str[64]; // allocate storage for a big number. 
cin.get(str, sizeof(str)); // read up to size of string -1 and null terminate
if ((str[0] == '*') && (str[1] == '\0'))
                   // first character in string is '*' and there 
                   // is no second character
{
    // print out all notes
}
else
{
    char * endp; 
    long code;

    code =  strtol(str, 
                   &endp, 
                   10);  
    if (*endp == '\0')  //this time endp will be valid 
    {
        // print note for code
    }
}

【讨论】:

  • 能否 OP 使用与 if 相同的逻辑来处理案例 *,但也可以将其用于整数?比如,if(str == "1"),等等……?根据允许的音符数量,它可能效率低下。
  • 这就是问题所在,我不能限制音符的数量,所以一个可以有1个或30个音符,怎么知道。
  • user4581301,我试过了,但正如你在编辑中所说,它真的不起作用,甚至不适合我的程序:/
  • @Javia1492 可以,但它会不必要地限制可能的音符数量。
  • @user4581301 为什么endp 在您的代码中最终无效?只有在你测试endp之前,str的内容发生了变化,它才会失效,这在这显然是不可能的。
猜你喜欢
  • 1970-01-01
  • 2015-07-08
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多