我的程序没有返回预期的结果
C++14 My program is not returning what is expected
#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 optionn");
}
}
对于用户选择p后的第一个选项L,我得到的命令没有找到。我应该…你现在在停车场。我不确定是什么原因造成的。
Replace
else (b==='P')
if (b == 'P')
查看此代码…
#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 optionn");
return 0;
}
您可能遇到的主要问题是scanf("%c")
只消耗单个字符,而在按下enter键后,您将在缓冲区中获得'n'
。您可能希望以其他方式获取输入,例如使用如下命令:
std::string command;
std::cin >> command;
if (command == "P") {
// ...
}
条件语句中有三个等号
b === ' p '代替b == ' p '
相关文章:
- 如何返回一个类的两个对象相加的结果
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++Brute Force攻击函数不会返回结果
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- c++ lambda:柯里和函数:使用按值捕获与按引用捕获返回不同的结果
- C++ 获取函数在常量引用中按值返回的结果
- 如何在一个表达式中生成并返回结果?
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 三元运算符在返回语句中给出意外的结果
- 为什么函数 tellg() 没有返回好的结果?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法将 for 循环结果返回到像三元运算符这样的函数中?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 检查文本文件中的 0 或 1,并根据结果返回
- 如何使用boost::any_range连接多个boost范围并作为函数w/o的结果返回
- 如何从线程的结果返回Qt对象(Qtfutur)
- C++:当在函数内部创建新对象并将其作为结果返回时,我必须使用新运算符来创建对象