0

I am just starting to learn c++, and i made a simple linked list program. The problem is, it is getting exceptions thrown at it from within the string library and when printing the list. I have narrowed it down to an error when i call malloc, but i do not know how to fix it, or the other exception.

    // linkedlists.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct person {
    string name;
    int age;
    struct person* next;
};
person *head = NULL;
int length() {
    int count = 0;
    person *current = head;
    while (current->age != NULL) {
        current = current->next;
        count++;
    }
    return count;
}
void printlist() {
    person * current = head;
    while (current->next != NULL){ //exception is here.
        cout << "Name: " << current->name << "    Age: " << current->age <<          "\n";
        current = current->next;
    }
}
void insert() {
//  int choice;
    person *newNode = (struct person*) malloc(sizeof(person));//assuming exception is here because it is showing an exception at the size function in string library, and the struct person has string name.
    //cout << "Press 1 to insert at beginning of list.\n";
    //cin >> choice;
//  switch (choice) {
    //case 1:

    *newNode->next = *head;
    cout << "What is this person's name?\n";
    cin >> newNode->name;
    cout << "\nWhat is the age of " << newNode->name << "?";
    cin >> newNode->age;
    cout << "The current list of people is " << length() << " long.\n";
    printlist();

}
void menu() {
    int choice;
    cout << "Welcome to the person recorder! ";
    bool inloop = true;
    while (inloop) {
        cout << "Press 1 to add more entries. Press 2 to print the entire list. Press 3 to exit the program.\n"; //error in string when i press 1. error in the while loop when i press 2.
        cin >> choice;
        switch (choice) {
        case 1:
            insert();
        case 2:
            printlist();
        case 3:
            inloop = false;
        }
    }
}
/*void change(person* human) {
    string temp_name;
    int temp_age;
    cout << "What is this person's name?\n";
    cin >> temp_name;
    cout << "\nWhat is this person's age?\n";
    cin >> temp_age;
    human->name = temp_name;
    human->age = temp_age;
}
*/
int main()
{
    menu();
}
  • 5
    Is this homework? If so, your professor should stop teaching "C++" – David Zech Aug 17 '15 at 23:43
  • You should be able to track down exactly where an exception that crashes your program originates by using a debugger. You also have not actually included the error you got. "Exceptions" could mean more than a few things. They could be exceptions specified to be thrown for invalid arguments (e.g., `std::stoi` receiving a string that isn't a number). They could be placed there by the implementation for something the standard is less kind on (e.g., creating `std::string` from a null pointer with GCC). They could even not be C++ exceptions (e.g. SEH in Windows). All have error messages. – chris Aug 17 '15 at 23:58

1 Answers1

2

You are using malloc on a structure that contains a C++ class member (std::string in this case). This is a problem because C++ object construction isn't going to happen. Generally, you'd want to use the new operator instead of calling malloc in C++.

David Zech
  • 745
  • 3
  • 14
  • I mean really, you wouldn't want to use `new`, but a smart pointer if you had to. But `new` is certainly better than `malloc` here. – chris Aug 17 '15 at 23:49
  • @chris I agree, this is another case of glorified C in a C++ environment. – David Zech Aug 17 '15 at 23:50