简体   繁体   English

字符指针,使用 cin.getline 读取?

[英]char pointer, read using cin.getline?

Currently doing some homework, and I got some issues.目前正在做一些功课,我遇到了一些问题。 My program is crashing whenever I write in my char pointer firstname.每当我写入我的字符指针名字时,我的程序就会崩溃。 I got char* firstname, and I try to use cin.getline(firstname, MAXTXT) where MAXTXT = 80;我得到了 char* firstname,我尝试使用 cin.getline(firstname, MAXTXT) where MAXTXT = 80; - This causes my program to crash, same happens to my char* lastname. - 这会导致我的程序崩溃,我的 char* 姓氏也会发生同样的情况。 I do suspect is has something to do with allocating memory?我怀疑与分配内存有关吗? As it worked fine with my dob cin.getline.因为它在我的 dob cin.getline 上运行良好。

How am I supposed to read input from user and into a char pointer?我应该如何将用户的输入读取到字符指针中? I have to use cin/cin.getline and not other kind of strings as that's a part of the homework.我必须使用 cin/cin.getline 而不是其他类型的字符串,因为这是作业的一部分。 I can use whatever is in // after the includes.我可以在包含之后使用 // 中的任何内容。

#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";
}

firstname is an uninitialised pointer. firstname是一个未初始化的指针。

It does not point to anything.它不指向任何东西。

You asked cin.getline to write to the buffer it points to.您要求cin.getline写入它指向的缓冲区。

Whoops!哎呀!


Immediately underneath you "got it right" with dob ;紧接着你用dob “做对了”; why not make firstname an array as well?为什么不让firstname成为一个数组呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM