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