Hack Forums
[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>
#include <windows.h>
#include <unistd.h>

char dofunction() {
    printf("You entered 5!");
    return 0;
}

int main(void) {
    printf("enter 5:\n");
    int number;
    number = getchar();

    if (number == 5) {
            dofunction();
    } else if (number != 5) {
        printf("you didn't enter the number...");
    }

}

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.

Code
(number == '5')

Spoiler (Click to View)

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

For such a short program, you might as well do the printing inside your if statement rather than spawning a new function.

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

<?php 
#include <stdio.h>
#include <windows.h>
#include <unistd.h>

void dofunction(){
   
printf("You entered 5!\n");
}

int main(void){
   
printf("Enter 5: ");
   
long number;
   
char *num = calloc(1, sizeof(long));
   
number = strtol(fgets(num, 10, stdin), NULL, 10);
   if (
number == 5)
      
dofunction();
   else
      exit(
fprintf(stderr, "You didn't enter the number..\n"));
} 



* - 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)
{
declarations
statements
}


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.