C ++ 14我的程序没有返回预期的内容

时间:2015-07-20 19:40:53

标签: c++ c++14

#include <stdio.h>
#include <stdlib.h>


int main()
{
    //You are in the elevator now;

    //Define variables;
    char a;
    char b;

    //Ask questions;
    printf("You are in the elevator now.\nPlease enter which floor you want to visit?\nPress 'L' for Basement, '1' for 1st floor, '2' for 2nd floor, and '3' for 3rd floor:\n");
    scanf("%c", &a);

    //Display options;
    if (a=='L'){
        printf("You are at the basement now.\n", a);

    scanf("%c", &b);
      printf("Where do you want to go?\nOnly Option is 'P' for Parking lot:\n");

         else (b=='P')

          printf("You are in the Parking Lot now.\n", b);
    }  

    else if (a=='1')
        printf("You are at the main floor now.\n", a);

    else if (a=='2')
        printf("You are at the Mechanical Department now.\n", a);

    else if (a=='3')
        printf("You are at the Electrical Department now.\n", a);

    else{
        printf("It's not an option\n");
    }

}

对于用户选择P后的第一个选项L,我找不到命令。我应该得到你现在在停车场。我不确定是什么原因造成的。

4 个答案:

答案 0 :(得分:2)

替换

else (b==='P')

通过

if (b == 'P')

答案 1 :(得分:0)

检查此代码......

#include <iostream>
using namespace std;
int main()
{
//You are in the elevator now;
//Define variables;
char a;
char b;

//Ask questions;
printf("You are in the elevator now.\nPlease enter which floor you want to visit?\nPress 'L' for Basement, '1' for 1st floor, '2' for 2nd floor, and '3' for 3rd floor:\n");
scanf("%c", &a);

//Display options;
if (a=='L')
{
        printf("You are at the basement now.\n");
        printf("Where do you want to go?\nOnly Option is 'P' for Parking lot:\n");
        cin >> b;
        if (b=='P'){
        printf("You are in the Parking Lot now.\n");}
}

else if (a=='1')
    printf("You are at the main floor now.\n");

else if (a=='2')
    printf("You are at the Mechanical Department now.\n");

else if (a=='3')
    printf("You are at the Electrical Department now.\n");

else
    printf("It's not an option\n");
return 0;
}

答案 2 :(得分:0)

您可能遇到的主要问题是scanf("%c")只消耗一个字符,按Enter键后,您将在缓冲区中显示'\n'。您可能希望以其他方式获取输入,例如使用以下内容:

std::string command;
std::cin >> command;
if (command == "P") {
    // ...
}

因为默认情况下会立即加载整个空格描述的字符串。

答案 3 :(得分:-1)

条件陈述中有三个等号。

b ===&#39; P&#39;而不是b ==&#39; P&#39;