Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++输入字符串崩溃 我是C++的新手,还在学习,我在书上做了一个练习,我的程序中的每一项都是正确的,但是当输入我输入的输入时,它会崩溃。我试着在不同的编译器和计算机上运行它,结果是一样的。希望有人能帮助我。谢谢。 #include<iostream> using namespace std; int main() { string name; char code[1]; float price; int quantity; cout<<"Enter an item name : "<<endl; getline(cin,name); cout<<"Enter the item code : "<<endl; cin>>code; cout<<"Enter the price per unit : "<<endl; cin>>price; cout<<"Enter the quanlity : "<<endl; cin>>quantity; cout<<"You entered the item name : "<<name<<endl <<"You entered the item code : "<<code<<endl <<"You entered the item price : "<<"RM"<<price<<endl <<"You entered the item quantity: "<<quantity<<" unit"<<endl <<"The total cost : "<<"RM"<<(quantity*price)<<endl; return 0; }_C++ - Fatal编程技术网

C++输入字符串崩溃 我是C++的新手,还在学习,我在书上做了一个练习,我的程序中的每一项都是正确的,但是当输入我输入的输入时,它会崩溃。我试着在不同的编译器和计算机上运行它,结果是一样的。希望有人能帮助我。谢谢。 #include<iostream> using namespace std; int main() { string name; char code[1]; float price; int quantity; cout<<"Enter an item name : "<<endl; getline(cin,name); cout<<"Enter the item code : "<<endl; cin>>code; cout<<"Enter the price per unit : "<<endl; cin>>price; cout<<"Enter the quanlity : "<<endl; cin>>quantity; cout<<"You entered the item name : "<<name<<endl <<"You entered the item code : "<<code<<endl <<"You entered the item price : "<<"RM"<<price<<endl <<"You entered the item quantity: "<<quantity<<" unit"<<endl <<"The total cost : "<<"RM"<<(quantity*price)<<endl; return 0; }

C++输入字符串崩溃 我是C++的新手,还在学习,我在书上做了一个练习,我的程序中的每一项都是正确的,但是当输入我输入的输入时,它会崩溃。我试着在不同的编译器和计算机上运行它,结果是一样的。希望有人能帮助我。谢谢。 #include<iostream> using namespace std; int main() { string name; char code[1]; float price; int quantity; cout<<"Enter an item name : "<<endl; getline(cin,name); cout<<"Enter the item code : "<<endl; cin>>code; cout<<"Enter the price per unit : "<<endl; cin>>price; cout<<"Enter the quanlity : "<<endl; cin>>quantity; cout<<"You entered the item name : "<<name<<endl <<"You entered the item code : "<<code<<endl <<"You entered the item price : "<<"RM"<<price<<endl <<"You entered the item quantity: "<<quantity<<" unit"<<endl <<"The total cost : "<<"RM"<<(quantity*price)<<endl; return 0; },c++,C++,这是输出: 字符码[1];数组被视为指针,它们在传递给函数时会衰减。有关阵列衰减的更多信息,请参见: cin>>代码;>>实际上是一个函数,所以代码被视为指向char的指针。它将指向char的指针视为一个c样式的字符串,并尝试使用null终止它。遗憾的是,只有一个字符的空间,没有空终止符的空间,因此空终止符被写入程序不拥有的内存中 如果该程序仍然有效,则您输入了项目代码:将代码[1]更改为代码?我将在单个字符中更改字符[1],否则std::cin将在[1]处插入一个\0,生成一个UB。解决此类问

这是输出:

字符码[1];数组被视为指针,它们在传递给函数时会衰减。有关阵列衰减的更多信息,请参见:

cin>>代码;>>实际上是一个函数,所以代码被视为指向char的指针。它将指向char的指针视为一个c样式的字符串,并尝试使用null终止它。遗憾的是,只有一个字符的空间,没有空终止符的空间,因此空终止符被写入程序不拥有的内存中


如果该程序仍然有效,则您输入了项目代码:将代码[1]更改为代码?我将在单个字符中更改字符[1],否则std::cin将在[1]处插入一个\0,生成一个UB。解决此类问题的正确工具是您的调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。@appleapple Yes,它起作用了。但是如何指定只使用一个字符。如果使用字符代码,并且键位于多个字符中,输出将不正确。@FTTEH忽略其他项是一个选项
char code;