Skip to content

Instantly share code, notes, and snippets.

@afifabroory
Last active July 31, 2021 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afifabroory/e4be703c1240a2df186601420ca86dfe to your computer and use it in GitHub Desktop.
Save afifabroory/e4be703c1240a2df186601420ca86dfe to your computer and use it in GitHub Desktop.
My solution for ods exercises 1.1 (2)
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
void read_fifty_input(ifstream* myFile);
void write_input(stack<string>& stackText);
int main(void)
{
const string fileName = "HelloWorld.txt";
ifstream myFile(fileName);
read_fifty_input(&myFile);
}
void read_fifty_input(ifstream* myFile)
{
string textLine;
stack<string> stackLine;
int counter = 0;
while (getline(* myFile, textLine))
{
if (counter >= 50) {
write_input(stackLine);
cout << endl;
stackLine.push(textLine);
counter = 1;
}
else
{
stackLine.push(textLine);
++counter;
}
}
if (!stackLine.empty())
{
write_input(stackLine);
}
}
void write_input(stack<string>& stackText)
{
while (!stackText.empty())
{
cout << stackText.top() << endl;
stackText.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment