This program is meant to take the info from my text file and sort it into data and instruction based on if the line has an equal sign or not.
This is the content of the text file:
I am facing a problem with my while loop.
int main()
{
ifstream inputFile(filename);
cout << "The content of your text file is (1 for data and 0 for instructions): " << endl << endl;
while (getline(inputFile, word))
{
cout << left << setw(10) << word << setw(10) << right << checkEqualSign(word) << endl;
if (checkEqualSign(word) == 1)
modeData();
else if (checkEqualSign(word) == 0)
modeInstruction();
else
cout << "Wrong Syntax." << endl;
}
return 0;
}
void modeData()
{
int varVal;
char varName, equalSign1;
ifstream inputFile(filename.c_str());
cout << endl << "Data: " << endl;
while (inputFile >> varName >> equalSign1 >> varVal)
cout << varName << " " << equalSign1 << " " << varVal << endl;
}
void modeInstruction()
{
string opCode, operand;
ifstream inputFile(filename.c_str());
cout << endl << "Instructions: " << endl;
while (inputFile >> opCode >> operand)
cout << opCode << " " << operand << endl;
}
The modeData()
function is working well, but the output keeps repeating after each successful condition, and I don't want that. I want it to execute only one time at the end, so how can I fix it?
This is a pic of the repetition part:
The modeInstruction()
function is not working well. It should execute only the parts that are without an equal sign, but it is executing everything in the text file with a repetition too.
This is a pic to explain more:
I want this as an output:
Aucun commentaire:
Enregistrer un commentaire