![]() |
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:
code of the program Quote:
|
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:
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 |
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 Learn your lesson, now! |
Quote:
|
there is such a thing as a simple c question
news to me |
Quote:
|
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); |
wow thanks for all the help.
ill be sure to come back for more help. |
All times are GMT -8. The time now is 09:44 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project