![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 2
Rep Power: 0
![]() |
negative cubed numbers
I just started to learn C and I typed this example out of Sams Teach Yourself C in 21 Days book plus i added a bit so that I can keep on trying different problems. Well the program works great except when I enter some numbers where the last two digits ending in 23 and then i get a negative number (e.g. 1523 shows a value of -762324629 or 1323 shows a value of -1979282029). I was wondering if it possibly had to do with the processor in some way because i dont see how the code could screw it up like that.
//Demonstrates a simple function
#include <stdio.h>
#define NO 0
#define YES 1
long cube(long x);
long input, answer;
int choice = YES;
int main (void)
{
while(choice != NO)
{
printf("Enter an interger value: ");
scanf("%d", &input);
answer = cube(input);
printf("The cube of %ld is %ld.\n", input, answer);
printf("Would you like to cube another number?(1 for yes 0 for no): ");
scanf("%d", &choice);
printf("\n");
}
return 0;
}
//Function: cube() - Calculates the cubed value of a variable
long cube(long x)
{
long x_cubed;
x_cubed = x*x*x;
return x_cubed;
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
Welcome to the forums.
Great choice on the book, I read it about 6 months ago. The first thing I noticed is this: int choice = YES; Another important thing that the book dosen't teach you until later on: scanf("%d", &input);
rewind(stdin); |
|
|
|
|
|
#3 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
Quote:
If you look at his code, you can see this: #define NO 0 #define YES 1 ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Oh, didn't see that. :o
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
A signed long variable in 32 bit C can only hold a value up to 2,147,483,647 (2^31 - 1)
1523^3 is 3,532,642,667, which is too big. This causes the value to overflow and become negative. To fix this, you could try using a 64 bit integer. Unfortunately, the way to do this is different depending on your compiler (e.g. __int64 for Visual C, long long for gcc). Alternatively, you could use floating point (e.g. double). This gives you a greater range, but possibly at the expense of accuracy. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2006
Posts: 2
Rep Power: 0
![]() |
Hey thanks for all of the replies. I changed around the variable type and that fixed it.
//Demonstrates a simple function
#include <stdio.h>
#define NO 0
#define YES 1
long long cube(long long x);
long input;
long long answer;
int choice = YES;
int main (void)
{
while(choice != NO)
{
printf("Enter an interger value: ");
scanf("%d", &input);
answer = cube(input);
printf("The cube of %ld is %lld.\n", input, answer);
printf("Would you like to cube another number?(1 for yes 0 for no): ");
scanf("%d", &choice);
printf("\n");
}
return 0;
}
//Function: cube() - Calculates the cubed value of a variable
long long cube(long long x)
{
long long x_cubed;
x_cubed = x*x*x;
return x_cubed;
}Last edited by kretchpoof; Feb 3rd, 2006 at 10:10 PM. |
|
|
|
|
|
#7 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just to highlight for Navid:
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|