![]() |
|
[C] [C] What's wrong with my code? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] [C] What's wrong with my code? (/showthread.php?tid=4264658) |
[C] What's wrong with my code? - .eXe - Jun 3, 2014 Hi, I've been practicing C for a couple of days and I'm trying to get my code to read number from a console then perform a function. Here's my code: Code #include <stdio.h>But, it's thinking I didn't enter 5. * - _SLQ - Jun 3, 2014 Try that. Code (number == '5')Read the return value. http://www.cplusplus.com/reference/cstdio/getchar/ Your ANSI value for the number 5 is not the same as the digit 5. However, it's recommended you use scanf(); int x = 0; scanf("%d", &x); Spoiler (Click to View) * - .eXe - Jun 3, 2014 (Jun 3, 2014 04:49 AM)_SLQ Wrote: Try that. Yes! Thank you. By the way, is it best to declare a function like the one I just used as "int dofunction()" or "void dofunction()"? * - _SLQ - Jun 3, 2014 (Jun 3, 2014 04:50 AM)Evil.eXe Wrote: Yes! Thank you. By the way, is it best to declare a function like the one I just used as "int dofunction()" or "void dofunction()"? Depends on the purpose... For such a short program, you might as well do the printing inside your if statement rather than spawning a new function. * - .eXe - Jun 3, 2014 (Jun 3, 2014 04:52 AM)_SLQ Wrote: Depends on the purpose... I was just practicing functions, I know for something like this they aren't necessary, I'm just trying to get used to it. * - MrPip - Jun 3, 2014 I looked at how you coded all of that just for a simple task, and I wondered. Dude use the scanf function please. * - Goodies - Jun 3, 2014 (Jun 3, 2014 10:44 AM)Gibsons Wrote: I looked at how you coded all of that just for a simple task, and I wondered. Dude use the scanf function please. Or how about not because it's inherently insecure? Use a more secure version. OT: This may very well not be the best way, but I don't think it's bad and I know it'll work. PHP Code * - tux1 - Jun 3, 2014 (Jun 3, 2014 04:50 AM)Evil.eXe Wrote: Yes! Thank you. By the way, is it best to declare a function like the one I just used as "int dofunction()" or "void dofunction()"? A function always uses this structure: Quote:return-type function-name(parameter declarations, if any) So the first part will dictate what the function returns. In this case you could use void because it doesn't return any useful information. You could use int to return 0 because 0 is an integer. |