c++ - C++ 无法识别 getline()
问题描述
我正在尝试完成编码任务,并且我正在进入需要对其进行编码的部分,以便用户可以输入一个字符来做出决定。对于分配,我需要将变量存储为字符串,因此我一直在尝试使用 get line() 函数来允许输入。我遇到的问题是,当我运行程序时,它根本不允许我输入任何字符串字符,而是直接通过错误检查运行。第 30 和 31 行是我遇到问题的地方,我们将不胜感激。
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//Variable Declarations
double num1,num2;
string input;
//step 1
cout<<"ECE 0301 - Vectors in R2 and Complex Numbers\nPlease enter two numbers, separated by a space,\nthat will represent a vector or a complex number."<< endl << endl;
//step 2
//asking for input
cin>> num1 >> num2;
//outputting
cout << setprecision(3) << fixed << "You entered " << num1 << " and " << num2 << "." << endl;
//step 3
cout <<"\nAre these numbers in Cartesian (C) or polar (P) coordinates?\nPlease enter a single character as your choice.\n";
getline(cin,input);
//error check
if(input != "c" || input != "C" || input != "p" || input != "P")
{
cout <<"\nERROR! Invalid selection,exiting.";
return 0;
}
return 0;
}
解决方案
推荐阅读
- ios - 无法识别多个用户的生物识别(Touch ID/Face ID)API 有什么用?
- java - 如何在断言 Selenium getCssValue("background") 返回的背景颜色 rgb(255,255,255) 时将#ffffff 转换为 #fff 或 #fff 转换为 #ffffff
- c# - 简单的空检查+属性访问
- c - Zlib nodejs错误:存储的块长度无效
- javascript - 如何在没有 jQuery 的情况下在模态中创建平滑滚动到给定 id 的元素
- sharepoint - 如何使用 Microsoft Graph 搜索所有网站?
- azure-cosmosdb - Order By ASC 与 DESC 在性能上的巨大差异
- java - 如何强制 Jgit 使用 SSH 而不是 HTTPs?
- javascript - 关闭浏览器后注销
- reactjs - 使用 React 按 Cruise Line 显示游轮列表