import java.util.*;

class occult {
    public static void main(String[] args){
        int n = 2;

        for (int i = 0; ; i++){
            if(Integer.toString(i).contains("666")){
                if ( n == 1 )
                    System.out.println(i);
                n -= 1; 
            }
        }


    }
}

I have written the above java code into c++ . i wanted to know is this the correct way to translate into c++.
is there any better way to achieve in c++.

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

string toString(int n){
    ostringstream oss;
    oss << n;
    return oss.str();
}

int main()
{
    int n = 2;
    string occult("666");
    for (int i = 0; ; i++)
    {
        if(toString(i).find(occult) != string::npos){
            if(n == 1)
                cout << i;
            n = n-1;
        }

    }

    return 0;
}

If using C++11, there's an std::to_string function in the <string> header.

#include <iostream>
#include <string>

int main()
{
    std::string occult = "666";

    for (int i = 0, n = 2; ; ++i)
    {
        if (std::to_string(i).find("666") != std::string::npos)
        {
            if (n == 1)
            {
                std::cout<<i<<"\n";
            }
            n -= 1;
        }
    }

    return 0;
}

Another way to convert an integer to a string is (Using the C header: <cstring>):

#include <iostream>
#include <cstring>

int main()
{
    char buff[256] = {0};
    std::string occult = "666";

    for (int i = 0, n = 2; ; ++i)
    {
        sprintf(buff, "%d", i);
        if (strstr(buff, "666"))
        {
            if (n == 1)
            {
                std::cout<<i<<"\n";
            }
            n -= 1;
        }
    }

    return 0;
}

This is an infinite loop: for( int i = 0, n = 2; ; ++i ) { /* ... (no break statement) */ }
A missing condition makes the implied while clause equivalent to while(true).

At some point of time, when i becomes equal to std::numeric_limits<int>::max(), ++i will cause a signed integer overflow.
When n becomes equal to std::numeric_limits<int>::min(), n -= 1 will also trigger a signed integer overflow.

Don't know what happens in Java, but in C++, signed integer overflow engenders undefined behaviour.

Change the type of i and n to unsigned int and the program is well-formed (though the infinite loop still remains). Unsigned integers obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of the unsigned integer.

Comments
Thanks for the info.

I modified a bit , but now i dont get the result .when i run the program , its keeps waiting.

I have posted the code below .

#include<iostream>
#include <string>

using namespace std;


int main()
{
    const string s = "666";
    for (int i = 0, n =2 ; ; i++)
    {
        if( to_string(i).compare(s) == 0){
            if ( n == 1 ){
                cout << i;
                break;
            }  
            n -= 1;
        }
    }

    return 0;
}

Here's your main problem:

if( to_string(i).compare(s) == 0){

string::compare looks for an exact match, which only happens when i is 666.

But you're ignoring the first time the match happens, so your code as written will happily spin on forever, looking for a nonexistent other 666 to show up.

If you want to reproduce Java's String.contains, you should use string::find instead.

Another observation:

cout << i;

This, unlike System.out.println, won't write a newline for you. Have a look at std::endl.

99% of the time, you want to use \n and not std::endl. std::endl will flush the buffer immediately. It's pretty much equivalent to Java's System.err.println or std::cerr.

std::cout<<blah<<"\n" is not the same as std::cout<<blah<<std::endl..

when i run the program , its keeps waiting

The proram engenders undefined behaviour; C++ has nothing to say about what its observable behaviour ought to be.

Using unsigned integer types for n and i would make the program constructs well-defined. However, it would still be 'computationally horrendous'.

Avoiding the expensive convert-to-string/check-for-substring would make it less horrendous; changing the type of n from std::uint32_t to std::uint16_t would make it tractable.

Also, limiting the output to a finite number:

#include <iostream>
#include <cstdint>
#include <iomanip>
#include <limits>

bool has_digits_666( unsigned int number )
{
    while( number > 665 )
    {
        if( number%1000 == 666 ) return true ;
        number /= 10 ;
    }

    return false ;
}

int main()
{
    std::uint16_t n = 2 ; // unsigned 16-bit wide integer
    int cnt = 50 ;

    for( unsigned int i = 0 ; cnt > 0 ; ++i ) // unsigned
    {
        if( has_digits_666(i) )
        {
            if( n == 1 )
            {
                constexpr int width = std::numeric_limits<int>::digits10 + 3 ;
                std::cout << std::setw(width) << i << ' ' ;
                if( --cnt % 5 == 0 ) std::cout << '\n' ; // 5 numbers per line
            }
            --n ;
        }
    }
}

It still performs a very large number of computations:

real    0m15.834s
user    0m15.833s
sys     0m0.000s

http://coliru.stacked-crooked.com/a/869a146a74f3f9eb

This article has been dead for over six months. Start a new discussion instead.