所以在我的代码中,无论我改变了多少,我都无法让它正常工作 它应该是有问题的。包括扫描对应于选项的int 然后它应该使用声明的选项调用导航并使用它 但无论你选择什么选项,它都会说 遗憾
#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;
}
答案 0 :(得分:2)
参数按值传递,而不是参考传递。因此,你的“选项”arg将在函数结束后很快“消失”。
如果将“引用”传递给var,则可以使用它来填充调用者变量。以下代码和示例修复了它。
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);
}
然后你这样称呼它:
int option;
question(&option);
// now you can use option...
由于函数可以返回值,您还可以:
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();
// ...
导航和主要使用参考(指针):
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;
}
答案 1 :(得分:1)
您需要传递option
作为传递引用。将选项的地址传递给question()并在那里更新。
参考修改后的代码。
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);
}
将问题()称为,
question(&option);
答案 2 :(得分:0)
您正在通过值option
question(option)
您应该通过引用传递option
变量
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;
}
有关此问题的详细信息,请查看此链接Difference between call by reference and call by value
答案 3 :(得分:0)
您正在传递“选项”作为按值调用。因此无论你传递给问题()。会丢失。
或者,你从question()返回“option”并将其传递给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;
}
~
答案 4 :(得分:0)
您需要将option
的指针传递给question
或从函数question
返回。
在您的情况下,option
中main()
的值question()
在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;
}
中读取时未发生变化。将代码更新为
{{1}}
答案 5 :(得分:0)
如果您不想使用pass-by-reference,则可以使用在代码中使用的pass-by-value。它只需要正确实施。你可以改变你的问题&#34;通过将void更改为&#34; int&#34;来返回值并在问题结束前发出一份退货声明。检查以下代码:
#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;
}
答案 6 :(得分:0)
因为变量选项只将其值传递给函数question(),所以变量选项的值确实不变,所以,也许你应该在函数问题中返回option的值()