?
Solved

reading number

Posted on 2004-10-15
10
Medium Priority
?
225 Views
Last Modified: 2010-04-01
Just a quick question.

How do we convert 10,000 to 10000?
I read the number using char array from a file, and want to copy thearray to an integer variable. Is that possible?
0
Comment
Question by:icysmarty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 12324691
Just the same way you convert "10000" to int representation 10000
if you don't use library calls.

like this:

while(x < array_size)
  num = num * 10;
  num = num + array[x];
  x = x + 1;
}

just add checks to skip commas.
x and num  start at zero
and 'array_size' should be the actual length of the string
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12324728
er,

num = num + (array[x]-'0')

or if you prefer

num = num + charToDigit(array[x])


int charToDigit(char v)
{
  switch(v) {
     default: return 0; break;
     case '0': return 0;
     case '1': return 1;
     case '2': return 2;
     case '3': return 3;
...
  }
}
0
 
LVL 1

Expert Comment

by:RogueAce
ID: 12325303
What's wrong with library calls?

#include <cstdlib> to get atoi and <ctypes> to get isdigit, if I recall correctly.

num=0;
for(unsigned int c=0; c < arraysize; c++){
  if( isdigit( myarray[c] )){
    num*=10;
    num += atoi(myarray[c]);
  }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Expert Comment

by:Mysidia
ID: 12325381
The fact that atoi takes a pointer to a string as its parameter,
and that segment passes an invalid input to atoi()

Moreover, building a string just to use atoi() would be
wasteful, and there's no reason to go through all the
trouble involved.

int num = 0, x = 0;

while(x < array_size)
  if (!isdigit(x)) {
      /* Detect, skip invalid characters,  report such errors as per the needs
          of your situation*/
      continue;
  }
  num = num * 10;
  num = num + (array[x] - '0');
  x = x + 1;
}
0
 

Author Comment

by:icysmarty
ID: 12325491
Does this apply to 10,000 only? What if I have 12,345 and 56,123?
I am doing this

void read_byte( unsigned char byte)
{
    ....
    ....
    if(byte == 44)
    {}
    else
    array[i++] = byte;
}

The problem is I dunno how to make it display as an integer say 12345, instead of digit 1 digit 2 digit 3 digit 4 digit 5.
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 800 total points
ID: 12325571
The answer is that the strategy works for an arbitrary positive integer inside the legitimate range for
integers.

It can trivially be extended to negative integers by handing the -.

Consider input digits
     D0   D1  D2  D3   D4

What does it mean for a digit to be in the  position on the far right?
Why it means that it is multiplied by 10^0

Second position is weighted as 10^1,  Third position from the right
is 10^2, etc.

Ok, What's num equal to after reading all four example digits then?

Well consider Num_[NNN] as a representation of num after the NNNth iterate

The first iterate of the loop is 0  what is Num after that?
Why:
Num_0 = 0*0 + D0

Guess what, it is in general true that for the algorithm:
Num_(i+1) = (Num_i * 10) + D_i+1

And:
Num_1 = Num_0 * 10 + D1 = D0 * 10 + D1
Num_2 = Num_1*10 + D2 = D0*100 + D1*10 + D2
Num_3 = Num_2*10 + D3 = D0*1000 + D1*100 + D2*10 + D3
Num_4 = Num_3*10 + D3 = D0*10000 + D1*1000 + D2*100 + D3*10 + D4

So if you let the four input digits in the example be
                D1 =  5
                D2  = 4
                D3  = 3
                D4   = 2

then the result is

num = 5*1000 + 4*100 + 3*10 + 2


0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12325577
legitimate range of integers can be determined by #includ'ing  <limits.h>

the maximum value is INT_MAX
the minimum value is called INT_MIN
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12325609
Err, oops technically:
 Num_(i+1) = (Num_i * 10) + D_i+1

should be more like
   Num_(i+1) = (Num_(i-1) * 10) + D_i
with initial condition  Num_(-1) = 0        [Since D0 is the first numbered digit]
0
 

Author Comment

by:icysmarty
ID: 12325618
I got the concept and has coded it according to the logic. Thank you.
0
 
LVL 1

Expert Comment

by:RogueAce
ID: 12325641
Hmm, you're right about atoi. I forgot it took a c-style string instead of a character.

The short version of what Mysidia is saying, icysmarty, is that it will work for any reasonably-sized (i.e. under two billion) number.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question