myparser.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main(){
  6. int addend = 0, sum = 0, i = 0; // initialize variables
  7. string word = ""; // initialize string object
  8. ifstream fin ("input.txt"); // input file at ./input.txt
  9. ofstream fout ("output.txt"); // create or overwrite output file at ./output.txt
  10. while (fin >> addend){ // run until end of file / get first int
  11. sum = 0; // reset the sum for each word
  12. sum += addend; // add first int to sum
  13. fin.ignore(); // skip the first comma
  14. fin >> addend; // get second int
  15. sum += addend; // add second int to sum
  16. fin.ignore(); // skip the second comma
  17. fin >> addend; // get third int
  18. sum += addend; // add third int to sum
  19. fin.ignore(); // skip the \n
  20. getline(fin, word); // get the word on the line below its ints
  21. for (i = 0; i < sum - 1; i++){ // for each specified time
  22. fout << word << ","; // print the word with a comma
  23. } fout << word << endl; // no comma at the end
  24. }
  25. fin.close();
  26. fout.close();
  27. }