C 如何从函数跳回main?
我需要从函数create_task跳回到main。我标记了我想跳到的地方和从哪里跳到的地方。 是否有逻辑,或者我们可以使用内置函数?goto语句不起作用。调用main函数将停止程序C 如何从函数跳回main?,c,function,C,Function,我需要从函数create_task跳回到main。我标记了我想跳到的地方和从哪里跳到的地方。 是否有逻辑,或者我们可以使用内置函数?goto语句不起作用。调用main函数将停止程序 #include<stdio.h> #include<string.h> struct task{ char t_name[99]; char task_noted[999]; }; void create_task(struct task *arrayt ); int main(){ st
#include<stdio.h>
#include<string.h>
struct task{
char t_name[99];
char task_noted[999];
};
void create_task(struct task *arrayt );
int main(){
struct task arrayt[999];
//i want to get here!!!
int check=0;
while(check>3||check<1){
printf("Enter 1 to create a task\nEnter 2 to delete a task\nEnter 3 to veiw task:\n");
scanf("%d",&check);
}
switch(check){
case 1:
printf("CREATING A TASK\n");
create_task(arrayt);
break;
case 2:
printf("DELETE TASK");
break;
case 3:
printf("VEIW TASKS");
break;
default :
printf("INVALID OPTION!!!!");
break;
}
}
void create_task(struct task *arrayt ){
int i=0,want=0;
FILE *ptr;
while(i!=999){
if(arrayt[i].t_name!= NULL && arrayt[i].task_noted!= NULL){
ptr = fopen("data.txt","a");
printf("Enter Task Name: ");
scanf("\n%[^\n]s",arrayt[i].t_name);
fprintf(ptr,"\n%s",arrayt[i].t_name);
printf("\nEnter Task Data:\n");
scanf("\n%[^\n]s",arrayt[i].task_noted);
fprintf(ptr,"\n%s",arrayt[i].task_noted);
i++;
printf("\nDo you want to add another task?\nPRESS 1 FOR ANOTHER\nPRESS 2 TO EXIT\n");
scanf("%d",&want);
if(want==2){
//i want to goto main from here!!!!
}
fclose(ptr);
}
}
}
#包括
#包括
结构任务{
字符t_名称[99];
char task_注意到[999];
};
void create_任务(结构任务*arrayt);
int main(){
结构任务数组[999];
//我想去这里!!!
整数检查=0;
while(check>3 | | check您应该使用该语句,而不是gotos或类似的语句:
if (want == 2) {
return;
}
在main的begining处添加一个if语句,bool设置为false,当want为2时,将其设置为true
我需要从函数create_task
跳回到main
另一种可能是小心地使用longjmp
。
当心,它们会让人困惑(因为longjmp
正在缩小你的页面)并且可能会使你的代码不可读。请确保对你的代码进行更多的文档记录和注释。你可能需要多用一点,或者用\n
结束你的printf
格式控制字符串。另请参阅
根据经验,longjmp
应该很少使用。它是C语言中的一种。
困难的问题可能是“撤销”在setjmp
和相应的longjmp
之间产生的重要副作用(特别是),在这种情况下,您需要一些好的方法
似乎是错误的(因为在您的代码中您忘记了测试ptr
)。失败时会发生什么情况(在其他系统中运行程序时会发生这种情况)?对的进一步调用可能会崩溃。请注意
我建议对printf(“删除任务”);
进行编码,这样可以避免缓冲并在终端中更早地显示该消息
一个相关的概念是and(在C中,with)
请务必阅读并查看网站(以及稍后的一些C草案标准,例如)。还要阅读C编译器(例如)和调试器(例如)的文档。我建议使用gcc-Wall-Wextra-g
编译代码(所有警告和调试信息),然后使用GDB来了解其行为。在Linux上,您也可以使用。
- 如果您正在考虑使用跳转语句,如
goto
、break
和continue
,它们将无法帮助您从一个函数跳转到另一个函数。这是因为这两个函数的作用域不同。如果希望将控件返回到main()中,可以使用return语句
- 如果您声明函数
void function(…)
,那么您只需在任意点返回,或者允许控制从函数的末尾运行—编译器将自动返回调用函数
- 如果将函数声明为返回值
int函数(…)
,则必须返回XXX
,并且XXX
类型必须与函数的返回类型匹配
您的问题并不是想“跳回”到main
。您想要的是有一个循环来读取和处理用户输入。为此,请使用while
语句创建一个循环:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Put needed declarations here.
while (1)
{
// Prompt user and read input.
printf("Enter 1 to create a task.\n...\nEnter 0 to stop.\n");
int check;
int result = scanf("%d", &check);
// Test whether scanf worked. End the program if it failed.
if (result != 1)
{
fprintf(stderr, "scanf failed.\n");
exit(EXIT_FAILURE);
}
// If the user entered 0 to stop the loop, stop the loop.
if (check == 0)
break;
// Dispatch based on the user's request.
switch (check)
{
case 1:
// Do stuff for case 1.
break;
case 2:
// Do stuff for case 2.
break;
case 3:
// Do stuff for case 3.
break;
// Handle incorrect input from user.
default:
printf("Error, %d is not a valid choice.\n", check);
break;
}
}
}
#包括
#包括
内部主(空)
{
//把需要的声明放在这里。
而(1)
{
//提示用户并读取输入。
printf(“输入1以创建任务。\n...\n输入0以停止。\n”);
整数检查;
int result=scanf(“%d”,&check);
//测试scanf是否工作。如果失败,则结束程序。
如果(结果!=1)
{
fprintf(stderr,“scanf失败。\n”);
退出(退出失败);
}
//如果用户输入0以停止循环,请停止循环。
如果(检查==0)
打破
//根据用户的请求进行调度。
开关(检查)
{
案例1:
//为案例1做些事情。
打破
案例2:
//为案例2做些事情。
打破
案例3:
//为案例3做些事情。
打破
//处理来自用户的错误输入。
违约:
printf(“错误,%d不是有效的选择。\n”,请检查);
打破
}
}
}
我知道我没有为测试ptr设置条件。我只是希望它能够顺利运行,然后我会补充。感谢您指出您是一名非常优秀的程序员。您知道setjmp正在使用大锤敲打鸡蛋,问题是组织而不是流控制。我当然知道这一点。
printf("DELETE TASK");
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Put needed declarations here.
while (1)
{
// Prompt user and read input.
printf("Enter 1 to create a task.\n...\nEnter 0 to stop.\n");
int check;
int result = scanf("%d", &check);
// Test whether scanf worked. End the program if it failed.
if (result != 1)
{
fprintf(stderr, "scanf failed.\n");
exit(EXIT_FAILURE);
}
// If the user entered 0 to stop the loop, stop the loop.
if (check == 0)
break;
// Dispatch based on the user's request.
switch (check)
{
case 1:
// Do stuff for case 1.
break;
case 2:
// Do stuff for case 2.
break;
case 3:
// Do stuff for case 3.
break;
// Handle incorrect input from user.
default:
printf("Error, %d is not a valid choice.\n", check);
break;
}
}
}