Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
    int a,bو,c;
    cout<<"Enter the First Number : ";
    cin>>a;
    cout<<"Enter the Second Number : ";
    cin>>b;

    c=a*b;

    cout<< "The Result is : "<<c;

}


when c=a*b
a = 75
b = 86

attention. the result will appear like this 6540
but i want space between the number like this 6 5 4 0 ;OK space between nubmer how can
Posted
Updated 27-Apr-13 14:16pm
v2

A C-like approach was:
C
...
const int size = 20;
char buffer[size];
int n = snprintf(buffer, size, "%d", c); // use the safe function: snprintf, do not use sprintf
assert(n >= 0);   // format error is indicated by a negative value
assert(n < size); // n is the number of characters written, excluding the terminating null-character
for(int i = 0; i < n; ++i) cout << buffer[i] << ' ';

What it does:
1) write the c into a character array (buffer).
2) make sure you do not write past the buffer end (use snprintf[^])
3) make sure the conversion does not need more characters than provided by the buffer (take in count the return value and assert[^] that writing was not expecting a larger buffer).
4) finally write each character followed by a space

A C++ like approach was to use ostringstream[^].
C++
...
ostringstream oss;
oss << c;
assert(oss.good()); // no format error
// C++11 version
for(char ch: oss.str()) cout << ch << ' ';
// iterator based version
string s = oss.str();
for(string::iterator it = s.begin(); it != s.end(); ++it) cout << *it << ' ';

Cheers
Andi
 
Share this answer
 
Convert to a string ( hint: look at sprintf ) then iterate through the characters outputting each one followed by a space.
Regards,
Ian.
 
Share this answer
 
Comments
nv3 28-Apr-13 3:27am    
It's as simple as that. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900