Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-03-2004, 10:32 PM   #1 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
Simple C question

the general purpose is to enter 3 variables and manipulate them the problem is that the program compiles with 0 errors but crashes when i enter a variable.

i get an aplication error (pop up box with the red X)


error:
Quote:
the instruction at "0x004021b8" references memory at 0x006e0065". The memory could not be "written".

click on ok to terminate the program
click cancel to debug the program.


code of the program

Quote:
#include <stdio.h>

main(void)
{
int nRoomLength=0, nRoomWidth=0, nRoomHeight=0;

double dWidthToLength=0 ; /* width/length */

short int nRoomVolume=0 ; /* nRoomWidth * nRoomLength * nRoomHeight*/

unsigned int nRoomArea=0 ; /* nRoomWidth * nRoomLength */



printf("Please enter a value for the room width:");
scanf("%d, &nRoomLength");
printf("Please enter a value for the room length::");
scanf("%d, &nRoomWidth");
printf("Please enter a value for the room height::");
scanf("%d, &nRoomHeight");

dWidthToLength = nRoomWidth / nRoomLength;
nRoomArea = nRoomLength * nRoomWidth;
nRoomVolume = nRoomLength * nRoomWidth * nRoomHeight;
printf("The area of the floor of the room is: %d, nRoomArea");

printf("The volume of the room is: %d, nRoomVolume");

printf("The ratio of the width to the length is: %d, dWidthToLength");




}
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
Old 02-03-2004, 11:00 PM   #2 (permalink)
Banned
 
Location: shittown, CA
You had your quotes on your scanf's in the wrong place. It needs to go:

scanf("format strings", variables);
like so:
scanf("%d", &bob);

Simple typo bug.

fixed code:
Quote:
#include <stdio.h>

main(void)
{
int nRoomLength=0, nRoomWidth=0, nRoomHeight=0;

double dWidthToLength=0 ; /* width/length */

short int nRoomVolume=0 ; /* nRoomWidth * nRoomLength * nRoomHeight*/

unsigned int nRoomArea=0 ; /* nRoomWidth * nRoomLength */

printf("Please enter a value for the room width:");
scanf("%d", &nRoomLength);
printf("Please enter a value for the room length::");
scanf("%d", &nRoomWidth);
printf("Please enter a value for the room height::");
scanf("%d", &nRoomHeight);

dWidthToLength = nRoomWidth / nRoomLength;
nRoomArea = nRoomLength * nRoomWidth;
nRoomVolume = nRoomLength * nRoomWidth * nRoomHeight;
printf("The area of the floor of the room is: %d", nRoomArea);

printf("The volume of the room is: %d", nRoomVolume);

printf("The ratio of the width to the length is: %d", dWidthToLength);
}
and it looks like you have a bug or two left.

bob:~ johnwort$ ./out
Please enter a value for the room width:50
Please enter a value for the room length::50
Please enter a value for the room height::42
The area of the floor of the room is: 2500The volume of the room is: -26072The ratio of the width to the length is: 1072693248

Last edited by juanvaldes; 02-03-2004 at 11:03 PM..
juanvaldes is offline  
Old 02-03-2004, 11:45 PM   #3 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
Ah, the glory/shame of scanf() comes to light!

In many ways, scanf() is a very poor function to use because of it's ease of destruction (just as you've pointed out), it's lack of error reporting, and general lack of robustness.

It's generally advised to use gets() to get the input string and then parse it yourself, with a combination of conversion functions, like atoi() and atof()...

Anyway, your problem, specifically, is that you're not supplying a variable assignment. You're just supplying a format string and that's bad!
Code:
scanf("%d, &nRoomLength");  // this is what you are doing
scanf( "%d", &nRoomLength);  // this is what you should be doing...
The reason why the compiler can't give you an error, or even a warning, is because of the variable arguments mechanism used by scanf() and printf(). It would take me a while to explain exactly why the compiler can't just "know" how to interpret the variable parameters (in both number and type!) but if you're interested, I'll be back to tell you an interesting story. Rest assured, this is no simple typo error. This is a seriously twisted "feature" of C and should always be looked upon with dear caution...

Learn your lesson, now!
KnifeMissile is offline  
Old 02-04-2004, 12:18 AM   #4 (permalink)
Junkie
 
Location: San Francisco
Quote:
Originally posted by KnifeMissle
It's generally advised to use gets() to get the input string and then parse it yourself, with a combination of conversion functions, like atoi() and atof()...
gets is no good either, unless you like buffer overflows. =P getline is the one to use if you must use C arrays for strings.
n0nsensical is offline  
Old 02-04-2004, 01:01 AM   #5 (permalink)
Junkie
 
there is such a thing as a simple c question



news to me
dragon2fire is offline  
Old 02-04-2004, 02:00 AM   #6 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
Quote:
Originally posted by n0nsensical
gets is no good either, unless you like buffer overflows. =P getline is the one to use if you must use C arrays for strings.
D'oh, you're right! You do risk buffer overflows with gets() but, unless you're talking about a different getline() function that I'm not currently aware of, gets() is still his best bet. Unless the April 2001 MSDN is lying to me, getline() is not available to poor C coders, being a C++ streaming function and all...
KnifeMissile is offline  
Old 02-04-2004, 12:50 PM   #7 (permalink)
Junkie
 
Location: San Francisco
getline is in the GNU C library and is indeed a different function than the STL getline, but since Microsoft apparently does like buffer overflows, it doesn't seem to be in theirs. Maybe it's not standard C, I don't really know since I rarely use straight C and not to get input from the console. But fgets is buffer-safe and so probably the most portable option is to use that with stdin.
char buffer[256];
fgets(buffer, 256, stdin);
n0nsensical is offline  
Old 02-04-2004, 02:47 PM   #8 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
wow thanks for all the help.

ill be sure to come back for more help.
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
 

Tags
question, simple


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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:33 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62