I'm trying to load a text file with the values: 10 11 12 13 14 15 16 17 18 19 20 30 40 50 55 60 70 80 90 91 92 93 94 95 96 97 98 99 into a linked list, inserting each new value into the end of the list. The issue I'm having is that when I run the code, I get the error that I'm trying to run a string through a function that expects an int, which makes sense, but once I added stoi()
into the mix to convert the values to int, I started getting crazy amounts of errors.
I've been working on this function for the past day or so, and none of my searching as yielded any results. I feel that I'm pretty close with this, but I'm probably missing something big here. Still pretty new to linked lists as a whole, we just learned about them in class last week.
#include <iostream>
#include <fstream>
#include <string>
#include "linkedlist.h" // Has the prototypes for each function
using namespace std;
// I didn't include a lot of functions since I don't think they're
// related to the error, but let me know if I should
Node* createNewNode(int data) {
Node *ptr;
Node *temp = new Node();
temp -> data = data;
temp -> next = NULL;
ptr = temp;
return ptr;
}
Node* createNewList() {
Node *head = NULL;
return head;
}
Node* load(string filename) {
Node *head = createNewList();
string num;
ifstream myfile(filename.c_str());
while(myfile >> num) { // looping through each number
int num1 = stoi(num); // Converting string to int
myfile << insertAtEnd(head, num1);
}
return head;
}
void insertAtEnd(Node *list, int data) {
Node *ptr = createNewNode(data);
if (list == NULL) {
list = ptr;
}
else {
Node *temp = list;
while(temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = ptr;
}
}
int main() {
load("../resource/listdata.txt"); // name/location of the file
//No errors from rest of code but I can post if necessary
}
There are far too many errors for me to just paste them here, but I took a screenshot of most of them here: https://i.stack.imgur.com/RWCbV.png .
Thank you in advance for any help you can give me!
Edits:
Node* load(string filename) {
Node *head = createNewList();
string num;
ifstream myfile(filename.c_str());
while(myfile >> num) { // looping through each number
int num1 = stoi(num); // Converting string to int
insertAtEnd(head, num1);
}
myfile.close();
return head;
}
There are no longer any compiling errors, though when the code runs it outputs:
0
0
NULL
exit status -1
If I had to guess, I would assume my issue is now the while(myfile >> num)
area, because I don't think the code is properly traversing the text file and using the numbers, though I'm not sure about that.
Edit 2:
Node *load (string filename) {
Node *head;
string num;
ifstream myfile(filename.c_str());
while(myfile >> num) {
if(head) {
int num1 = stoi(num);
insertAtEnd(head, num1);
}
else {
head = createNewList();
int num1 = stoi(num);
head = createNewNode(num1);
}
}
myfile.close();
return head;
}
I'm hoping I followed the instructions correctly, though there's a good chance I haven't... I'm getting the same message as above, being 0 0 NULL exit status -1
but no errors still, which is good and bad, as I'd love to see what isn't working right now.