Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Finding like-minded numbers

Reply
Thread Tools

Finding like-minded numbers

 
 
arnuld
Guest
Posts: n/a
 
      09-24-2008
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::streamsiz e>::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

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      09-24-2008
> 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

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-24-2008
On Sep 24, 7:26*pm, arnuld <sunr...@invalid.address> 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::streamsiz e>::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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
win32com ppt embedded object numbers reverting back to original numbers Lance Hoffmeyer Python 2 07-26-2007 01:45 AM
Fibonacci Numbers and Lucas Numbers Andrew Tatum C++ 6 05-27-2007 12:47 AM
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
Re: Numbers and more numbers... Breedo A+ Certification 0 12-02-2005 12:45 AM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57