Problem with types in C

cmpeters

n00b
Joined
Dec 2, 2006
Messages
17
I wrote the following code to convert a number into a "look and say" value. Everything works properly except for one thing. I cannot enter an integer value greater than 2,147,483,647 (the maximum long int). However, I'm expecting an input upto the maximum value of unsigned long int which is 4,294,967,295. When I enter anything greater than 2,147,483,647 I get an overflow error.

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
  unsigned long int inValue;
  int length, temp1, temp2, count;

  printf("Enter a non-negative integer \n");
  scanf("&#37;lu", &inValue);

  length = (int) log10(inValue);

  count = 1;
  while(length >= 0){
    temp1 = (int) inValue/pow(10,length);
    inValue = inValue - (temp1 * pow(10,length));
    temp2 = (int) inValue/pow(10,length-1);

    if(temp1 == temp2 && length != 0){
      count++;
      length--;
      continue;
    }

    printf("%d%d", count, temp1);

    count = 1;
    length--;
  }

  printf("\n");
  return 0;
}

Am I mixing some type values?
 
Doing this with math is the wrong way to go. It's lots easier and faster with character manipulation over string representations of the values.

You don't tell us anything about the platform or tools you're using. What is "an overflow error", and how does it manifest itself on your platform?

LATER: Looks like your casts are bogus.

Code:
temp1 = (int) inValue/pow(10, length);

ends up casting the denominator to int, then dividing by the denominator. I think you meant to cast the result of the division. What you've coded does a signed int divide, when you need the result of an unsigned int divide cast to a signed integer.

But I still don't understand what's causing your runtime error, and need more information from you about diagnosing it.
 
Doing this with math is the wrong way to go. It's lots easier and faster with character manipulation over string representations of the values.

You don't tell us anything about the platform or tools you're using. What is "an overflow error", and how does it manifest itself on your platform?

Sorry, its a Solaris server and the compiler is gcc-3.2.3.

If I input 2147483647 the program outputs the proper information. If I input 2147483648 I get 1-21-11-41-71-41-81-31-61-41-8 , which is wrong... all the way up to 4,294,967,295 which gives me 901-1.

The outputs past the limit increase by one, so I'm guessing its an overflow.

I'm not sure what you mean by character manipulation. Can you explain?

I chose this way because I'm only in my fifth week of class and this is my second c program ever. So I don't know many advanced topics, and my professor doesn't expect us to use what we haven't discussed in class yet.

I guess my question regarding my code is: If I'm specifying the input type as unsigned long int shouldn't I be able to input a value up to 4,294,967,295 and the program should output properly?
 
I guess my question regarding my code is: If I'm specifying the input type as unsigned long int shouldn't I be able to input a value up to 4,294,967,295 and the program should output properly?
Sure. But all the intermediate operations have to be unsigned long, too. See my edit above.

You should get to strings and characters pretty soon; until then, you might play with this program in your spare time as a starting point:

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
  unsigned long int inValue;
  char szTemp[100];
  int n;

  printf("Enter a non-negative integer \n");
  scanf("%lu", &inValue);

  sprintf(szTemp, "%lu", inValue);

  for (n = 0; n < 100 && szTemp[n] != '\0'; n++)
  {
     printf("%d: '%c'\n", n, szTemp[n]);
  }

  return 0;
}
 
I changed the casting mistake and now I can enter 4,294,967,295 and get a proper output.

Thanks for the sample code. I will play around with it.

We briefly discussed strings and c-strings. Then pointers and arrays.

My professor doesn't give many practical examples or explanations. Just a definition, and a line or two of code. So, sometimes its hard to conceptualize or know when to use certain approaches. I guess that's where experience comes in?

Thanks again for the help.
 
Back
Top