473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Finding like-minded numbers

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

Sep 24 '08 #1
2 1179
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

Sep 24 '08 #2
On Sep 24, 7:26*pm, arnuld <sunr...@invalid.addresswrote:
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 sameway.
**
** 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 itis 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

Sep 24 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
3
by: Jorn W Janneck | last post by:
hello everyone. i have the sort of question that makes me feel like i am missing the forest for the trees, so apologies if i am missing the blatantly obvious here. i am using saxon, and mostly...
0
by: Helge Jensen | last post by:
Having posted in microsoft.public.dotnet.framework.sdk and microsoft.public.dotnet.framework.wmi without receiving any response, I posthere on the off-chance that someone who isn't following those...
9
by: dave m | last post by:
I need to be able to retrieve a unique ID from a users PC. I needs to be something a user could not easily change, like the computer name. Could someone point me in the right direction to find ...
15
by: cwsullivan | last post by:
Hi gang, I have a grid full of particles in my program, and I want to find an angle between two particles. I'm having trouble because it seems like the answer depends on whether or not the target...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
19
by: Kirk Strauser | last post by:
Given a class: how can I find its name, such as: 'foo' I'm writing a trace() decorator for the sake of practice, and am trying to print the name of the class that a traced method belongs...
0
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket...
1
by: me | last post by:
Hi, Im having a few issues with finding exactly which program may be accessing a certain file. Lets say I need to replace a DLL with a newer version, but I need to make sure its not being...
7
by: Nick | last post by:
Hi there, I have a website that functions fine locally, but when published to the server it develops a bottleneck during loading some of the pages. Basically what happens is the page loads to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.