Separate Digits in C++: How to Place Spaces Between Numbers for Easier Output

  • C/C++
  • Thread starter ranger
  • Start date
  • Tags
    C++
In summary, the conversation discusses ways to add spaces between digits in a six digit number when storing it in one variable. Some suggestions include converting the number to a string and then copying it to another string with spaces, or using integer and modulus operations to process the number directly. It is also mentioned that using languages such as Python or C++ may make this task easier. There is also a discussion about the limitations of integer sizes in different compilers, with suggestions to use unsigned int or long int.
  • #1
ranger
Gold Member
1,687
2
If I take a six digit number and store it in one variable, how can I place spaces between each digit at the output. This would be so much easier if each digit were stored in separate variables.


--thanks
 
Technology news on Phys.org
  • #2
If your number is not already in a string then first store it as a string using your favorite method (sprintf, string streams, anything you like). Then copy this string to another that is twice as long. Copy it one digit at a time and append a space after each digit.

Alternatively, process the number directly. It is an unsigned integer, right? So do "digit = number % 10", store the digit to a string (going right to left) as a character, add a space character between each one, divide "number /= 10" before proceeding with the next digit. Repeat 6 times.
 
  • #3
If you're not required to use C++, consider using another language instead. Python, for example, can do such things quite easily.

In Python:

Code:
from string import join, zfill

n = 12345
print join( zfill(n, 6), ' ' )

- Warren
 
  • #4
You can make your own function that prints integers with a space between each digit (and the possible minus sign), like this:
Code:
void rangers_integer_print(int n)
{
  stringstream s;
  char c;

  s << n;  

  while (s.get(c))  
	 cout << c << " ";
}


Here is a little test program using the function (I ran it and it seems to work fine)
Code:
#include <iostream>
#include <sstream>

using namespace std;

void rangers_integer_print(int n)
{
  stringstream s;
  char c;

  s << n;

  while (s.get(c))  
	 cout << c << " ";
}

void main()
{
  int n;

  cout << endl << "enter number: ";
  cin >> n;  

  rangers_integer_print(n);
}
 
  • #5
Here is the program...(using 5 digits)


Code:
#include <iostream.h>

main()
{

	int num;              //user input
	int a,b,c,d,e,f,g,h;  //used for integer division and modulus operations

	cout <<"Enter a five digit number." <<endl;
	cin >> num;

	
	
	a = num/10000;           //   
        b = num%10000;         //             
        c = b/1000;              //             
	d = b%1000;              //            
	e = d/100;               //Integer and Modulus Operations
	f = d%100;              //             
	g = f/10;                //            
	h = f%10;              //  

    cout <<endl;
	cout <<a<<"   "<<c<<"   "<<e<<"   "<<g<<"   "<<h<<" ";
	cout <<endl;

	return 0;

}

I wrote this program using Visual C++ 6.0. When I run the program it works fine. But when I run the program using Turbo C++ I keep getting a negative number or some other number for the output when I enter high numbers ex-98765 i get -3 -2 -3 0 -7 as the out for Turbo C++. It works fine for lower number - 12345.

Any idea why this happens?

--thanks.
 
  • #6
The old Turbo-C++ integer size must be 16 bits while the MS compiler uses 32 bits. Using "unsigned int" will help a little, using "unsigned long int" will help even more.
 
  • #7
I also suggest using a for loop.

- Warren
 
  • #8
if you use long int in turbo c++ it will give you the right answers instead of negative answer the reason is that int can not store a a high value of integers into it it has a limit so long int can help u


hope the answer satisfies you
regards
Engineer Asim Arshad
 

1. How can I separate individual digits from a number in C++?

There are a few different approaches to separating digits in C++. One method is to convert the number to a string and then use a loop to iterate through each character in the string. Another method is to use the modulus (%) operator to extract the last digit and then divide the number by 10 to remove the last digit. This process can be repeated until all digits have been separated.

2. Can I separate digits from a negative number in C++?

Yes, the same methods mentioned in the first question can be used to separate digits from a negative number in C++. However, it is important to keep track of the negative sign and handle it accordingly in your code.

3. How can I store the separated digits in an array or vector in C++?

To store separated digits in an array or vector, you can use the push_back() function to add each digit to the end of the array or vector. Alternatively, you can use the insert() function to insert each digit at a specific index in the array or vector.

4. Is there a built-in function in C++ for separating digits from a number?

No, there is not a built-in function specifically for separating digits from a number in C++. However, there are built-in functions that can be used in conjunction with the methods mentioned in the first question, such as to_string(), stoi(), and atoi().

5. Can I separate digits from a floating-point number in C++?

Yes, the methods mentioned in the first question can also be used to separate digits from a floating-point number in C++. However, you may need to handle the decimal point separately depending on your desired output.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
3K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
4
Views
335
  • Programming and Computer Science
Replies
3
Views
2K
Replies
4
Views
215
  • Precalculus Mathematics Homework Help
Replies
13
Views
907
Back
Top