473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

INSERTING 3 SPACES TO OUTPUT

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;
}
Jul 22 '05 #1
4 1456
Ian
1111111111 wrote:
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
Jul 22 '05 #2
> 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
Jul 22 '05 #3
1111111111 wrote:
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[i]);
}

// 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[i];
}
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

Jul 22 '05 #4
1111111111 wrote:
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::ostream& operator<<(std::ostream& 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::ostream& operator<<(std::ostream& 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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Sharman | last post by:
Writing a form, several questions have got a list of checkboxes, with associated descriptions (mostly one word), all on a line. Eg: Describe yourself: Fat Hairy Ugly Wears glasses. I want to...
0
by: Chris Sharman | last post by:
Writing a form, several questions have got a list of checkboxes, with associated descriptions (mostly one word), all on a line. Eg: Describe yourself: Fat Hairy Ugly Wears glasses. I want to...
4
by: epaetz | last post by:
I'm doing a bcp out of a table to a file. Some of the fields in a record may have an empty string. When I bcp out to the file and examine it, the fields that have an empty string in the database...
2
by: Alan Silver | last post by:
Hello, VWD Express seems obsessed with inserting loads of spaces at the start of each line whenever you drag controls into the source view. I guess this is MS' idea of making the source code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.