I am using a function to read strings from a normalized file in order to push each string to the top of a stack that is being implemented dynamically using a linked list.
the normalized file is using data like this:
racecar
rotator
rotor
civic
when i call my function i expect my output to look the same after it uses get line to push each string to the stack, however, the actual out put is erasing the first character in the first string in a manner that looks like this:
acecar
rotator
rotor
civic
i have tried to use the ignore(), sync(), and clear functions before each iteration of get line() but i still keep coming up with the same problem.
i need some real help with this one and i hope one of you code masters can help me find the solution since i have yet to be able to get ignore() and the rest of them to ever work correctly!!! :/
here is the code to my function:
void createStack(fstream &normFile, ostream &outFile)
{
string catchNewString;
do
{
DynStrStk stringStk;
getline(normFile,catchNewString,'\n');
stringStk.push(catchNewString);
//tracer rounds
cout << endl;
outFile << catchNewString << endl;
cout << "test: " << catchNewString << endl;
} while(!normFile.eof());
}
this is the entirety of my main function:
//
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
#include <string>
#include "DynStrStk.h"
using namespace std;
void processFile();
void parseFile(ifstream&, fstream&);
void createStack(fstream&, ostream&);
int main()
{
//call function to open file and process
cout << "processing file" << endl;
processFile();
return 0;
}
void processFile()
{
ifstream inFile;
fstream normFile;
ofstream outFile;
cout << "opening files" << endl;
// open files
inFile.open("inFile.txt");
normFile.open("normFile.txt");
outFile.open("outFile.txt");
cout << "parsing file" << endl;
//parse file for capitalization & punctuation
parseFile(inFile, normFile);
//create stack with parsed and normalized normFile
createStack(normFile, outFile);
//close files
outFile.close();
normFile.close();
inFile.close();
}
void parseFile(ifstream &inFile, fstream &normFile)
{
//create and initialize variables
string newString;;
int i;
if(!inFile)
{
cout << "ERROR!!! Cannot read file.";
}
else
{
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0;
//parse each string for punctuation
while(newString[i])
{
if(isalpha(newString[i])) //check each char in each
//string for punctuation
{
if(isupper(newString[i])) //check each string for
//capitalization
{
newString[i] = tolower(newString[i]); //convert
//string to lower case
}
normFile << newString[i]; //output each line tofile
cout << newString[i];
}
i++;
}
normFile << '\n';
cout << '\n';
} while(!inFile.eof());
}
}
void createStack(fstream &normFile, ostream &outFile)
{
string catchNewString;
do
{
DynStrStk stringStk;
getline(normFile,catchNewString,'\n');
stringStk.push(catchNewString);
//tracer rounds
cout << endl;
outFile << catchNewString << endl;
cout << "test: " << catchNewString << endl;
} while(!normFile.eof());
}
this is my push function in my header file:
//function that pushes the argument onto the list
void DynStrStk::push(string newString)
{
StackNode *newNode = nullptr; //Pointer to a node
//Allocate a new node and store string
newNode = new StackNode;
newNode->newString = newString;
//if there are no nodes in the list make newNode the first node
if(isEmpty())
{
top = newNode;
newNode->next = nullptr;
}
else //otherwise insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}
this is my header file:
#ifndef DynStrStk_h
#define DynStrStk_h
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class DynStrStk
{
private:
//struct stack nodes
struct StackNode
{
string newString; //string held in the node
StackNode *next; //pointer to the next node
};
StackNode *top; //pointer to the top of the stack
public:
//Constructor
DynStrStk()
{top = nullptr;}
//Destructor
~DynStrStk();
//Stack Operations
void push(string);
void pop(string &);
bool isEmpty();
};
#endif