本文介绍了修复这个if,else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题似乎在
我做错了什么?有更好的方法吗?
What am I doing wrong? Is there a better way of doing this?
#include <stdio.h> int main(void) { char pname[25]; char Y[1]; char N[1]; char choice[1]; printf("when responding to Yes or No questions use Y/N.\n"); printf("Hello,traveler Welcome to the Castle of Atal...." "What is your name?\n"); scanf("%s", &pname); printf("Greeting's %s What bring's you to the kingdom of Tharnos?\n", pname); printf("I see how intresting do you wish to enter %s ?\n", pname); scanf("%c", &choice); if (choice == 'Y'); printf("Enter %s...\n", pname); else (choice == 'N'); printf("Farewell lost soul!\n"); return 0; }
推荐答案
char Y [1]; char N [1]; char choice[1];
到
char Y; char N; char choice;
否则,使用当前定义, choice 表示数组(1个元素),
Otherwise, with the current definition, choice represents an array (of 1 element), which
- char 将会正常工作。
- 会触发执行 ,因为你不能比较数组与 == 运算符。
- is not required here, a single char will work just fine.
- will trigger the warning for doing choice == 'Y', because, you cannot compare array with == operator.
也就是说,
- scanf(%c,& choice) code>应该 scanf(%c,& choice); 以避免以前的换行符 。
- scanf(%s,& pname); 应为 scanf ,pname); 以避免缓冲区溢出。
- scanf ("%c",&choice); should be scanf (" %c",&choice); to avoid the previous newline.
- scanf ("%s",&pname); should be scanf ("%24s",pname); to avoid buffer overrun.
: @iharob ,
- 3>
- if(choice =='Y'); )(; 已移除),否则如果语句实际上无用。
- else 没有条件表达式计算。但你可以使用 else if(choice =='N')。
- if (choice == 'Y');should be if (choice == 'Y') (the ; removed), otherwise, the if statement is effectively useless.
- There is no conditional expression evaluation for else. You can make use of else if(choice == 'N'), though.
这篇关于修复这个if,else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!