Public Group

# Breaking a three-digit integer. Is this a good way?

This topic is 3852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently had a computer science exam and I messed it up a bit. There was an exercise to break a three digit number to three numbers. I didn't do it..because it's different to be under the pressure than in your room. So I sat and did it now. I'm asking if this is good way to do it(it works by the way)
#include <iostream>

int main()
{
int num;
std::cout << "Enter a number between 100 and 999: ";
do
{
std::cin >> num;
if( num < 100 || num > 999 )
std::cout << "100-999 please. Enter it again: ";

} while ( num < 100 || num > 999 );

int i, j, k;

for( int a = 900; a >= 100; a -= 100 )
{
if( num / a == 1 )
{
i = a / 100;
break;
}
}

for( int a = 90; a >= 10; a -= 10 )
{
if( (num - i * 100) / a == 1 )
{
j = a / 10;
break;
}

if( a == 20 )
j = 0;
}

k = num - (i * 100) - (j * 10);

std::cout << "i = " << i << ", j = " << j << ", k = " << k << "\n";

system("pause");
return 0;
}



##### Share on other sites
123 = (1 * 10 + 2) * 10 + 3123 % 10 = 3123 / 10 = 1212 % 10 = 212 / 10 = 1

Therefore, a simpler way is:

int i = num % 10;int j = (num / 10) % 10;int k = num / 100;

##### Share on other sites
Oh that was so simple :(

##### Share on other sites
Suggested exercise: Write a program that will take a number and break it into it's digits. The program should work no matter how many digits the number has (within the limits of an integer, of course).

For example, if the input is 23, the output should be 2, 3. If it's 2053, the output should be 2, 0, 5, 3, etc.

##### Share on other sites
Quote:
 Original post by Gage64Write a program that will take a number and break it into its digits.

#include <algorithm>#include <iostream>#include <iterator>#include <sstream>#include <string>int main(){    std::cout << "enter a number: ";    int i = 0;    std::cin >> i;    std::stringstream ss;    ss << i;    std::string s = ss.str();    std::copy(s.begin(), s.end() - 1, std::ostream_iterator<char>(std::cout, ", "));    std::cout << *(s.end() - 1);}

##### Share on other sites
Quote:
 Original post by DevFred*** Source Snippet Removed ***

First, the suggestion was geared towards the OP so that he'll get a better understanding of this, along with some additional practice. Posting a solution kind of gets in the way of that, don't you think?

Second, the idea was to use a method like that described by ToohrVyk (and clearly that is what should have been used in the test). In your code, you are letting the STL do all the work for you, and therefore bypassing any need to think about how to solve this yourself (unless the objective is to get some practice using the STL, but clearly that is not the case here).

##### Share on other sites
My english is so bad, it cost me more than 10 minutes to get the means,and then
find it so easy.

cout<< num/100<<endl;
cout<<(num%100)/10<<endl;
cout<<num%10<<endl;

##### Share on other sites
I'm working on it.

##### Share on other sites
Ok, here's mine:

#include <iostream>#include <vector>#include <cmath>int main(){	int num, size = 0, a;	std::cout << "Enter a number: ";	std::cin >> num;	std::vector< int > digits;	for( int i = 10;; i*= 10 ) // get how many digits the number has	{		if( num / i == 0 )		{			a = i / 10;			size = log10( float(i));			digits.reserve(size);			break;		}	}	for( int i = a; i != 0; i/=10 ) // put the digits in the vector		digits.push_back( num / i % 10 );		for( std::vector< int >::const_iterator iter = digits.begin(); iter != digits.end(); ++iter ) // display		std::cout << *iter << " ";	std::cout << "\n";	system("pause");	return 0;}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×