【发布时间】:2015-02-04 13:38:39
【问题描述】:
我们可以在 else 中使用 scanf() 函数吗,就像我在这段代码中使用的那样。 因为我无法为性别变量输入值(字符)。 所以我想知道为什么我无法输入性别变量的值?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int age;
char s,ms;
printf("Please enter M if you are married or U if you are un-married\n");
scanf("%c", &ms);
if(ms=='M')
printf("\nyou are recruted");
else if(ms=='U')
{
printf("\nenter sex- A for male & B for female\n");
scanf("%c",&s);
if(s=='A')
{
printf("\nEnter your age\n");
scanf("%d",age);
if(age>30)
printf("\nYou are selected");
else
printf("\nYour age is less for this job");
}
else if(s=='B')
{
printf("\nEnter your age\n");
scanf("%d",age);
if(age>25)
printf("\nyou are recruted");
else
printf("\nyour age is less to be recruted");
}
else
{
printf("Please enter A for male or B for female");
}
}
else
{
printf("PLEASE ENTER THE CORRECT VALUE\n please enter M for Married or U for un-married");
}
getch();
}
输出:
Please enter M if you are married or U if you are un-married
U
enter sex A for male or B for female
Please enter A for male or B for female
【问题讨论】:
-
您应该始终测试scanf(3) 的结果,它给出了成功扫描的项目数
-
顺便说一句,不要忘记编译所有警告和调试信息(例如
gcc -Wall -Wextra -g)然后使用调试器(gdb)
标签: c if-statement scanf