Creating an array for my MADLIBS program

Instructions: "The first line in the file contains how many questions or prompts there are for the particular mad libs game. You need to read this number from the file in order to know your array sizes."

I've figured out how to open the file and check if it opens successfully. After that, my program displays the .txt file text, but not in the correct manner. I'm stuck on how my arrays are supposed to work here...

My code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ifstream myFile("dog.txt"); 

            if(!myFile)
            {
                cout << "Error opening file" << endl;
                exit(1);
            }

            if(myFile.is_open())
            {
                while(getline (myFile,line))
                {
                    cout << line << ":";
                    //user input?
                    cout << '\n';                    
                }
                myFile.close();
            }


This is my output so far:

14:
verb ending in "ing":
part of the body:
plural noun:
verb:
noun:
a place:
adverb:
noun:
plural noun:
part of the body:
part of the body:
plural noun:
plural noun:
noun:

dog.txt includes:

14
verb ending in "ing"
part of the body
plural noun
verb
noun
a place
adverb
noun
plural noun
part of the body
part of the body
plural noun
plural noun
noun

Intended output to user:

verb ending in "ing":
part of the body:
plural noun:
verb:
noun:
a place:
adverb:
noun:
plural noun:
part of the body:
part of the body:
plural noun:
plural noun:
noun:


The user should have each line from the .txt file displayed (not including the 14) one at a time so they can enter their string to be saved and reproduced on a separate .txt file with the "story". Their strings will fill in blank spots on the other .txt file to produce a complete "madlib". That is my main issue.
Last edited on
The user should have each line displayed one at a time so they can enter their string to be saved and reproduced on a separate .txt file with the "story".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <fstream>
#include <iostream>
#include <string>

int main()
{
	std::ifstream myFile("dog.txt");

	if (!myFile) {
		std::cout << "Error opening file\n";
		return 1;
	}

	std::ofstream myout("story.txt");
	std::string line;

	std::getline(myFile, line);		// Skip over number of lines

	while (getline(myFile, line)) {
		std::cout << line << ": ";
		std::getline(std::cin, line);
		myout << line << ' ';
	}

	myout << '\n';
}

Topic archived. No new replies allowed.