gpt4 book ai didi

C 语言中 if else 函数后的代码中断

转载 作者:行者123 更新时间:2023-11-30 21:09:46 25 4
gpt4 key购买 nike

所以我的代码在我创建的 if else 函数中的结束大括号之后不断中断。它与返回 EXIT_SUCCES 有关系吗?我使用它错误吗?

这是我的代码:

//  recursive.c
// lynda

#include <stdio.h>
#include <stdlib.h>
/*
checks for traffic light if else example
*/
void processColor(char c);
void checkTraficLight(void);
int main(void){
checkTraficLight();
return EXIT_SUCCESS;
}

void checkTraficLight(void){
printf("what is the light? r, y, g: \n");
char color;
scanf("%c", &color);
processColor(color);
}

void processColor(char c){
if (c == 'r') {
printf("color is red");
} else if (c == 'y'){
printf("color is yellow");
} else if(c == 'g'){
printf("color is green");
} else {
printf("U entered an invalid color");
}
}
<小时/>

[评论更新:]

但问题是,它在打印出我想要打印的内容之前停止了,假设我输入“r”,它应该打印出“颜色是红色”,但它只是说“(lldb)并停止,它不打印出“颜色是红色”

最佳答案

我已经在代码块 13.12 上运行了你的代码,假设它是 C++ 代码。它工作正常,没有错误,也没有崩溃。

你的代码结构和第一行的注释让我觉得你想编写一个递归函数。如果是这样,您应该提供有关该功能目标的更多详细信息。

关于C 语言中 if else 函数后的代码中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com