user3055015
user3055015

Reputation: 79

Cannot get function in C to execute properly

I have a simple function that I am trying to create. I need this function to add 6 to whatever number the user decides to enter into the program. I have been working with this code for an hour now and cannot figure out what I'm doing wrong, even after looking at multiple examples from the course I am taking. I really appreciate the help.

Here is my code:

#include <stdio.h>

void closing(void);
void addSix(void);
int x;
int result;

int main()
{
    int x;
    printf("Please enter a number to add to 6: ");
    scanf("%d", &x);
    getchar();

    addSix();


    closing();
    closing();



    return 0;
}

void closing(void)
{
    printf("That's all folks.\n");
}

void addSix(void)
{
    int result = x+6;
    printf("Result: %d\n", result);
}

Upvotes: 1

Views: 99

Answers (4)

LZR
LZR

Reputation: 958

You have a global x, and a local x variable in which you actually save the user input. Using only the global variable will do the trick for you

#include <stdio.h>

int x;

void addSix(void)
{
    int result = x+6;
    printf("Result: %d\n", result);
}


int main()
{
    printf("Please enter a number to add to 6: ");
    scanf("%d", &x);
    getchar();

    addSix();

    return 0;
}

Upvotes: 0

master_latch
master_latch

Reputation: 444

You have x declared as both a global variable and variable local to main. When you pass x by pointer to scanf, it refers to the local variable, not the global.

You can remove the local declaration of int x in main but this isn't really the best solution. Generally, global variables should be avoided when possible (though they are, of course, sometimes necessary/the best tool for the job).

The best solution in this case is to make x a parameter to addSix(). There are a few options here:

  • You can have addSix return the sum and then use that return value
  • You can pass the address of x to addSix and have the function modify x itself by using a pointer.

The former would look like this:

int addSix(int x) {
    return x + 6;
}

The latter would look like this:

void addSix(int * x) {
    *x += 6;
}

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 613491

You have two variables named x. One is a local to main, and the other is a global variable.

The function addSix cannot see the local in main. It can only see the global variable.

You should change addSix so that it is passed the value as a parameter.

void addSix(int x)
{
    printf("Result: %d\n", x+6);
}

Call the function like this:

addSix(x);

Or perhaps you want your function to return a value:

int addSix(int x)
{
    return x+6;
}

Which you can call like this:

int result = addSix(x);

Both of your global variables are needless. Remove them.

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 362087

You've got both a global x variable outside of main(), and a local x inside of main(). The code inside of main() writes to the local x while the code in addSix() reads from the global x.

Remove the int x declaration in main() so that both places access the global x.

Upvotes: 2

Related Questions