【问题标题】:Certain line of code doesn't execute [duplicate]某些代码行不执行[重复]
【发布时间】:2017-10-30 20:03:23
【问题描述】:

我的程序的内容与这个问题无关,但我们几乎使用类和对象来保存信息。我的代码如下:

#include <iostream>
using namespace std;

class Car
{
private:
    int carNumber;
    bool loaded;
    string reportingMark, kind, destination;
public:
    void setUp(string reportingMark, int carNumber, string kind, bool 
loaded, string destination);
    void output();
};

void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination);

/* ******************** main ********************
 */
int main()
{
    Car *ptr;

    string reportingMark, kind, destination;
    int carNumber;
    bool loaded;

    Car c = *new Car();
    ptr = &c;
    input(reportingMark, carNumber, kind, loaded, destination);
    ptr->setUp(reportingMark, carNumber, kind, loaded, destination);
    ptr->output();
    return 0;
}
/* ******************** Member Functions ********************
 */

/* ******************** setUp ********************
 Takes 5 parameters by value: reporting mark, car number, kind, loaded, 
and destination
 Inputs these paramaters into the Car class' variables
 */
void Car::setUp(string rm, int cn, string k, bool l, string d)
{
    reportingMark = rm, carNumber = cn, kind = k, loaded = l, 
destination = d;
}

/* ******************** output ********************
 Prints the reporting mark, car number, kind, loaded, and destination 
in a neat format
 */
void Car::output()
{
    cout << "\n\nReporting Mark: " << reportingMark << "\nCar Number: " 
<< carNumber << "\nKind: " << kind << "\nCar is loaded: " << loaded << 
"\Destination: " << destination << endl;
}
/* ******************** Global Functions ********************
 */

/* ******************** input ********************
 Takes the reporting mark, car number, kind, loaded, and destination as 
reference parameters
 Asks the user to input these values
 */
void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination)
{
    do
    {
        cout << "What is the Reporting Mark? (Between 2-4 characters) ";
        getline(cin, reportingMark);
    if(reportingMark.length() < 2 || reportingMark.length() > 4) cout << "invalid input, try again\n";
} while(reportingMark.length() < 2 || reportingMark.length() > 4);
cout << "What is the Car Number? ";
cin >> carNumber;
int carNumResponse;

do
{
    cout << "What is the Kind? (Choose '1' for business, '2' for maintenance, or '3' for other) ";
    cin >> carNumResponse;
    if(carNumResponse < 1 || carNumResponse > 3) cout << "invalid input, try again\n";
} while(carNumResponse < 1 || carNumResponse > 3);
if(carNumResponse == 1) kind = "business";
if(carNumResponse == 2) kind = "maintenance";
if(carNumResponse == 3) kind = "other";

int loadedResponse;

do
{
    cout << "Is the car loaded? (Type '1' if true and '2' if false) ";
    cin >> loadedResponse;
    if(loadedResponse < 1 || loadedResponse > 2) cout << "invalid input, try again\n";
} while(loadedResponse < 1 || loadedResponse > 2);
if(loadedResponse == 1) loaded = true;
if(loadedResponse == 2) loaded = false;

cout << "What is the Destination? ";
getline(cin, destination);
}

我的程序的最后两行:

cout << "What is the Destination? ";
getline(cin, destination);

由于某种原因不让我输入任何内容。我是否错误地使用了 getline?它在程序的其他部分以相同的方式使用。感谢任何提供帮助的人!

【问题讨论】:

  • “不运行”是什么意思? cout 是否被写入屏幕? getline 失败了吗?都失败了吗?你有错误吗?
  • 我编辑了它,基本上我会看到消息“目的地是什么?”但它不会让我输入响应。
  • 为什么要动态分配变量? C++ != Java,你不需要使用动态内存来分配类的实例。
  • @ThomasMatthews 在作业的说明中使用指向类的指针
  • 也许可以试试 'std::cin.getline' - 你需要预先为目的地预留()空间。

标签: c++ string getline


【解决方案1】:

这里的问题是您在执行getline 操作之前有一个cin&gt;&gt;cin&gt;&gt; 所做的是它从流中提取所有字符,但留下换行符。

对于 getline,默认分隔符是换行符。因此它会跳过输入,因为它会收到流中留下的换行符。

解决方案,您必须执行cin.ignore (),这将刷新流,即删除换行符。我建议在使用getline() 函数之前先执行cin.ignore ()

属性: 来自帖子的帮助:How to use cin.getline in blocking mode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 2017-01-07
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2023-03-16
    相关资源
    最近更新 更多