错误 "s1, s2 are used uninitialised in this function"
С error "s1, s2 are used uninitialised in this function"
这是我在第一节编程基础课后遇到的问题之一
gets() 工作正常(我认为),但 "cin" 拒绝工作
int main(void)
{
char *s1, *s2;
puts("Enter your name and surname for gets()");
gets(s1);
puts("Enter your name and surname for cin()");
cin >> s2;
cout << s1 << "! Hello from gets" << endl;
cout << s2 << "! Hello from cin" << endl;
return 0;
}
我希望 cin 输出你在控制台中输入的内容,但在输入 programm 后等待一秒钟,然后一切都关闭,根本没有任何输出。
截图是我们老师给我们的,它不起作用
Screenshot
这些指针
char *s1, *s2;
具有自动存储持续时间的未初始化且具有不确定的值。结果,该程序具有未定义的行为。
改用 std::string
.
类型的字符数组或对象
请注意,C 标准不支持函数 gets
。使用 fgets
而不是 gets
.
或者因为这是一个 C++ 程序然后使用 std::getline
或成员函数 std::cin.getline
.
注意这条语句
cin >> s2;
不允许输入多个由空格分隔的单词。
这是一个演示程序。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
int main()
{
const size_t N = 100;
char s1[N];
std::string s2;
std::ios_base::sync_with_stdio();
std::printf( "Enter your name and surname for fgets(): " );
std::fgets( s1, sizeof( s1 ), stdin );
s1[std::strcspn( s1, "\n" )] = '[=12=]';
std::printf( "Enter your name and surname for std::cin: " );
std::getline( std::cin, s2 );
std::cout << s1 << "! Hello from fgets" << std::endl;
std::cout << s2 << "! Hello from std::cin" << std::endl;
return 0;
}
它的输出可能看起来像
Enter your name and surname for fgets(): Bob Fisher
Enter your name and surname for std::cin: Tomas Man
Bob Fisher! Hello from fgets
Tomas Man! Hello from std::cin
您的程序有未定义的行为。
gets
需要一个指向足够有效内存以读取和存储输入的参数。在您发布的代码中,s1
不符合该要求。您对 cin
和 s2
.
的使用也存在类似问题
更重要的是,不要再使用 gets
。由于安全问题,这是一个已弃用的功能。使用 std::string
和 std::getline
.
int main(void)
{
std::string s1;
std::string s2;
puts("Enter your name and surname");
std::getline(std::cin, s1);
puts("Enter your name and surname again");
std::getline(std::cin, s2);
// Use s1 and s2.
return 0;
}
有用的阅读:Which functions from the standard library must (should) be avoided?
此外,请记住 declaring/creating 指针变量不会自动创建指向的对象。您必须始终明确地这样做。
char* s = new char[ N ];
...无论 N
是什么值。在您希望使用它之后,请不要忘记在某个时候释放内存。
delete [] s;
就是说,由于您使用的是 C++,通常可以避免直接处理指针,如果必须,请使用 std::unique_ptr
或 std::shared_ptr
这是我在第一节编程基础课后遇到的问题之一
gets() 工作正常(我认为),但 "cin" 拒绝工作
int main(void)
{
char *s1, *s2;
puts("Enter your name and surname for gets()");
gets(s1);
puts("Enter your name and surname for cin()");
cin >> s2;
cout << s1 << "! Hello from gets" << endl;
cout << s2 << "! Hello from cin" << endl;
return 0;
}
我希望 cin 输出你在控制台中输入的内容,但在输入 programm 后等待一秒钟,然后一切都关闭,根本没有任何输出。
截图是我们老师给我们的,它不起作用
Screenshot
这些指针
char *s1, *s2;
具有自动存储持续时间的未初始化且具有不确定的值。结果,该程序具有未定义的行为。
改用 std::string
.
请注意,C 标准不支持函数 gets
。使用 fgets
而不是 gets
.
或者因为这是一个 C++ 程序然后使用 std::getline
或成员函数 std::cin.getline
.
注意这条语句
cin >> s2;
不允许输入多个由空格分隔的单词。
这是一个演示程序。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
int main()
{
const size_t N = 100;
char s1[N];
std::string s2;
std::ios_base::sync_with_stdio();
std::printf( "Enter your name and surname for fgets(): " );
std::fgets( s1, sizeof( s1 ), stdin );
s1[std::strcspn( s1, "\n" )] = '[=12=]';
std::printf( "Enter your name and surname for std::cin: " );
std::getline( std::cin, s2 );
std::cout << s1 << "! Hello from fgets" << std::endl;
std::cout << s2 << "! Hello from std::cin" << std::endl;
return 0;
}
它的输出可能看起来像
Enter your name and surname for fgets(): Bob Fisher
Enter your name and surname for std::cin: Tomas Man
Bob Fisher! Hello from fgets
Tomas Man! Hello from std::cin
您的程序有未定义的行为。
gets
需要一个指向足够有效内存以读取和存储输入的参数。在您发布的代码中,s1
不符合该要求。您对 cin
和 s2
.
更重要的是,不要再使用 gets
。由于安全问题,这是一个已弃用的功能。使用 std::string
和 std::getline
.
int main(void)
{
std::string s1;
std::string s2;
puts("Enter your name and surname");
std::getline(std::cin, s1);
puts("Enter your name and surname again");
std::getline(std::cin, s2);
// Use s1 and s2.
return 0;
}
有用的阅读:Which functions from the standard library must (should) be avoided?
此外,请记住 declaring/creating 指针变量不会自动创建指向的对象。您必须始终明确地这样做。
char* s = new char[ N ];
...无论 N
是什么值。在您希望使用它之后,请不要忘记在某个时候释放内存。
delete [] s;
就是说,由于您使用的是 C++,通常可以避免直接处理指针,如果必须,请使用 std::unique_ptr
或 std::shared_ptr