writing to a data file

I'm having some issues with my program and don't know where to look really. The user is supposed to enter 5 lines to make a limerick poem. When I check the file its code like 006CF744 and not the poem as desired. What should i fix?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	ofstream outFile;
	outFile.open("C:\\Users\\Admin\\Limerick.dat");
	string limerickArray[5];
	for (int i = 1; i <= 5; i++)
	{
		cout << "Enter Limerick line: " << i << endl;
		getline(cin, limerickArray[i]);
		cin.clear();
	}
	for (int i = 1; i <= 5; i++)
	{
		outFile << limerickArray << endl;
		outFile.close();
		system("pause");
		return 0;
	}
}
Last edited on
closed account (SECMoG1T)
1
2
3
outFile << limerickArray << endl;
outFile << limerickArray[i] << endl;///change to this
///remove line 19 


or you could reduce that overhead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ofstream outFile("C:\\Users\\Admin\\Limerick.dat");
	std::string line{};

	for (int i = 1; i <= 5; i++)
	{
		std::cout << "Enter Limerick line: " << i << std::endl;
		std::getline(std::cin,line,'\n');
		outFile << line << std::endl;
	}
	
	outFile.close();
        system("pause");
}
Last edited on
Thanks Yolanda! I had outFile << limerickArray[i] initially but think i had a build issue. Moving the outfield.close outside the for {} fixed my issue
Yolanda so this is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	ofstream outFile;
	outFile.open("C:\\Users\\Bryan Bean\\Limerick.dat");
	string limerickArray[5];
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter Limerick line: " << i << endl;
		getline(cin, limerickArray[i]);
		cin.clear();
	}
	for (int i = 0; i < 5; i++)
	{
		outFile << limerickArray[i] << endl;
	}
	outFile.close();
	system("pause");
	return 0;
	
}


whenever i try for (int i = 1; i <= 5; i++) i keep getting an error in which i have to break(i guess due to the arrays size). What I want it to say is Enter Limerick line 1...instead of starting at 0. What's up with that. I thought setting i=1; i<=5 would do it be it's not.
@Billyin4C

You just need to add a little bit to line 19

cout << "Enter Limerick line: " << i+1 << endl;

That would be a '+1' after the i.
Thanks whitenite1!

I do believe that easy fix signals bedtime.
Topic archived. No new replies allowed.