Results 1 to 7 of 7

Thread: Making numbers with pow()

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99

    Making numbers with pow()

    I have a 4 positon array with characters (numbers) stored in it. For example 49, 50, 51, 52...which would be 1,2,3,4 respectively. What I want to do is build the number 1234 out of the array. This is the code I have so far and it doesn't seem to wanna work.
    PHP Code:
    j=iFieldSz;
         for(
    i=0;i<iFieldSz;i++) {
              
    itemp = (int)(number[i] - 48);
              
    inumber += itemp pow(10.0,(double)j);
              
    j--;
         }
         
    printf("%d",inumber); 
    any help would be great

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Your code worked for me once I set inumber to 0 to start and took the loop down one cycle. What problem are you having? What's the output?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    I end up an inumber of -209399982

    here is the entire function, maybe it will help clear things up a bit more

    PHP Code:
    int getint(int iFieldSz) {

         
    char number[4];
         
    int inumber=0,bvalid=0,ipos=0,i,itemp,j;
         
    char c;

         for(
    i=0;i<4;i++)
              
    number[i] = 0;
         
    fflush(stdin);
         do {
              
    getch();
              if (
    strlen(number) < iFieldSz && isdigit(c)) {
                   
    printf("%c"c);
                   
    number[ipos++] = c;
              }
              if (
    == '\\b' && ipos 0) {
                   
    printf ("\\b \\b");
                   
    number[--ipos] = '\0';
              }
              if (
    == '\r' && strlen(number) > 0)
                   
    bvalid 1;
         } while (! 
    bvalid);
         
    j=iFieldSz;
         for(
    i=0;i<iFieldSz;i++) {
              
    itemp = (int)(number[i] - 48);
              
    inumber += itemp pow(10.0,(double)j);
              
    j--;
         }
         
    printf("%d",inumber);
         return 
    inumber;


    thanks a lot,
    Steve

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    hmm well it works fine for me again. There are two problems I see, the first one might be your problem.

    #1 - You are hard coding iFieldSz & number[] which is fine but you are not stoping your loop if 4 is being reached. Also what is the point of iFieldSz?
    #2 - You are getting an extra 0 on your number at the end because you have j set to 4 but i starts at 0 so you need to take one away from j.

    Here is the code I used:
    PHP Code:
    #include <iostream>  //preprocessor statement
    using namespace std;  //choosing the namespace that we are going to use
    #include <conio.h> 
    #include <math.h>
    #include <stdio.h>

    /*
        This is our main function, everything
        starts running here.
    */
    int getint(int iFieldSz) {

         
    char number[4];
         
    int inumber=0,bvalid=0,ipos=0,i,itemp,j;
         
    char c;

         for(
    i=0;i<4;i++)
              
    number[i] = 0;
         
    fflush(stdin);
         do {
              
    getch();
              if (
    strlen(number) < iFieldSz && isdigit(c)) {
                   
    printf("%c"c);
                   
    number[ipos++] = c;
              }
              if (
    == '\b' && ipos 0) {
                   
    printf ("\b \b");
                   
    number[--ipos] = ' ';
              }
              if (
    == '\r' && strlen(number) > 0)
                   
    bvalid 1;
         } while (! 
    bvalid);
         
    j=iFieldSz-1;
         for(
    i=0;i<iFieldSz;i++) {
              
    itemp = (int)(number[i] - 48);
              
    inumber += itemp pow(10.0,(double)j);
              
    j--;
         }
         
    printf("\r\n%d",inumber);
         return 
    inumber;

    }

    int main()
    {
        
    int q getint(4);

        return 
    0// quit back to the OS


    My input/output
    3452
    3452Press any key to continue
    q = 3452
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    What complier are you using for that? I have Dev C++ 4...i'm starting to think that it maybe the problem

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    MSVC 7
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Even DevC++4 should be able to compile this correctly.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width