-1

so here in my code no matter how much i change i cant get it to work properly it is supposed to go to question. that includes scanning for a int which corresponds to an option then its supposed to call navigate now with the option declared and work with it but no matter what option you choose it just says sorry

#include <stdio.h>
#include <stdlib.h>

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

void question(int option)

{
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", &option);

}

void navigate(int option)
{
    switch(option)
    {
        case 1:
        printf(OPENWINDOW);
        break;
        case 2:
        printf(OPENWINDOW);
        break;
        case 3:
        printf(OPENWINDOW);
        break;
        case 4:
        printf(OPENWINDOW);
        break;
        default :
        printf("sorry");
        question(option);


    }
}

int main()
{
     int option;    

     question(option);
     navigate(option);    

     return 0;
}
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
Gunr Jesra
  • 110
  • 1
  • 10

7 Answers7

2

Arguments are passed by value, not reference. So, your "option" arg is going to "disappear" soon after the function ends.

If you pass the "reference" to the var then you can use it to fill the caller variable. The following code and example fixes it.

void question(int *option)
{
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", option);
}

Then you call it like this:

int option;
question(&option);
// now you can use option...

Since function can return values, you could also:

int question(void)
{
        int option;
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", &option);
        return option;
}

// ...
int option = question();
// ...

The navigate and main using reference (pointers):

void navigate(int *option)
{
    switch(*option)
    {
        case 1:
          printf(OPENWINDOW);
          break;
        case 2:
          printf(OPENWINDOW);
          break;
        case 3:
          printf(OPENWINDOW);
          break;
        case 4:
          printf(OPENWINDOW);
          break;
        default:
          printf("sorry");
          question(option);    
    }
}

int main(void)
{
     int option;    

     question(&option);
     navigate(&option);    

     return 0;
}
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • (the same "fix" should be applied to the function navigate) – ShinTakezou Jun 06 '13 at 06:41
  • thanks. i made the mistake of learning java as first language and completly confused about pointers thanks – Gunr Jesra Jun 06 '13 at 07:05
  • the program crashes if i apply the fix to navigate why?? @shin takezou – Gunr Jesra Jun 06 '13 at 07:29
  • have you called navigate like this? `navigate(&option);`? have you changed several `option` into `*option`? – ShinTakezou Jun 06 '13 at 07:43
  • i put a pointer on all options in navigate except the calling one – Gunr Jesra Jun 06 '13 at 07:47
  • and also on i added the & on the main navigate one and also on the one inside navigate the application prints a blank line then waits for you to type something else, its looping through scanf now – Gunr Jesra Jun 06 '13 at 07:48
  • 1
    im sorry for wasting this time, i actually turned off the num locks lol sorry we are all humans thanks alot man – Gunr Jesra Jun 06 '13 at 07:49
  • there's no loop, it will just: question for an option, print OPENWINDOW if the option is recognized, otherwise it will print sorry and ask again for an option, then no matter what you will answer, the program will end – ShinTakezou Jun 06 '13 at 07:54
1

You need to pass option as pass-by-reference. Pass the address of option to question() and update there.

Refer the modified code.

void question(int *option)
{
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", option);
}

call the question() as,

question(&option);
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

You are passing the variable option by value question(option)

You should pass option varible by reference

void question(int *option)
{
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", option);

}

void navigate(int *option)
{
    switch(*option)
    {
        case 1:
        printf(OPENWINDOW);
        break;
        case 2:
        printf(OPENWINDOW);
        break;
        case 3:
        printf(OPENWINDOW);
        break;
        case 4:
        printf(OPENWINDOW);
        break;
        default :
        printf("sorry");
        question(option);

}

int main()
{
     int option;    

     question(&option);
     navigate(&option);    

     return 0;
}

For more information regarding this have a look at this link Difference between call by reference and call by value

Community
  • 1
  • 1
GeekFactory
  • 399
  • 2
  • 13
0

You are passing "option" as call by value. Hence whatever you pass to question(). Would be lost.

Either, you return "option" from question() and pass this to navigate().

#include <stdio.h>
#include <stdlib.h>

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

int question()
{       int option;
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", &option);
        return option;

}

void navigate(int option)
{
    switch(option)
    {
        case 1:
        printf(OPENWINDOW);
        break;
        case 2:
        printf(OPENWINDOW);
        break;
        case 3:
        printf(OPENWINDOW);
        break;
        case 4:
        printf(OPENWINDOW);
        break;
        default :
        printf("sorry");
        question(option);


    }
}

int main()
{
int option;

option = question();
navigate(option);

return 0;
}
~
Denny Mathew
  • 1,072
  • 9
  • 13
0

You need to either pass pointer of option to question or return it from the function question.

In your case value of option in main() is not changing when you read it in question(). Update your code as

int question()

{
        int option;

        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", &option);
        return option;
}

int main()
{
    int option;
    option = question(option);
    navigate(option);
    return 0;
}
Rohan
  • 52,392
  • 12
  • 90
  • 87
0

If you do not want to use pass-by-reference, you can use pass-by-value which you are using in your code. It only needs to be implemented properly. You can change your "void question" to return a value by changing the void to "int" and issuing a return statement before the end of question function. Check code below:

#include <stdio.h>
#include <stdlib.h>

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

int question()

{
        printf("What Would You Like To Do?\n");
        printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n");
        scanf("%i", &option);
        return i;
}

void navigate(int option)
{
    switch(option)
    {
        case 1:
        printf(OPENWINDOW);
        break;
        case 2:
        printf(OPENWINDOW);
        break;
        case 3:
        printf(OPENWINDOW);
        break;
        case 4:
        printf(OPENWINDOW);
        break;
        default :
        printf("sorry");
        question(option);
    }
}

int main()
{
int option;
option = question(option);
navigate(option);

return 0;
}
otep
  • 49
  • 2
0

Because the variable option only pass its value into function question(), the variable option's value indeed is unchanged, so, maybe you should return the value of option in the function question()

Charles0429
  • 1,406
  • 5
  • 15
  • 31