Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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,如果函数的结果不符合条件,我将尝试返回或重复该函数。我尝试过各种方法,例如do while语句、设置BOOL标志等,但都没有成功。在满足条件之前,重新启动函数的最佳方法是什么 代码: #包括 #包括 #包括 #包括 int main(){ 标准:国际九号; std::srand(std::time(NULL)); std::coutwhile循环应该可以正常工作: #include <iostream> #include <cstdlib> #include <ct

如果函数的结果不符合条件,我将尝试返回或重复该函数。我尝试过各种方法,例如
do while
语句、设置
BOOL
标志等,但都没有成功。在满足条件之前,重新启动函数的最佳方法是什么

代码:

#包括
#包括
#包括
#包括
int main(){
标准:国际九号;
std::srand(std::time(NULL));

std::coutwhile循环应该可以正常工作:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main() {
    int nine = 0;
    srand(std::time(NULL));

    while (nine != 9)
    {
      nine = (rand() % 10);
      std::cout << nine << std::endl;

      if (nine == 9) {
        std::cout << 'nine';
      }
    }
} 
#包括
#包括
#包括
#包括
int main(){
int九=0;
srand(std::time(NULL));
而(九!=9)
{
九=(rand()%10);

std::coutwhile循环应该可以正常工作:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main() {
    int nine = 0;
    srand(std::time(NULL));

    while (nine != 9)
    {
      nine = (rand() % 10);
      std::cout << nine << std::endl;

      if (nine == 9) {
        std::cout << 'nine';
      }
    }
} 
#包括
#包括
#包括
#包括
int main(){
int九=0;
srand(std::time(NULL));
而(九!=9)
{
九=(rand()%10);

std::cout我想你的问题在
nine
,你没有分配它

nine = rand()%10;
使用基本循环应该可以

while ( nine != 9){
      nine = rand()%10;
}

我想你的问题出在第九个,你没有分配它

nine = rand()%10;
使用基本循环应该可以

while ( nine != 9){
      nine = rand()%10;
}
您可以执行以下操作:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main() {

     std::srand(std::time(NULL));

     do {
          std:int nine;
          std::cout << nine << (rand() % 10);
          std::cout << '\n';
          if(nine == 9) {
              std::cout << 'nine';
              break;
          } 
     } while(true);
} 
#包括
#包括
#包括
#包括
int main(){
std::srand(std::time(NULL));
做{
标准:国际九号;
std::cout您可以执行以下操作:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main() {

     std::srand(std::time(NULL));

     do {
          std:int nine;
          std::cout << nine << (rand() % 10);
          std::cout << '\n';
          if(nine == 9) {
              std::cout << 'nine';
              break;
          } 
     } while(true);
} 
#包括
#包括
#包括
#包括
int main(){
std::srand(std::time(NULL));
做{
标准:国际九号;

std::cout由于
std:int
(缺少冒号输入),这个特定的代码甚至无法编译。但是假设输入错误是固定的,它也不会做任何事情,因为您从未为
nine
赋值

无论如何,如果要在代码中重复操作,最好的选择是循环:

int nine = 0;
do {
  nine = rand() % 10;
} while (nine != 9);

由于
std:int
(缺少冒号输入),此特定代码甚至无法编译。但假设输入错误已修复,它也不会做任何事情,因为您从未为
nine
赋值

无论如何,如果要在代码中重复操作,最好的选择是循环:

int nine = 0;
do {
  nine = rand() % 10;
} while (nine != 9);

您可能不会递归调用C++中的Mead。您的代码也包含其他错误。程序可以采用以下方式:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() 
{
        std::srand( ( unsigned )std::time( NULL) );
        int nine;

        do
        {      
                nine = std::rand() % 10;
        } while ( nine != 9 );

        std::cout << "nine" << std::endl;
}
#包括
#包括
#包括
int main()
{
std::srand((未签名)std::time(空));
int九;
做
{      
九=标准::兰德()%10;
}而(9!=9);

STD::CUT

你不能递归调用C++中的主元素。你的代码也包含了其他错误。程序可以看下面的方式。

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() 
{
        std::srand( ( unsigned )std::time( NULL) );
        int nine;

        do
        {      
                nine = std::rand() % 10;
        } while ( nine != 9 );

        std::cout << "nine" << std::endl;
}
#包括
#包括
#包括
int main()
{
std::srand((未签名)std::time(空));
int九;
做
{      
九=标准::兰德()%10;
}而(9!=9);

std::你为什么要使用
main
进行递归?只需定义另一个递归函数并在
main
中使用它。它可能通过迭代完成tho@mangusta好的问题,但我认为它会起作用,这就是为什么我问你。你有一个例子吗?THXG++:警告:ISO C++禁止使用函数的地址::main“[-pedantic]”int是一个关键字;它没有在任何名称空间中定义。此外,我不知道您在这里试图做什么,但我看不出循环如何不能帮助您实现它。如果您不确定递归深度的上限是否合理,递归通常是一个坏主意。@heinrichj,我想超越递归的可能性,好吧,也许不是。谢谢你的解释-很抱歉,我的示例显然很糟糕,但希望你能理解我的意思。为什么要使用
main
进行递归?只需定义另一个递归函数并在
main
中使用它。这可能是通过迭代完成的tho@mangusta,好问题,但我想会的D工作,这就是为什么我问。你有一个例子吗?THXG++:警告:ISO C++禁止函数的地址::主[ [学究] ] int是一个关键字;它没有在任何名称空间中定义。此外,我不知道您在这里试图做什么,但我看不出循环如何不能帮助您实现它。如果您不确定递归深度的上限是否合理,递归通常是一个坏主意。@heinrichj,我想超越递归的可能性,嗯,也许不是。谢谢你的解释-很抱歉,我的示例显然很糟糕,但希望你能理解我的意思。真的吗?它对我有用。也许可以放松一下编译器的机箱。是的,这是一个错误,所以感谢你指出它。:)当然它编译--
std
只是一个标签!请参阅我对OP.re的评论ally?这对我很有效。也许可以放松一下你的编译器机箱。是的,这是一个错误,所以感谢你指出它。:)确保它编译--
std
只是一个标签!请参阅我对OP的评论。