Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++用户。 我的代码如下: #include <iostream> using namespace std; int main() { int option = 1; char abstract='a'; while(option == 1){ char temp; cin>> temp; abstract = temp; cout << abstract; option = 1; if(abstract == '!'){ option = 0; } } return 0; } #包括 使用名称空间std; int main() { int选项=1; char abstract='a'; while(选项==1){ 焦炭温度; cin>>温度; 抽象=温度; cout_C++ - Fatal编程技术网

为什么在一个字符变量中键入多个字符时它会起作用? 我是一个新的C++用户。 我的代码如下: #include <iostream> using namespace std; int main() { int option = 1; char abstract='a'; while(option == 1){ char temp; cin>> temp; abstract = temp; cout << abstract; option = 1; if(abstract == '!'){ option = 0; } } return 0; } #包括 使用名称空间std; int main() { int选项=1; char abstract='a'; while(选项==1){ 焦炭温度; cin>>温度; 抽象=温度; cout

为什么在一个字符变量中键入多个字符时它会起作用? 我是一个新的C++用户。 我的代码如下: #include <iostream> using namespace std; int main() { int option = 1; char abstract='a'; while(option == 1){ char temp; cin>> temp; abstract = temp; cout << abstract; option = 1; if(abstract == '!'){ option = 0; } } return 0; } #包括 使用名称空间std; int main() { int选项=1; char abstract='a'; while(选项==1){ 焦炭温度; cin>>温度; 抽象=温度; cout,c++,C++,,因为while循环依次处理每个字符。不确定您预期会发生什么 使用分隔符打印出来,以确保每次迭代打印的字符不会超过一个: cout << "'" << abstract << "'"; CUT> P>终端窗口本身负责读取字符并将其回传到屏幕上。C++程序要求终端显示字符,而在这种程序中,至少对字符的显示没有影响。 实际上,每次在一个字符中只存储一个字符,在 char < /代码>。cin>>temp;一次读取一个字符,因为不能容纳更多的字符。循环只读取并打

,因为while循环依次处理每个字符。不确定您预期会发生什么

使用分隔符打印出来,以确保每次迭代打印的字符不会超过一个:

cout << "'" << abstract << "'";

<代码> CUT> P>终端窗口本身负责读取字符并将其回传到屏幕上。C++程序要求终端显示字符,而在这种程序中,至少对字符的显示没有影响。

实际上,每次在一个字符中只存储一个字符,在<代码> char < /代码>。<代码>cin>>temp;一次读取一个字符,因为不能容纳更多的字符。循环只读取并打印一个字符接一个字符


作为一个可视化提示,尝试用<代码> CUT> P>来回响你的字符,你的终端不限制键入的字符数,这就是为什么你可以按你想要的类型输入的原因。你的C++编译器只读取其中一个字符,因为“TEMP”是char类型的。你可以输入一个“IF”语句来检查键入的字符数。终端

而不是使用整数作为标志,考虑使用<代码> BOOL ,或更多的惯用的直接流控制结构,如<代码>中断< /代码>。