I can't seem to "return" a value that is stored in "diff". I am suppose to give the value of diff to "int main()" to activate the void low() or void high() functions. But the program I did skips

difficulty(diff);
    if (diff == 'l')
    {low();}
    else if (diff == 'h')
    {high();}

This is my codes (I removed the function random() because it is not gonna be "bumped" and my problem is mainly on how to transfer the value from a function to activate other functions.

int difficulty(char diff)
{
    char difficulty;
    printf("Level of difficulty? (l-Low,h-High) - ");
    scanf("%c",&difficulty);
    while(difficulty != 'l' && difficulty != 'h')
    {                 
    scanf("%c",&difficulty);
    }
    if (difficulty == 'l')
    {printf("WEAK! :3\n");
    diff = 'l';}
    if (difficulty == 'h')
    {printf("PRO! XD\n");
    diff = 'h';}
    return diff;
}
void low()
{
     printf("low");
}
void high()
{
     printf("high");
}
int main()
{
    char diff;
    difficulty(diff);
    if (diff == 'l')
    {low();}
    else if (diff == 'h')
    {high();}
    random();
system("pause");
}

Any help will be appreciated.

When you pass a variable to a function as you have declared you are dealing with a copy of that variable within the body of the function. If you want to be able to modify the passed in variable you will need to pass a pointer to it. Something like the following:

void foo(char * c) {
   *c = 'x'; // This actually sets the original variable value through the pointer
}

int main () {
   char c = 'y';
   foo (&c); // pass the address of the variable, not the variable itself
   // at this point c == 'x' is a true statement
   ...
}

Delete line 8, put lines 4 and 5, where line 8 is now. Right after the scanf() line, add this:

getchar();

//new line 5:
difficulty = '0';

And you don't need to assign diff in two places. Just assign it once, just before you return from the difficulty function.

I'm not sure what the whole "weak" "pro", and "low", "high" print out to the user is for. Either use one, or the other, but both seems quite redundant.

If you indent your subordinate code lines, your code will be easier to troubleshoot, by far.

One more thing - your choices are just two, difficulty and diff, being either l or h. So just

if(diff=='l') {
  //do something for low diff here
}else {
  //do something for high diff here
}

Edited 5 Years Ago by Adak: n/a

@L7Sqr Thanks! my program works now :).
@Adak The Low ,High is actually just examples cause I tried to minimize the code as much as possible :)

This question has already been answered. Start a new discussion instead.