Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 3rd, 2006, 7:48 PM   #1
kretchpoof
Newbie
 
Join Date: Feb 2006
Posts: 2
Rep Power: 0 kretchpoof is on a distinguished road
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;
}
kretchpoof is offline   Reply With Quote
Old Feb 3rd, 2006, 8:15 PM   #2
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
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;
The variable choice is declared to be an interger (int). An integer is a whole number, and you are assigning text to it. I suggest assigning a number to your the variable choice to condition your while loop for now.

Another important thing that the book dosen't teach you until later on:
scanf("%d", &input);
rewind(stdin);
Whenever the user is requested for input, the input is kept in the buffer, which will cause problems later on for other additional inputs. So make sure to clear out the stdin with rewind(stdin).
Navid is offline   Reply With Quote
Old Feb 3rd, 2006, 8:20 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Navid
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;
The variable choice is declared to be an interger (int). An integer is a whole number, and you are assigning text to it. I suggest assigning a number to your the variable choice to condition your while loop for now.

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!
Jessehk is offline   Reply With Quote
Old Feb 3rd, 2006, 8:22 PM   #4
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
Oh, didn't see that. :o
Navid is offline   Reply With Quote
Old Feb 3rd, 2006, 8:51 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Feb 3rd, 2006, 9:55 PM   #6
kretchpoof
Newbie
 
Join Date: Feb 2006
Posts: 2
Rep Power: 0 kretchpoof is on a distinguished road
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.
kretchpoof is offline   Reply With Quote
Old Feb 4th, 2006, 7:34 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just to highlight for Navid:
Quote:
int choice = YES;
The lack of quotation marks is a hint that there might be a preceding definition of this token as a number.
Quote:
Whenever the user is requested for input, the input is kept in the buffer, which will cause problems later on for other additional inputs.
This is just flat not true. Only input that is not requested or that input which fails to meet the specifications of the specified input mechanism will be left in the buffer. Read the documentation for the particular function to determine what might be automatically discarded, used, or left in the buffer. Incidentally, one can dispense with the buffer in most cases if one desires.
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC