ctfd
ctfd

Reputation: 348

Conditional return to beginning of function

I'm trying to return or repeat the function if the results of it do not meet the criteria. I've tried various things, such as do while statements, settings a BOOL flag, etc. and have been unsuccessful. What's the best way to restart the function until the criteria is met?

CODE:

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

int main() {
        std:int nine;
        std::srand(std::time(NULL));
        std::cout << nine << (rand() % 10);
        std::cout << '\n';
    if (nine == 9) {
        std::cout << 'nine';
    } else {
        main();
    }
} 

Upvotes: 0

Views: 90

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

You may not recursively call main in C++. Also your code contains some other errors. The program could look the following way.

#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;
}

You could also add an output statement

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

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

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

        std::cout << "nine" << std::endl;
}

Upvotes: 2

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

You can do as follows:

#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);
} 

Note that you don't ever change nine in your code, the loop will go forever.

Upvotes: 1

Avi Tevet
Avi Tevet

Reputation: 828

This particular code wouldn't even compile due to the std:int (missing colon typo). But assuming the typo was fixed, it also wouldn't do anything because you never assign a value to nine.

Anyway, if you want to repeat an action in your code, your best bet is a loop:

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

Upvotes: 0

Krofz
Krofz

Reputation: 111

i think your problem is in nine , you don't assign it

nine = rand()%10;

and with the basic loop it should be fine

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

Upvotes: 1

cf-
cf-

Reputation: 8856

A while loop should work just fine:

#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';
      }
    }
} 

Upvotes: 1

Related Questions