Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

[C] What's wrong with my code?

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.
aint my sig design skills sick?
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)
(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()"?
aint my sig design skills sick?
(Jun 3, 2014 04:50 AM).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.
(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.
aint my sig design skills sick?
I looked at how you coded all of that just for a simple task, and I wondered. Dude use the scanf function please.
(Jun 3, 2014 10:44 AM)MrPip 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"));
} 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Jun 3, 2014 04:50 AM).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.

Hey there,
If I have helped you or if you just enjoy my posts, please consider donating bitcoin to me 1DpL2kcxuidEsjt2MMN2AYufcVoXbi3v2X

[Image: FtwuQgr.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by imrankhanDec 28, 2013 06:22 PM 19 599 Dec 28, 2013 06:22 PM
Last Post: Сlassy
[.NET] by L3g1tWa5teNov 20, 2010 03:58 PM 9 124 Nov 20, 2010 03:58 PM
Last Post: xSilent
[C] by aquabatsOct 6, 2009 07:39 AM 4 89 Oct 6, 2009 07:39 AM
Last Post: archangel.dmitry