当我在程序中输入第二个值时,将其带到第三个值并且不能正确执行

时间:2016-11-28 15:15:01

标签: c++

当我在程序中输入第二个值时,将其带到第三个值并且不能正确执行

当我输入性别时,它应该让我输入学位但是请输入第三个值并且程序不能正常工作。

#include <iostream>
#include <stdio.h>
#include <conio.h>
int main()
{
    char gender; 
    char degree;
    int age;
    printf("Enter Gender (F/M) ");
    scanf("%c",&gender);
    printf("Enter Degree ");
    scanf("%c",&degree);
    printf("Enter Age ");
    scanf("%d",&age);
    if(gender == 'M')
    {
        if(degree == 'P')
        {
            if(age >= 35 && age <= 50)
            printf("Good Work");
            else
            printf("Better Luck Next Time");
        }
        else
        {
            if(degree == 'G')
            {
                if(age >= 22 && age <= 40)
                printf("Good Work");
                else
                printf("Next Time");
            }
        }
    }
    else
    {
        if(degree == 'P')
        printf("Good Work");
        else
        printf("Next Time");
    }
    getch();
}

2 个答案:

答案 0 :(得分:1)

使用cin功能代替scanf。它会解决你的问题。

答案 1 :(得分:0)

#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
    char gender; 
    char degree;
    int age;
    cout<< "Enter Gender (F/M) ";
    cin>>gender;
    cout<<"Enter Degree ";
    cin>>degree;
    cout<<"Enter Age ";
    cin>>age;
    if(gender == 'M')
    {
        if(degree == 'P')
        {
            if(age >= 35 && age <= 50)
            printf("Good Work");
            else
            printf("Better Luck Next Time one");
        }
        else
        {
            if(degree == 'G')
            {
                if(age >= 22 && age <= 40)
                printf("Good Work");
                else
                printf("Next Time two");
            }
        }
    }
    else
    {
        if(degree == 'P')
        printf("Good Work");
        else
        printf("Next Time three");
    }
    getch();
}