Finding like-minded numbers

Discussion in 'C++' started by arnuld, Sep 24, 2008.

  1. arnuld

    arnuld Guest

    Again, my friend, who does not much access to internet, has given me a
    working program and I need your views on improvement and design. I put
    some error checking but my experience with C has made it a messy error
    checking, as compared to C++ standards. I will welcome any constructive
    criticism:


    /* This program has actually emerged from a real world requirement. In India, vehicle
    * registration numbers are alloted from 0 to 9999, and many people want to have their
    * vehicle number based on their single lucky number e.g. like my friend has 1 as his
    * lucky number. Vehicle numbers are always alloted in four digits, padded by zeroes if
    * necessary, like 0001, 0567, hence the output needs to be int he same way.
    *
    * VERSION 1.0
    *
    */


    #include<iostream>


    int get_lucky_num();
    void print_lucky_nums( int& );


    const int final_num = 10;

    int main()
    {
    int lucky_num;
    const int no_num = -1;


    lucky_num = get_lucky_num();

    if( lucky_num != no_num )
    {
    print_lucky_nums( lucky_num );
    }


    return 0;
    }




    int get_lucky_num()
    {
    std :: cout <<"Please Enter a single digit to get the collection :- ";

    int num = final_num;
    std::cin.clear();
    std::cin >> num;

    /* This very carefully checks for anythigng greater than "final_num" but
    is this the right check to know user entered the non-dgit character like
    @ or even F , some user's are stupid anyway ;)
    */
    while( (num >= final_num) )
    {
    std::cout << "INPUT = "
    << num
    << "\n";


    std::cout << "\nAre you drunk? \nPlease enter a number less than "
    << final_num
    << ": ";

    std::cin.clear();
    // I get th enext line from somehwere but have no idea why it is here.
    // I am only using it because if I don't use it ans uder enters something
    // non-digit like F then program falls into infinite loop
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin >> num;
    }

    return num;
    }



    // can't I have a way to pass the limit of 10,000 as some constant integer ?
    void print_lucky_nums( int& num )
    {
    int k;

    for(int i =0 ; i < final_num; ++i)
    {
    for(int f=0; f < final_num ; ++f)
    {
    for(int g=0; g < final_num; ++g)
    {
    for(int h=0; h < final_num; ++h)
    {
    k=i+f+g+h;

    int div,modu,sum;
    div = k / 10;
    modu = k % 10;
    sum = div + modu;

    if( num == sum)
    {
    std :: cout << i
    << f
    << g
    << h
    << std::endl;
    }
    }
    }
    }
    }
    }





    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Google Groups is Blocked. Reason: Excessive Spamming
    arnuld, Sep 24, 2008
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    Finding lucky numbers

    > On Wed, 24 Sep 2008 16:26:57 +0500, arnuld wrote:


    Just changed the subject to reflect the content. I shopuld have done it
    earlier. apologies.


    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Google Groups is Blocked. Reason: Excessive Spamming
    arnuld, Sep 24, 2008
    #2
    1. Advertising

  3. arnuld

    Barry Guest

    On Sep 24, 7:26 pm, arnuld <> wrote:
    > Again, my friend, who does not much access to internet, has given me a
    > working program and I need your views on improvement and design. I put
    > some error checking but my experience with C has made it a messy error
    > checking, as compared to C++ standards. I will welcome any constructive
    > criticism:
    >
    > /* This program has actually emerged from a real world requirement. In India, vehicle
    >  * registration numbers are alloted from 0 to 9999, and many people want to have their
    >  * vehicle number based on their single lucky number e.g. like my friend has 1 as his
    >  * lucky number. Vehicle numbers are always alloted in four digits, padded by zeroes if
    >  * necessary, like 0001, 0567, hence the output needs to be int he same way.
    >  *
    >  * VERSION 1.0
    >  *
    >  */
    >
    > #include<iostream>
    >
    > int get_lucky_num();
    > void print_lucky_nums( int& );
    >
    > const int final_num = 10;
    >
    > int main()
    > {
    >   int lucky_num;
    >   const int no_num = -1;
    >
    >   lucky_num = get_lucky_num();
    >
    >   if( lucky_num != no_num )
    >     {
    >       print_lucky_nums( lucky_num );
    >     }
    >
    >   return 0;
    >
    > }
    >
    > int get_lucky_num()
    > {
    >   std :: cout <<"Please Enter a single digit to get the collection :- ";
    >
    >   int num = final_num;  
    >   std::cin.clear();
    >   std::cin >> num;
    >
    >   /* This very carefully checks for anythigng greater than "final_num" but
    >      is this the right check to know user entered the non-dgit character like
    >      @ or even F , some user's are stupid anyway ;)  
    >   */
    >   while( (num >= final_num) )
    >     {
    >       std::cout << "INPUT = "
    >                 << num
    >                 << "\n";
    >
    >       std::cout << "\nAre you drunk? \nPlease enter a number less than "
    >                 << final_num
    >                 << ": ";  
    >
    >       std::cin.clear();
    >       // I get th enext line from somehwere but have no idea why it is here.
    >       // I am only using it because if I don't use it ans uder enters something
    >       // non-digit like F then program falls into infinite loop
    >       std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    >       std::cin >> num;
    >     }


    I think this while statement has to be something like this,
    taking state of std::cin into account.

    delete std::cin >> num; in before "while"

    while (std::cin >> num && num >= final_num)
    {
    ...
    }

    >
    >   return num;
    >
    > }
    >
    > // can't I have a way to pass the limit of 10,000 as some constant integer ?
    > void print_lucky_nums( int& num )
    > {
    >   int k;
    >
    >   for(int i =0 ; i < final_num; ++i)
    >     {  
    >       for(int f=0; f < final_num ; ++f)
    >         {
    >           for(int g=0; g < final_num; ++g)
    >             {
    >               for(int h=0; h < final_num; ++h)
    >                 {
    >                   k=i+f+g+h;
    >
    >                   int div,modu,sum;
    >                   div  = k / 10;
    >                   modu = k % 10;
    >                   sum  = div + modu;
    >
    >                   if( num == sum)
    >                       {
    >                         std :: cout << i
    >                                     << f
    >                                     << g
    >                                     << h
    >                                     << std::endl;
    >                       }
    >                 }                
    >             }          
    >         }
    >     }
    >
    > }
    >


    My Firefox scans that "num" is not changed in "print_lucky_nums",
    but why "int&" as param type?

    --
    Best Regards
    Barry
    Barry, Sep 24, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VJL
    Replies:
    0
    Views:
    379
  2. Subra
    Replies:
    25
    Views:
    1,185
    user923005
    Mar 8, 2007
  3. Andy B
    Replies:
    3
    Views:
    301
    Munna
    Jun 12, 2008
  4. Matt Pattison
    Replies:
    4
    Views:
    154
    Daniel Amelang
    Jun 15, 2005
  5. D'Anne Asquith

    Numbers not acting like numbers

    D'Anne Asquith, Oct 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    91
    Heinrich Mislik
    Oct 2, 2003
Loading...

Share This Page