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 02:05 AM.

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360