Finding like-minded numbers

A

arnuld

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

arnuld

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.
 
B

Barry

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top