INSERTING 3 SPACES TO OUTPUT

1

1111111111

Here is what I need to do...

I need to enter a 5 digit number from 11111 to 99999 and have the
output look like this.. Example I enter 12345..It needs to look like
this when it outputs.
1 2 3 4 5 (each number needs to be 3 spaces apart)

Here is my code so far thanks for everyone help!!!
Thanks for the input everyone!! Here is what i have so far. I AM NEW
TO THIS SO DONT BASH ME!!! :)


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();

long integer;

cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<<endl;
cin>>integer;

if ((integer>99999) || (integer<11111)) cout<<"The integer you entered
is not between 11111 and 99999"<<endl;

cout<<integer%10 THIS IS WHERE I AM STUCK!!
return 0;
}
 
I

Ian

1111111111 said:
Here is what I need to do...

I need to enter a 5 digit number from 11111 to 99999 and have the
output look like this.. Example I enter 12345..It needs to look like
this when it outputs.
1 2 3 4 5 (each number needs to be 3 spaces apart)

Here is my code so far thanks for everyone help!!!
Thanks for the input everyone!! Here is what i have so far. I AM NEW
TO THIS SO DONT BASH ME!!! :)


#include <iostream.h>
Don't use this!

#include <iostream>

using namespace std;
#include <conio.h>
int main()
{
clrscr();

long integer;

cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<<endl;
cin>>integer;

if ((integer>99999) || (integer<11111)) cout<<"The integer you entered
is not between 11111 and 99999"<<endl;

cout<<integer%10 THIS IS WHERE I AM STUCK!!
return 0;
}
Give the number has 5 digits, a simple solution is something like

cout << integer%10000 << " " << ...

I'm sure you can find a more elegant or generic form if required.

Ian
 
J

John Harrison

cout<<integer%10 THIS IS WHERE I AM STUCK!!
return 0;
}

You need to know a way to get any digit out of an integer, until you figure
that, you aren't going to get very far.

integer%10 gets the last digit, what about the second last digit?

12345%10 == 5 last digit

(12345 @ ?)%10 == 4 second last digit

Can you work out what goes in place of @ and ? to get the second last digit?
Hint, ? is a number and @ is an operator (*, / +, -, one of those four).

Now what about the third last digit, very similar formula to the second last
digit, once you've figured the second last digit, the third last digit is
easy. And so on.

john
 
T

Thomas Matthews

1111111111 said:
Here is what I need to do...

I need to enter a 5 digit number from 11111 to 99999 and have the
output look like this.. Example I enter 12345..It needs to look like
this when it outputs.
1 2 3 4 5 (each number needs to be 3 spaces apart)

Here is my code so far thanks for everyone help!!!
Thanks for the input everyone!! Here is what i have so far. I AM NEW
TO THIS SO DONT BASH ME!!! :)


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();

long integer;

cout<<"Please enter a 5 digit integer (from 11111 to 99999)"<<endl;
cin>>integer;

if ((integer>99999) || (integer<11111)) cout<<"The integer you entered
is not between 11111 and 99999"<<endl;

cout<<integer%10 THIS IS WHERE I AM STUCK!!
return 0;
}

One could treat each digit as a character and eliminate the math
part:
[Warning: untested, uncompiled code follows]

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; // because I'm lazy and know better.

int main(void)
{
string reply;
bool valid_number(false);

while (!valid_number)
{
cout << "\nPlease enter a 5 digit integer (from 11111 to 9999):";
cout.flush(); // Ensure that the prompt is displayed.
getline(cin, reply, '\n'); // read in those digits.

// Now to validate the user's input.
if (reply.length() == 5)
{
valid_number = true;
for (unsigned int i = 0;
valid_number && (i < 5);
++i)
{
valid_number = is_digit(reply);
}

// Add check here to see that number is
// greater than 11111.

} // End: if reply has 5 characters.
} //End: while not valid number

for (unsigned int i = 0; i < 5; ++i)
{
cout << " " << reply;
}
cout << endl;
return EXIT_SUCCESS;
}

This _may_ not be what the instructor expected, but
it fulfills the requirements. :)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
E

E. Robert Tisdale

1111111111 said:
Here is what I need to do...

I need to enter a 5 digit number from 11111 to 99999 and have the
output look like this.. Example I enter 12345..It needs to look like
this when it outputs.
1 2 3 4 5 (each number needs to be 3 spaces apart)

cat main.cc
#include <iostream>
#include <iomanip>

class Integer {
private:
// representation
int I;
public:
// functions
friend
std::eek:stream& operator<<(std::eek:stream& os, const Integer& i);
// operators
// implicit
operator int(void) const { return I;}
operator int(void) { return I;}
// constructors
Integer(int i = 0): I(i) { }
};

inline
std::eek:stream& operator<<(std::eek:stream& os, const Integer& i) {
int q = (int)i/10;
int r = (int)i%10;
if (0 < q)
os << Integer(q) << std::setw(4) << r;
else
os << r;
return os;
}

int main(int argc, char* argv[]) {
const
int i[] = {11111, 12345, 23456, 34567, 99999};
const
size_t n = sizeof(i)/sizeof(i[0]);
for (size_t j = 0; j < n; ++j)
std::cout << Integer(i[j]) << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
1 1 1 1 1
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
9 9 9 9 9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top