|
-
Dec 3rd, 2002, 03:22 PM
#1
Thread Starter
Lively Member
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
-
Dec 3rd, 2002, 03:53 PM
#2
Frenzied Member
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

-
Dec 3rd, 2002, 03:59 PM
#3
Thread Starter
Lively Member
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 {
c = getch();
if (strlen(number) < iFieldSz && isdigit(c)) {
printf("%c", c);
number[ipos++] = c;
}
if (c == '\\b' && ipos > 0) {
printf ("\\b \\b");
number[--ipos] = '\0';
}
if (c == '\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
-
Dec 3rd, 2002, 04:27 PM
#4
Frenzied Member
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 {
c = getch();
if (strlen(number) < iFieldSz && isdigit(c)) {
printf("%c", c);
number[ipos++] = c;
}
if (c == '\b' && ipos > 0) {
printf ("\b \b");
number[--ipos] = ' ';
}
if (c == '\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

-
Dec 3rd, 2002, 07:02 PM
#5
Thread Starter
Lively Member
What complier are you using for that? I have Dev C++ 4...i'm starting to think that it maybe the problem
-
Dec 3rd, 2002, 07:03 PM
#6
Frenzied Member
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

-
Dec 4th, 2002, 10:02 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|