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

  • Context: C/C++ 
  • Thread starter Thread starter ranger
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around techniques for formatting the output of a multi-digit number in C++, specifically how to place spaces between each digit for clearer readability. Participants explore various methods, including string manipulation and integer operations, while also addressing issues related to different compilers.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests storing the number as a string and then copying it to another string while appending spaces after each digit.
  • Another participant proposes processing the number directly as an unsigned integer, extracting digits using modulus and division operations, and storing them in reverse order.
  • A suggestion is made to consider using Python for this task, highlighting its ease of handling such formatting.
  • A function is provided that prints an integer with spaces between each digit, demonstrating a working example in C++. This includes a test program that successfully formats the output.
  • A participant shares a program that uses integer division and modulus operations to separate digits but notes issues with negative outputs when using Turbo C++ for larger numbers.
  • Another participant attributes the negative output to the integer size limitation in Turbo C++, suggesting the use of "unsigned int" or "unsigned long int" for better results.
  • There is a recommendation to use a for loop for a more efficient approach to digit separation.
  • A participant confirms that using "long int" in Turbo C++ resolves the negative output issue due to the limitations of the standard int type.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to achieve the desired output formatting, with no consensus on a single method. There is also disagreement regarding the behavior of different compilers when handling large integers.

Contextual Notes

Limitations include the dependency on compiler behavior regarding integer sizes, which affects the output for high-value integers. The discussion does not resolve the specific issues related to Turbo C++ versus other compilers.

ranger
Gold Member
Messages
1,687
Reaction score
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
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.
 
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
 
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);
}
 
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.
 
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.
 
I also suggest using a for loop.

- Warren
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
81
Views
7K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
65
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
19
Views
4K
  • · Replies 47 ·
2
Replies
47
Views
4K