Me myself and I
Me myself and I

Reputation: 57

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; - This causes my program to crash, same happens to my char* lastname. I do suspect is has something to do with allocating memory? As it worked fine with my 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. 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";
}

Upvotes: 1

Views: 565

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

firstname is an uninitialised pointer.

It does not point to anything.

You asked cin.getline to write to the buffer it points to.

Whoops!


Immediately underneath you "got it right" with dob; why not make firstname an array as well?

Upvotes: 1

Related Questions