Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++_Loops_While Loop - Fatal编程技术网

C++:循环次数时

C++:循环次数时,c++,loops,while-loop,C++,Loops,While Loop,因此,我试图编写一个基本程序,要求用户输入除5以外的任何数字,在用户不输入数字5的10次迭代后,我希望程序打印到屏幕上。 以下是我目前的代码: #include <iostream> #include <string> using namespace std; int main(){ int num; cout << "Please enter a number other than 5." << endl; cin

因此,我试图编写一个基本程序,要求用户输入除5以外的任何数字,在用户不输入数字5的10次迭代后,我希望程序打印到屏幕上。 以下是我目前的代码:

#include <iostream>
#include <string>
using namespace std;

int main(){

    int num;

    cout << "Please enter a number other than 5." << endl;
    cin >> num;

    while (num != 5){
        cout << "Please enter a number other than 5." << endl;
        cin >> num;
    }

    return 0;
}

我只是不知道如何告诉计算机在10次迭代时停止循环并输出到屏幕。

这是利用

#include <iostream>
#include <string>
using namespace std;

int main(){

    int num;
    int counter=1;

   cin >> num;
   cout <<num;
   if(num==5) 
    cout << "Please enter a number other than 5." << endl;



    while (num != 5&&counter<=10){
        cin >> num;
        cout <<num;
        if(num==5) 
        cout << "Please enter a number other than 5." << endl;
        counter=counter+1;
    }

    return 0;
}
do while
环路

它的工作方式是在块内执行语句而不计算任何条件,然后计算条件以确定循环是否应再次运行

这就是你的程序的样子

#include <iostream>
using namespace std;

int main(void)
{
int counter = 0, num;
do
{
if (counter > 10) // or >=10 if you want to display if it is 10
{
cout << "exiting the loop!" << endl;
break; // the break statement will just break out of the loop
}
cout << "please enter a number not equal to 5" << endl;
cin >> num;
counter++; // or ++counter doesn't matter in this context

}
while ( num != 5);
return 0;
} 

使用计数器跟踪。如果用户在while循环中输入5会发生什么?嘿,路易斯!欢迎来到Stackoverflow。。。根据你的问题,我建议你至少选择两个选项,阅读它们,然后你可以回到这里问你的问题。。我们将非常乐意帮助您:-是,这是他所要求的…如果用户输入5,那么他需要退出循环