1

I'm trying to write a code that pulls from a txt file (input.txt) that looks like this:

1,2,3
Hello
2,3,4
Music
1,3,4
Dodie
3,0,1
Kitty
5,1,0
Cheese

And prints out the word as many times as the numbers above it added up. So the first one would be 1+2+3=6, so it would print: Hello,Hello,Hello,Hello,Hello,Hello

Current Output:
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
Hello
Hello
Hello
Hello
Hello
2,3,4
2,3,4
2,3,4
2,3,4
2,3,4
Music
Music
Music
Music
Music
1,3,4
1,3,4
1,3,4
1,3,4
1,3,4
Dodie
Dodie
Dodie
Dodie
Dodie
3,0,1
3,0,1
3,0,1
3,0,1
3,0,1
Kitty
Kitty
Kitty
Kitty
Kitty
5,1,0
5,1,0
5,1,0
5,1,0
5,1,0
Cheese
Cheese
Cheese
Cheese
Cheese

I can't figure out how to pull a specific line separately/separate the lines of the txt file.

Current code (we aren't supposed to use namespace std, but I was trying to figure out the basics first. It is also supposed to print into another txt file called output.txt):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
const int MAX=7;
int main() {

        ifstream inFile ("input.txt");
        char input[MAX];

        while (inFile) {
                int num = 5;
                inFile.getline(input, MAX);

                for(int n = 0; n < num; n++) {

                        printf(input);
                        printf("\n");

                } // end for
        } // end while

        inFile.close();

        return 0;
}
  • Does your assignment requirements allow the use of `std::string` and `std::stirngstream`? [They can make this brutally easy.](https://stackoverflow.com/a/7868998/4581301) – user4581301 Oct 05 '18 at 21:28
  • @user4581301, yes, I'm pretty sure we can. – Lauren Fasig Oct 05 '18 at 21:32
  • Are you not “supposed to use namespace std” as in “the standard library”, which seems overbroad, or as in `using namespace std;`, which is widely derided? – Davis Herring Oct 06 '18 at 15:15

0 Answers0