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