Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   Simple C question (https://thetfp.com/tfp/tilted-technology/44257-simple-c-question.html)

Dilbert1234567 02-03-2004 10:32 PM

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");




}


juanvaldes 02-03-2004 11:00 PM

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

KnifeMissile 02-03-2004 11:45 PM

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!

n0nsensical 02-04-2004 12:18 AM

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.

dragon2fire 02-04-2004 01:01 AM

there is such a thing as a simple c question



news to me

KnifeMissile 02-04-2004 02:00 AM

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...

n0nsensical 02-04-2004 12:50 PM

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);

Dilbert1234567 02-04-2004 02:47 PM

wow thanks for all the help.

ill be sure to come back for more help.


All times are GMT -8. The time now is 03:36 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, 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 63 64 65 66 67 68 69 70 71 72 73