将字符串数组作为输入时出错
Error while taking array of strings as input
我正在尝试将字符串数组作为输入,但它给出 运行 时间错误
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
string str[500];
int n;
cin>>n;
for(int i=0;i<n;i++)
scanf("%s",&str[i]); \works fine with cin but gives error using scanf
return 0;
}
Process terminated with status -1073741510 (0 minute(s), 10 second(s))
works fine with cin
but give error if i use scanf
谁能告诉我为什么会出现这样的错误
what does that error mean
and why doesn't it gives error while using cin
我正在使用代码块 13.12
string
是 C++ 类型,您应该使用 C++ I/Os 因此 cin
和 cout
。如果你真的想使用 C I/Os 你可以访问 c_str
方法来获取 string
.
后面的 C char *
scanf()
用于 C 风格的字符串(又名 char *
或 char[]
),它应该是
char myString[101];
char* myOtherString = (char*)malloc(101);
scanf("%100s", myString);
scanf("%100s", myOtherString);
free(myOtherString);
没有理由在 std::string
之后使用它。
您正在使用 C++ 来利用 cin
头文件避免使用非空指针和char数组
cin>> 具有运算符重载并使用现代字符串 class
我正在尝试将字符串数组作为输入,但它给出 运行 时间错误
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
string str[500];
int n;
cin>>n;
for(int i=0;i<n;i++)
scanf("%s",&str[i]); \works fine with cin but gives error using scanf
return 0;
}
Process terminated with status -1073741510 (0 minute(s), 10 second(s))
works fine with
cin
but give error if i usescanf
谁能告诉我为什么会出现这样的错误
what does that error mean and why doesn't it gives error while using
cin
我正在使用代码块 13.12
string
是 C++ 类型,您应该使用 C++ I/Os 因此 cin
和 cout
。如果你真的想使用 C I/Os 你可以访问 c_str
方法来获取 string
.
char *
scanf()
用于 C 风格的字符串(又名 char *
或 char[]
),它应该是
char myString[101];
char* myOtherString = (char*)malloc(101);
scanf("%100s", myString);
scanf("%100s", myOtherString);
free(myOtherString);
没有理由在 std::string
之后使用它。
您正在使用 C++ 来利用 cin 头文件避免使用非空指针和char数组
cin>> 具有运算符重载并使用现代字符串 class