我们可以在else中使用scanf()函数,就像我在这段代码中使用的那样。 因为性变量,我无法输入值(char)。 所以我想为什么我不能输入性变量的值?
#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
答案 0 :(得分:3)
在第一次读取此scanf时输入数据:
scanf("%c", &ms);
换行符仍在键盘中。要解决这个问题,请在第二个scanf中添加一个空格:
scanf(" %c",&s);
这是为了在stdin
读取用户输入之前使用carriage return
中可能由之前的用户输入(如scanf
)留下的任何尾随字符。另请注意,您错过了&
中的scanf("%d",age);
:
#include <stdio.h>
#include <stdlib.h>
int main()
{
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");
}
getchar();
return 0;
}
阅读Why is adding a leading space in a scanf format string recommended?
答案 1 :(得分:0)
输入年龄值时忘记添加&
,您可以使用getchar()
处理缓冲区中的新行。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
char s,ms;
printf("Please enter M if you are married or U if you are un-marriedn");
scanf("%c", &ms);
getchar(); // deal with thre newline in buffer
if(ms=='M')
printf("nyou are recruted");
else if(ms=='U')
{
printf("nenter sex- A for male & B for femalen");
scanf("%c",&s);
getchar(); // deal with thre newline in buffer
if(s=='A')
{
printf("nEnter your agen");
scanf("%d",&age); // add & when you use scanf
if(age>30)
printf("nYou are selected");
else
printf("nYour age is less for this job");
}
else if(s=='B')
{
printf("nEnter your agen");
scanf("%d",&age); // add & when you use scanf
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 VALUEn please enter M for Married or U for un-married");
}
getchar();
return 0;
}
此外,int main()
和return 0;
更好。