char指针,使用cin.getline读取?

时间:2018-02-24 15:34:41

标签: c++ pointers object cin

目前正在做一些功课,我遇到了一些问题。每当我在char指针firstname中写入时,我的程序都会崩溃。我得到了char * firstname,我尝试使用cin.getline(firstname,MAXTXT),其中MAXTXT = 80; - 这导致我的程序崩溃,同样发生在我的char * lastname上。我怀疑是否与分配内存有关?因为它与我的dob cin.getline一起工作正常。

我应该如何读取用户输入和char指针?我必须使用cin / cin.getline而不是其他类型的字符串,因为这是家庭作业的一部分。我可以在包含之后使用//中的任何内容。

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif 

//  INCLUDE:
#include  <iostream>         //  cin, cout
#include  <cstring>          //  strcpy, strlen, strncmp
#include  <cctype>           //  toupper
#include  <cstdlib>          //  itoa
#include <iomanip>           // setw

using namespace std;

//  CONST:
const int MAXEMP = 100;     //  Max. amount of employees.
const int MAXTXT = 80;     //  Max lenght for text/string.
const int DATELEN = 7;     //  Fixed lenght for date incl \0.

                           //  ENUM:
enum Gender { female, male };

//  FUNCTION DECLARATION
void printMenu();
char read();
int  read(const char* t, int min, int max);
void newEmployee();

//  KLASSER:
class Person {                   
protected:
    char* firstname;                //  Person's firstname.
    char  dob[DATELEN];   //  Date of birth format yymmdd

public:
    Person() {
        cout << "\nfirstname: ";
        cin.getline(firstname, MAXTXT); // This crashes the program after I've written in a firstname
        cout << "\ndob (yymmdd): "; cin.getline(dob, DATELEN); // This seems to work
    }
};

class Grownup : public Person {  
protected:
    char* lastname;              //  Grownups' lastname

public:
    Grownup() {
        cout << "Lastname: "; cin.getline(lastname, MAXTXT); // The program also crashes when entering lastname
    }
};



class Employee : public Grownup {        //  Employee class
private:
    int      nr;                       //  Unique employee ID number

public:
    Employee() {
        cout << "\n\nWARNING: This message should never be displayed\n\n";
    }

    Employee(int n) {
        nr = n; // Sets the nr to whatever is sent in the parameter
    }

};

//  GLOBAL VARIABLES
Employee* employees[MAXEMP + 1];     //  Array with pointers to all the employees
int lastUsed;                //  Last used empolyee "array number"

                               //  MAIN PROGRAM
int main() {
    char command;                //  Users wish/command

    printMenu();                  //  Prints menu with command options

    command = read();             //  Read users command wish
    while (command != 'Q') {
        switch (command) {
        case 'N': newEmployee();        break;   // Add a new employee
        default:  printMenu();      break;   // Print menu
        }
        command = read();           // Reads users command wish
    }
    return 0;
}

//  FUNCTION DEFINITIONS
void printMenu() {
    cout << "\n\nCOMMANDS AVAILABLE:";
    cout << "\n\tN - New employee";
    cout << "\n\tQ - Quit";
}


char read() {                     //  reads and returns in uppercase
    char ch;
    cout << "\n\nCommand:  ";
    cin >> ch;  cin.ignore();
    return (toupper(ch));
}
//  Reads leadtext (t), read and
//    return a number between min and max
int read(const char* t, int min, int max) {
    int number;
    do {
        cout << '\t' << t << " (" << min << '-' << max << "):  ";
        cin >> number;  cin.ignore();
    } while (number < min || number > max);
    return number;
}


void newEmployee() {                    //  N - NEW EMPLOYEE:
    if (lastUsed <= MAXEMP) {   // read employeenumber
        employees[++lastUsed] = new Employee(read("Employee nummer", 0, 9999));
    }
    else cout << "\nMax amount of employees reached";
}

1 个答案:

答案 0 :(得分:1)

firstname是一个未初始化的指针。

它没有任何意义。

您要求cin.getline写入指向的缓冲区。

糟糕!

dob之下,你立即“正确”了;为什么不让firstname成为一个数组呢?