So after my program enters the for loop it asks me for the head nodes data, then returns back into the loop and crashes?
Here is my code
main.cpp:
#include "main.h"
void print_list(Node*);
void get_data(Node*&);
int main()
{
Node *head = NULL;
Node *temp, *last;
unsigned int numberOfFriends;
std::cout << "Number of friends: ";
std::cin >> numberOfFriends;
for(unsigned int i = 0; i < numberOfFriends; i++)
{
if(head == NULL)
{
head = new Node;
get_data(head);
last = head;
}
else
{
temp = new Node;
get_data(temp);
last->next_node = temp;
last = temp;
}
}
print_list(head);
return 0;
}
void print_list(Node* head)
{
Node * ptr = head;
while(ptr != NULL) //While the ptr iterator hasn't reached the end of the list, proceed
{
//Output node contents to console
std::cout << ptr->SFriend.name << std::endl;
std::cout << ptr->SFriend.email << std::endl;
ptr = ptr->next_node; //next pointer in the linked list
}
//Exit function when ptr has reached NULL
}
void get_data(Node*& node)
{
std::cout << "Name: ";
std::cin >> node->SFriend.name;
std::cout << "Email: ";
std::cin >> node->SFriend.email;
}
main.h:
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <iostream>
#include <windows.h>
#include <vector>
#include "main.h"
struct friends
{
char *name;
char *email;
};
struct Node
{
friends SFriend;
Node * next_node;
};
#endif // MAIN_H_INCLUDED

New Topic/Question
Reply



MultiQuote






|