Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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> #include <vector> #include <string> using namespace std; int main() { int variable; char userInput; variable = 0; cout << "To increment variable one, press a \n"; do { variable = variable++; cout << "variable++ \n" ; } while (userInput = "a"); } #包括 #包括 #包括 使用名称空间std; int main() { int变量; 字符用户输入; 变量=0; 在while循环中,您需要一个_C++_Loops_User Input - Fatal编程技术网

简单C+;中的用户输入+;反程序 我试图在C++中做一个简单的计数器程序,它将增加一个变量,并输入用户输入(通过按下某个键指定)。以下是我的想法: #include <iostream> #include <vector> #include <string> using namespace std; int main() { int variable; char userInput; variable = 0; cout << "To increment variable one, press a \n"; do { variable = variable++; cout << "variable++ \n" ; } while (userInput = "a"); } #包括 #包括 #包括 使用名称空间std; int main() { int变量; 字符用户输入; 变量=0; 在while循环中,您需要一个

简单C+;中的用户输入+;反程序 我试图在C++中做一个简单的计数器程序,它将增加一个变量,并输入用户输入(通过按下某个键指定)。以下是我的想法: #include <iostream> #include <vector> #include <string> using namespace std; int main() { int variable; char userInput; variable = 0; cout << "To increment variable one, press a \n"; do { variable = variable++; cout << "variable++ \n" ; } while (userInput = "a"); } #包括 #包括 #包括 使用名称空间std; int main() { int变量; 字符用户输入; 变量=0; 在while循环中,您需要一个,c++,loops,user-input,C++,Loops,User Input,此外,以下行 variable = variable++; 将产生。阅读更多关于更好地理解 尝试: 最后在while循环中断条件下,代替赋值操作 while (userInput = "a"); 使用比较,例如: while (userInput == "a"); 你需要使用 std::cin >> userInput; // each time you go through the `do` loop. 另外,您只需要使用 variable++; // on the lin

此外,以下行

variable = variable++;
将产生。阅读更多关于更好地理解

尝试:

最后在while循环中断条件下,代替赋值操作

while (userInput = "a");
使用比较,例如:

while (userInput == "a");
你需要使用

std::cin >> userInput; // each time you go through the `do` loop.
另外,您只需要使用

variable++; // on the line.
另外!使用
cout在循环中添加cin:

    cin>>userInput;
改变

variable = variable++;

接下来,您的while条件应如下所示:

 while (userInput=='a');
#include <iostream>

using namespace std;

int main()

{
int variable;
char userInput;
variable = 0;
cout << "To increment variable one, press a \n";
do
{
    cin>>userInput;
    variable++;
    cout << "variable++ \n" ;
} while (userInput=='a');
return 0;
}
因此,您的总体计划如下所示:

 while (userInput=='a');
#include <iostream>

using namespace std;

int main()

{
int variable;
char userInput;
variable = 0;
cout << "To increment variable one, press a \n";
do
{
    cin>>userInput;
    variable++;
    cout << "variable++ \n" ;
} while (userInput=='a');
return 0;
}
#包括
使用名称空间std;
int main()
{
int变量;
字符用户输入;
变量=0;
cout>userInput;
变量++;

cout并不能真正解决“无效转换”OP之所以出现,是因为while语句中的赋值而不是比较。@AlgirdasPreidžius,ty,意外地错过了它。我试过了-它不起作用。另外,首先,当您只有简单的
char
s时,为什么要使用为
char*
设计的函数?我不需要尝试它就知道它是正确的不起作用。
(const char)
(const char*)
是另一个很大的区别,即使是“正确的”强制转换代码仍然是错误的。不要使用C样式强制转换。使用
静态强制转换
动态强制转换
。如果这些都不起作用,90%的时间你的代码出现错误。你是向我保证它有效的人,我不会为你修复它。
 while (userInput=='a');
#include <iostream>

using namespace std;

int main()

{
int variable;
char userInput;
variable = 0;
cout << "To increment variable one, press a \n";
do
{
    cin>>userInput;
    variable++;
    cout << "variable++ \n" ;
} while (userInput=='a');
return 0;
}