[英]c++ Exception thrown at 0x7A45FF80 (ucrtbased.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000000
i have an error when i run this code and I don't know what it is, I get an error in string in visual studio app运行此代码时出现错误,但我不知道它是什么,我在 Visual Studio 应用程序中收到字符串错误
the error " Exception thrown at 0x7A45FF80 (ucrtbased.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000000."错误“在 ConsoleApplication1.exe 中的 0x7A45FF80 (ucrtbased.dll) 处抛出异常:0xC0000005:访问冲突读取位置 0x00000000。”
#include <iostream>
using namespace std;
#include <string>
int main()
{
float userinput01 = 0, userinput02 = 0;
float avareg = 0;
string option = 0;
cout << "Welcome to Calculator app!\n" << "What is the desired operation?\n\nChoose the operation letter:\n" << endl;
cout << "a- Additon\n" << "b- Subtraction\n" << "c- Multiplicstion\n" << "d- Division\n" << endl;
if (option == "a") {
cout << "Additon" << endl;
cin >> userinput01 >> userinput02;
avareg = userinput01 + userinput02;
cout << userinput01 << "+" << userinput02 << "= " << avareg << endl;
}
else if (option == "b") {
cout << "Subtraction" << endl;
cin >> userinput01 >> userinput02;
avareg = userinput01 - userinput02;
cout << userinput01 << "-" << userinput02 << "= " << avareg << endl;
}
else if (option == "c") {
cout << "Multiplicstion" << endl;
cin >> userinput01 >> userinput02;
avareg = userinput01 * userinput02;
cout << userinput01 << "*" << userinput02 << "= " << avareg << endl;
}
else if (option == "d") {
cout << "Division" << endl;
cin >> userinput01 >> userinput02;
avareg = userinput01 / userinput02;
cout << userinput01 << "/" << userinput02 << "= " << avareg << endl;
}
else
cout << "You did not choose a correct letter" << endl;
return 0;
}
string option = 0;
is the culprit and string option;
是罪魁祸首和
string option;
is the fix.是修复。
The latter relies on the std::string
default constructor.后者依赖于
std::string
默认构造函数。
The former will cause the constructor to a const char*
to be called, with undefined results.前者将导致构造
const char*
调用const char*
,结果未定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.