473,378 Members | 1,544 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,378 software developers and data experts.

How to read a part of an integer

Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

May 3 '07 #1
11 2337
panther wrote:
Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?
you need to split the number manually. At first, do you need 0921 or 921
as a last number? because if you don't want to loose the zero you have
to work at a string level. If you want to loose the zero, and you just
want to work generally with integers, you can just for example take the
last four digits by doing

i % 10000;

and then go for the high order digits by

i /= 10000;

Regards,

Zeppe
May 3 '07 #2
On May 3, 12:53 pm, panther <tharaka...@gmail.comwrote:
Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?
panther's response is the right way to do it.
However, if you simply need to turn the integer into a string, you
could use itoa(int) or a some other comparable functions.

May 3 '07 #3
panther <th********@gmail.comwrote:
I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?
One way would be to use the % and / operators. For example,

i = 761120921
j = i / 10000000
k = (i / 10000) % 1000
l = i % 1000

Another way would be to read the number in as a string, and use the
string::substr() function.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 3 '07 #4
Marcus Kwok <ri******@gehennom.invalidwrote:
panther <th********@gmail.comwrote:
>I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

One way would be to use the % and / operators. For example,

i = 761120921
j = i / 10000000
k = (i / 10000) % 1000
l = i % 1000
Actually it should be l = i % 10000 if you wanted the last 4 digits.
Another way would be to read the number in as a string, and use the
string::substr() function.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 3 '07 #5
seank76 wrote:
On May 3, 12:53 pm, panther <tharaka...@gmail.comwrote:
Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

panther's response is the right way to do it.
However, if you simply need to turn the integer into a string, you
could use itoa(int) or a some other comparable functions.
That is a non-standard function, and therefore is platform-specific and
off-topic in this group. There are a number of ways to accomplish this
with resorting to such things.

The best idea would be to use std::string and the capabilities from
sstream.

Brian

May 3 '07 #6
"panther" <th********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?
It depends. It sounds to me like this is some type of part number with
different positions specifying different things. I once did this in a
retail system and realized when they ran out of space in the 2 byte int that
I should of use strings so they could use characters.

Anyway, it depends mostly becuase you said "if user enters..." which means
you are dealing with user input. I find the best way to deal with user
input is by characters, by strings. Once you get it in a string you can see
exactly what they entered.

That is, what if they etenered
071120921
when that gets put into an int it becomes
71120921
And now you have more of a headache, especially if you have different lenght
numbers they can enter. So, I would accept their input into a string.
Validate that all characters are numeric. Break up the characters into the
parts I want, load them in.

Untested pseudo type code:

int StrToInt( const std::string& Number )
{
std::stringstring Stream;
Stream << Number;
int Result;
Stream >Result;
return Result;
}

std::string Number;
while ( std::cin >Number && Number != "" )
{
if ( Number.length() != 9 )
{
std::cout << "Input must be 9 digits. Try again.\n";
break;
}
if ( ! isdigits( Number ) )
{
std::cout << "Only numbers 0-9 are allowed. Try again.\n";
break;
}
// at this point we know we have 9 numberic characters in Number
int j = StrToInt( Number.substr( 0, 2 ) );
int k = StrToInt( Number.substr( 2, 3 ) );
int l = StrToInt( Number.substr( 5, 4 ) );

// Do whatever with j, k and l here as integers
}

If you aren't dealing directly with user input but are receiving an int as
input, then I've just been blowing hot air :D

The general rule is: when dealing with user input treat it as
characters/strings as long as possible to validate it. That's the rule I
follow anyway.
May 5 '07 #7
On May 6, 3:42 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"panther" <tharaka...@gmail.comwrote in message

news:11**********************@p77g2000hsh.googlegr oups.com...
Hi,
I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

It depends. It sounds to me like this is some type of part number with
different positions specifying different things. I once did this in a
retail system and realized when they ran out of space in the 2 byte int that
I should of use strings so they could use characters.

Anyway, it depends mostly becuase you said "if user enters..." which means
you are dealing with user input. I find the best way to deal with user
input is by characters, by strings. Once you get it in a string you can see
exactly what they entered.

That is, what if they etenered
071120921
when that gets put into an int it becomes
71120921
And now you have more of a headache, especially if you have different lenght
numbers they can enter. So, I would accept their input into a string.
Validate that all characters are numeric. Break up the characters into the
parts I want, load them in.

Untested pseudo type code:

int StrToInt( const std::string& Number )
{
std::stringstring Stream;
Stream << Number;
int Result;
Stream >Result;
return Result;

}

std::string Number;
while ( std::cin >Number && Number != "" )
{
if ( Number.length() != 9 )
{
std::cout << "Input must be 9 digits. Try again.\n";
break;
}
if ( ! isdigits( Number ) )
{
std::cout << "Only numbers 0-9 are allowed. Try again.\n";
break;
}
// at this point we know we have 9 numberic characters in Number
int j = StrToInt( Number.substr( 0, 2 ) );
int k = StrToInt( Number.substr( 2, 3 ) );
int l = StrToInt( Number.substr( 5, 4 ) );

// Do whatever with j, k and l here as integers

}

If you aren't dealing directly with user input but are receiving an int as
input, then I've just been blowing hot air :D

The general rule is: when dealing with user input treat it as
characters/strings as long as possible to validate it. That's the rule I
follow anyway.
Hi,

Thanks for all of you. I get the idea Jim. I beleive that is the way I
have to follow. Because i have to face the situations as you explain
their.
Again Thanks for you all.


May 6 '07 #8
Hi,
now i have a new problem.
Here is my codes.

using namespace std;
int main()
{

string number,number1,number2,number3;
cout << "Please enter your National ID Number :";
cin >number;
while (number != "" ){

if (number.length() != 9)
{
cout <<"Input must be 9 digits. Try again.\n ";
break;
}

// if (! isdigit(number) )
// {
// cout <<" Only numbers 0-9 are allowed. Try again\n";
// break;
// }

else
{

number1 = number.substr(0,2);
number2 = number.substr(2,3);
number3 = number.substr(5,4);
int i = atoi(number1.c_str());
int j = atoi(number2.c_str());
int k = atoi(number3.c_str());
cout <<i<<endl;
cout <<j<<endl;
cout <<k<<endl;
break;
}
}

return 0;

}

The problem is isdigit() not work with the string. It for the
character. So how do i check the contain of the string?
Please help me.

May 8 '07 #9
panther wrote:
Hi,
now i have a new problem.
Here is my codes.

[..]
The problem is isdigit() not work with the string. It for the
character. So how do i check the contain of the string?
Two ways. One is to enumerate all characters and call 'isdigit' for
each of them. The other is to try to convert the string into some
large enough number. If it succeeds, you got numbers only.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '07 #10
panther wrote:
Hi,
now i have a new problem.
Here is my codes.
Don't use tabs for indents.
using namespace std;
int main()
{

string number,number1,number2,number3;
cout << "Please enter your National ID Number :";
cin >number;
while (number != "" ){
What do you think this while() does? How will number ever change?
>
if (number.length() != 9)
{
cout <<"Input must be 9 digits. Try again.\n ";
break;
}
How will the user "try again"? Restart the entire program? That doesn't
seem very useful.
// if (! isdigit(number) )
// {
// cout <<" Only numbers 0-9 are allowed. Try again\n";
// break;
// }

else
{

number1 = number.substr(0,2);
number2 = number.substr(2,3);
number3 = number.substr(5,4);
int i = atoi(number1.c_str());
int j = atoi(number2.c_str());
int k = atoi(number3.c_str());
cout <<i<<endl;
cout <<j<<endl;
cout <<k<<endl;
break;
}
}

return 0;

}

The problem is isdigit() not work with the string. It for the
character. So how do i check the contain of the string?
A string is composed of a group of characters, in sequential order. You
have a testing function that takes one character. Hmmmm, what should we
do? Perhaps, iterate over the string, checking each character? How
would you do that?


Brian

May 8 '07 #11
"panther" <th********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
Hi,
now i have a new problem.
Here is my codes.

using namespace std;
int main()
{

string number,number1,number2,number3;
cout << "Please enter your National ID Number :";
cin >number;
while (number != "" ){

if (number.length() != 9)
{
cout <<"Input must be 9 digits. Try again.\n ";
break;
}

// if (! isdigit(number) )
// {
// cout <<" Only numbers 0-9 are allowed. Try again\n";
// break;
// }

else
{

number1 = number.substr(0,2);
number2 = number.substr(2,3);
number3 = number.substr(5,4);
int i = atoi(number1.c_str());
int j = atoi(number2.c_str());
int k = atoi(number3.c_str());
cout <<i<<endl;
cout <<j<<endl;
cout <<k<<endl;
break;
}
}

return 0;

}

The problem is isdigit() not work with the string. It for the
character. So how do i check the contain of the string?
Please help me.
Untested psuedo code again.

bool IsStringDigits( const std::string& Input ) // If isdigit doesn't
accept const char, remove const.
{
for ( size_t i = 0; i < Input.length(); ++i )
if ( ! isdigit( Input[i] ) )
return false;
return true;
}
May 9 '07 #12

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

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
2
by: Shelley | last post by:
Hi everyone, I am searching a way to read part of the data in the text field in a form. As the length of the field is dynamic, I can't use the left or right function. I need to read the first 2...
3
by: Programmer | last post by:
Hi all I wan't to know if i'm able to read mail from a mail server. My mail server is a pop3 server (UNIX) and i want to be able to get the mails from an aspx or an asmx. with out using external...
8
by: Stacey | last post by:
I've been trying for the last couple of days to read some pixel values from a window of an independent application, but, even though I got close, I just can't figure the last part out. I am able...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
6
by: kimiraikkonen | last post by:
Hi, Sorry if this is so basic but i wanted to know the correct way of displaying a value's "only" integer part. I mean; if i declare a value as integer which is actually a decimal number, it...
1
Oodles Of Noodles
by: Oodles Of Noodles | last post by:
Hello fellow geeks I have a problem in my database iVB .Net program that is generating 'Error:Invalid attempt to read when no data is present.' The weird part is that when you call the page from...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.