简体   繁体   中英

C/C++ function not calling a switch structure properly

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;
}

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;
}

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);

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

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;
}
~

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;
}

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;
}

因为可变选项仅将其值传递给函数question(),所以可变选项的值确实没有改变,因此,也许您应该在函数question()中返回选项的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM