C++ 代码不会退出while循环
尝试创建我的第一个程序预期行为:C++ 代码不会退出while循环,c++,C++,尝试创建我的第一个程序预期行为: 1. User inputs an integer (lets say 7) 2. User continues to input same integer more times (lets say 4 total) 3. User inputs 8 4. Program then prints ("the count of 7 was 4") 5. User continues to enter 8 (lets say a 8 was entered a
1. User inputs an integer (lets say 7)
2. User continues to input same integer more times (lets say 4 total)
3. User inputs 8
4. Program then prints ("the count of 7 was 4")
5. User continues to enter 8 (lets say a 8 was entered a total of 11 times)
6. User enters 73
7. Program then prints ("the count of 8 was 11")
第4步后,程序卡住,任何输入yeild“7被输入0次”
#包括
int main(){
int inputedvalue=0;
int firstvalue=0;
int-cnt=0;
如果(标准::cin>>第一个值){
++碳纳米管;
而(标准::cin>>输入值){
如果(InputedValue==firstvalue)
++碳纳米管;
否则{
std::cout我正在查看您的示例,我相信下面的内容可能会让您回到正确的方向
using namespace std;
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
cout << "Enter Starting value."<< std::endl;
if (cin >> firstvalue){
cout << "New count " << firstvalue <<" of " << cnt << endl;
while(cin >> inputtedvalue)
{
if(inputtedvalue != firstvalue)
{
cout << "It Doesn't Match" << endl;
firstvalue = inputtedvalue;
}
else
{
cout << "It Matches" << endl;
}
}
}
使用名称空间std;
int main(){
int inputedvalue=0;
int firstvalue=0;
int-cnt=0;
cout(第一个值){
你的意思是firstvalue=inputedvalue;
?这是一个很好的例子,说明了糟糕的命名选择使得调试程序变得非常困难。如果firstvalue
被命名为currentvalue
,则更明显的是赋值被意外地颠倒了。这是一个错误吗?我在代码中看不到任何可能导致输出的东西“7已输入0次”。
using namespace std;
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
cout << "Enter Starting value."<< std::endl;
if (cin >> firstvalue){
cout << "New count " << firstvalue <<" of " << cnt << endl;
while(cin >> inputtedvalue)
{
if(inputtedvalue != firstvalue)
{
cout << "It Doesn't Match" << endl;
firstvalue = inputtedvalue;
}
else
{
cout << "It Matches" << endl;
}
}
}