字符到整数 - C++
Char to Int - C++
我知道已经有关于它的问题的解答,但我已经阅读了大部分,但仍然无法解决我的问题。
我有一个程序可以读取笔记,将它们保存在列表中并为用户提供删除、更改或 select 特定笔记的选项。
我正在使用这个结构:
struct List {
char title [101];
char text [501];
int cont; //code of the note.
struct List* next;
};typedef List list;
我卡在了 selection 点,如果用户输入 * 它必须 return 所有注释,如果用户输入数字它必须 return只有相应的注释。
到目前为止我只有这个:
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,以便将其与注释的代码进行比较。
对不起,如果我写错了,我是巴西人,我有 none 写作练习。
编辑:
非常感谢你们,它真的帮了我很多,现在我要完成我的工作了! :D
试试这个:
获取字符串形式的输入
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 == '[=12=]') // 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(), &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] == '[=13=]'))
// 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 == '[=13=]') //this time endp will be valid
{
// print note for code
}
}
如果你真的只需要 char
和 int
类型,你可以试试这个:
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] == '[=11=]') )
{
// 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 == '[=11=]') // If the number didn't end didn't end at the end
// of the string, the number is invalid
{
// print note for code
}
我知道已经有关于它的问题的解答,但我已经阅读了大部分,但仍然无法解决我的问题。 我有一个程序可以读取笔记,将它们保存在列表中并为用户提供删除、更改或 select 特定笔记的选项。
我正在使用这个结构:
struct List {
char title [101];
char text [501];
int cont; //code of the note.
struct List* next;
};typedef List list;
我卡在了 selection 点,如果用户输入 * 它必须 return 所有注释,如果用户输入数字它必须 return只有相应的注释。
到目前为止我只有这个:
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,以便将其与注释的代码进行比较。
对不起,如果我写错了,我是巴西人,我有 none 写作练习。
编辑: 非常感谢你们,它真的帮了我很多,现在我要完成我的工作了! :D
试试这个:
获取字符串形式的输入
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 == '[=12=]') // 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(), &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] == '[=13=]'))
// 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 == '[=13=]') //this time endp will be valid
{
// print note for code
}
}
如果你真的只需要 char
和 int
类型,你可以试试这个:
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] == '[=11=]') )
{
// 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 == '[=11=]') // If the number didn't end didn't end at the end
// of the string, the number is invalid
{
// print note for code
}