首页 > 解决方案 > 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;
}

    

标签: c++

解决方案


推荐阅读