【问题标题】:Beginner C++ difficulty...why is this loop exiting early?初学者 C++ 困难......为什么这个循环会提前退出?
【发布时间】:2014-10-01 03:16:53
【问题描述】:

这段代码是一个简单的类继承和虚函数练习。案例类“人”派生雇员、受抚养人、工人和经理。代码应该显示一个输入屏幕,用户可以在其中输入 10 个人的数据,这取决于用户选择输入数据的类型。然后,当用户输入满意的数据时,按 1、2、3、4 或 5 以外的键将把他们带到显示菜单,在那里他们可以选择他们想要查看的信息。

问题:main()中的第一个for循环运行正常,数据最多可以输入10次。但是,下一个循环(显示循环)显示菜单,然后立即退出而不读取用户的决定。我无法弄清楚为什么会这样。 IT 甚至在最后跳过了我的小测试输入,它只是退出了程序?

它不应该显示菜单,然后等待用户输入,然后继续或结束循环吗?

main.cpp

using namespace std;

#include "person.h"
#include <iostream>

using namespace std;

int main()
{

    worker w;
    employee e;
    dependent d;
    manager m;
    person p;

    int decision;


    for (int i=0; i<10; i++)
    {
        cout << "To enter employee information, press 1." << endl;
        cout << "To enter dependent information, press 2." << endl;
        cout << "To enter manager information, press 3." << endl;
        cout << "To enter worker information, press 4." << endl;
        cout << "To edit information for a person who is none of the above, press 5." << endl;
        cout << "To display entered information, press anything else." << endl;
        cin >> decision;
        if (decision==1)             //employee
        {
            e.obtain();
        }
        else if (decision==2)       //dependent
        {
            d.obtain();
        }
        else if (decision==3)         //manager
        {
            m.obtain();
        }
        else if (decision==4)        //worker
        {
            w.obtain();
        }
        else if (decision==5)        //other person
        {
            p.obtain();
        }
        else
        {
            break;
        }

    };

    for (int i=0; i<10; i++)

    {
        cout << "To display employee information, press 1." << endl;
        cout << "To display dependent information, press 2." << endl;
        cout << "To display manager information, press 3." << endl;
        cout << "To display worker information, press 4." << endl;
        cout << "To display information for a person who is none of the above, press 5." << endl;
        cout << "To exit, press anything else" << endl;
        cin >> decision;
        if (decision==1)             //employee
        {
            e.display();
        }
        else if (decision==2)       //dependent
        {
            d.display();
        }
        else if (decision==3)         //manager
        {
            m.display();
        }
        else if (decision==4)        //worker
        {
            w.display();
        }
        else if (decision==5)        //other person
        {
            p.display();
        }
        else
        {
            break;
        }
    }

    int test;
    cin >> test;
    return 0;
}

人.h

#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>

using namespace std;

struct date                       //Structures defining date display and SSN display
    {                             //e.g. 3/12/14, XXX-XX-XXXX
        int day;
        int month;
        int year;
    };
struct SSN
    {
        int dig123;
        int dig45;
        int dig6789;
    };


class person                     //Base class
{
public:
    person();
    virtual ~person();
    virtual void obtain ();
    virtual void display();

protected:
    string name;
    char gender;
    date dob;                     //Date of Birth
    SSN ssn;
    string address;
    int phone;
};


class employee: public person   //Employee derived class
{
public:
    employee();
    ~employee ();
    void obtain();
    void display();
private:
    date doh;               //Date of Hire
    int salary;
    string location;
    int workphone;
};

class dependent: public person   //Dependent derived class
{
public:
    dependent();
    ~dependent();
    void obtain();
    void display();
private:
    string address;
    SSN ssn2;                    //SSN of employee of dependent

};

class manager: public person    //Manager derived class
{
public:
    manager();
    ~manager();
    void obtain();
    void display();
private:
    string title;
};

class worker: public person
{
public:
    worker();
    ~worker();
    void obtain();
    void display();
private:
    string project;
};


#endif // PERSON_H

person.cpp

#include "person.h"

person::person()
{

}

person::~person()
{

}

void person::obtain ()
{

    cout<<"Please enter information about the individual:" << endl;
    cout<< "Name:" << endl;
    getline (cin, name);
    getline (cin, name);
    cout<< "Gender (m or f): "<< endl;
    cin>> gender;
    cout<< "Date of Birth: Day? " << endl;
    cin>> dob.day;
    cout<< "Date of Birth: Month? " << endl;
    cin>> dob.month;
    cout<< "Date of Birth: Year? " << endl;
    cin>> dob.year;
    cout<< "Address: "<< endl;
    getline (cin, address);
    getline (cin, address);
    cout<< "Phone number: " << endl;
    cin>> phone;
    cout<< "First three digits of SSN: " << endl;
    cin>> ssn.dig123;
    cout<< "Next 2 digits of SSN: " << endl;
    cin>> ssn.dig45;
    cout<< "Final 4 digits of SSN: " << endl;
    cin>> ssn.dig6789;
}
void person::display()
{
    cout<< "Name: " << name << endl;
    if (gender=='m')
    {
        cout<<"Gender: "<<gender<< endl;
    }
    else if (gender=='f')
    {
        cout<<"Gender: "<< gender<<endl;
    }
    else
    {
        cout<<"You did not enter male(m) or female(f) as a gender."<<endl;
    }
    cout << "Date of Birth: " << dob.month << "/" << dob.day <<"/" << dob.year << endl;
    cout << "Address: " << address << endl;
    cout << "Phone number: " << phone << endl;
    cout << "SSN: " << ssn.dig123 << "-" << ssn.dig45 << "-" << ssn.dig6789 << endl;
}

employee::employee():person()
{

}

employee::~employee()
{

}


void employee::obtain()
{
    person::obtain();
    cout<< "Date of Hire: Day? " << endl;
    cin>> doh.day;
    cout<< "Date of Hire: Month? " << endl;
    cin>> doh.month;
    cout<< "Date of Hire: Year? " << endl;
    cin>> doh.year;
    cout<< "Salary: $" << endl;
    cin >> salary;
    cout<< "Location: "<< endl;
    getline (cin, location);
    getline (cin, location);
    cout<< "Work phone number: " << endl;
    cin>> workphone;
}

void employee::display()
{
    person::display();
    cout << "Date of Hire: " << doh.month << "/" << doh.day <<"/" << doh.year << endl;
    cout << "Location: " << location << endl;
    cout << "Work phone number: " << workphone << endl;
}

dependent::dependent(): person()
{

}

dependent::~dependent()
{

}

void dependent::obtain()
{
    person::obtain();
    cout << "SSN of employee of which this person is a dependent:" << endl;
    cout<< "First three digits of SSN: " << endl;
    cin>> ssn2.dig123;
    cout<< "Next 2 digits of SSN: " << endl;
    cin>> ssn2.dig45;
    cout<< "Final 4 digits of SSN: " << endl;
    cin>> ssn2.dig6789;
    cout << "Address (if different from employee's address): " << endl;
    cout<< "Address: "<< endl;
    getline (cin, address);
    getline (cin, address);

}

void dependent::display()
{
    person::display();
    cout<< "Address: " << address << endl;
    cout << "SSN of employee: " << ssn2.dig123 << "-" << ssn2.dig45 << "-" << ssn2.dig6789 << endl;
}

worker::worker():person()
{

}
worker::~worker()
{

}

void worker::obtain()
{
    person::obtain();
    cout << "Project: " << endl;
    getline (cin, project);
}

void worker::display()
{
    person::display();
    cout << "Project: " << project << endl;
}

manager::manager():person()
{

}

manager::~manager()
{

}

void manager::obtain()
{
    person::obtain();
    cout << "Title: " << endl;
    getline (cin, title);
    getline (cin, title);
}

void manager::display()
{
    person::display();
    cout << "Title: " << title << endl;
}

如果有人可以帮助我了解为什么会发生这种情况,我将不胜感激。

【问题讨论】:

  • 你能简化代码吗?至于现在,还蛮长的。
  • 嗯,我不确定如何简化它,但我认为问题出在 main()?
  • 像做一个更小的例子。这是一大堆代码。
  • 编译所有警告和调试信息(例如g++ -Wall -g,如果使用GCC ....)并学习如何使用调试器(例如gdb on Linux)。
  • 没有警告或错误...

标签: c++ loops for-loop exit


【解决方案1】:

cin &gt;&gt; decision; 最多只能读取一个 \n 或一个空格,一旦它将您的字符串从输入缓冲区中取出, \n 仍然会留下。因此,在第二次迭代中,当您再次到达 cin &gt;&gt; decision; 时,它会读取到 \n 并退出。您需要通过调用getline 来清除缓冲区以消耗剩余的\n。

【讨论】:

  • 打败我 :) 正在输入这个。
  • 非常感谢!那么我是否只使用 cin.getline() 来做出决定?
  • @user3919624 no. getline(cin, decision); 可以解决问题。
  • @Josh 我应该在第一次迭代时也这样做吗?
  • 现在我得到错误,'错误:没有匹配的函数调用 getline(std::istream&, int&)
【解决方案2】:

1) 第一个 for 循环的末尾不需要分号。

2) 如果输入被跳过,很可能您在正在读取的输入缓冲区中有垃圾。在第二个循环之前试试这个:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

查看此链接:My cin is being ignored inside a while loop

【讨论】:

  • cin.ignore() 会起作用吗?我把它放在我的第一个循环之后,同样的问题仍然存在
  • 您可能需要清除失败位。在第二个循环之前和调用 ignore() 之前使用 std::cin.clear();
猜你喜欢
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 2013-04-18
  • 2013-11-03
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多