Just check to make sure

5. Mary Conrad wants a program that allows her to save each letter of the alphabet and an associated word starting with the same letter of the alphabet on each line in a sequential access file
Example:
A apple
B button
She will enter the information from the keyboard. Assuming that the file name is alphabetChart.txt.
Write the corresponding C++ program.

This is my program

inputData.open(“alphabetChart.txt”, ios::in);
cout<<A Apple<<endl;

etc till

cout<<Z Zebra<<endl;

I not sure if I'm on the right track.
I believe you are right. The c equivalent is to use freopen("alphabetChart.txt", stdout);

Then when you type:
cout<<"A Apple"<<endl;

It will appear in the text
Last edited on
Thank you very much
closed account (N36fSL3A)
No it won't appear in the tect.

After you open inputData, assuming it's an ofstream object, you'd do this:
1
2
3
4
inputData << "A apple"; // etc.
// Then we close the file when we're done to
// avoid memory leaks
inputData.close();
As you're saving to file, I'm not sure why you're calling your file stream inputData.

The obvious thing to do is open the output file.
Read the input in from std::cin, and write it to the output file.
That's what the requirements call for.

There is no reason to redirect stdout/std::cout.


Fredbill30 wrote:
After you open inputData, assuming it's an ofstream object, you'd do this:

File streams are closed automatically when they go out of scope.

Topic archived. No new replies allowed.