I am trying to read a .CSV file and then put it into a stack the file contains data like this: id, name, date, num (The file has no header is for reference only)
-Example:
12, Mason, 08/12/2019, 58
10, Liam, 08/18/2018, 25
18, Ethan, 02/13/2020, 15
What I want is to take the first data from each row of the .CSV file and put it on the stack.
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
struct Node{
int id;
Node *next;
};
typedef Node *ptrNode;
void addstack( ptrNode *ptrtop, int n );
void printstack( ptrNode cursor );
int main(){
ifstream in( "file.csv" );
string s, t;
Node *stack1 = NULL;
int dat;
while( !in.eof() ){
getline( in, s );
in >> dat;
//getline( in, t ); //
addstack( &stack1, dat );
printstack( stack1 );
}
getch();
return 0;
}
void addstack( ptrNode *ptrtop, int n ){
ptrNode ptrnew;
ptrnew = new Node;
if ( ptrnew != NULL ) {
ptrnew->id = n;
ptrnew->next = *ptrtop;
*ptrtop = ptrnew;
}
cout << "\tAdded: " << n << endl;
}
void printstack( ptrNode cursor )
{
if( cursor == NULL ) {
cout << "\n\tEmpty stack\n";
} else {
cout << "\tStack is: " ;
while( cursor != NULL ) {
cout << cursor->id << "->";
cursor = cursor->next;
}
cout << "NULL\n\n";
}
cout << endl;
}