Confused on creating a text.file

//I am having trouble with the bottom half of this program creating text files for the numbers and word. Basically know what
//and where to put things is very confusing to me.
//Any assistance on this part would be greatly appreciated!!!!!!


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

using namespace std;


int main()
{

int array[5];
int counter;
string words[6];
const string endWord("end_of_array");




//five numbers in array
cout << "Please enter five numbers: ";


for (counter = 0; counter < 5; counter++)
{
cin >> array[counter];
}

cout << endl;


cout << "The numbers are: ";

for(counter = 0; counter < 5;counter++)
{
cout << array[counter] << " ";
}

cout << endl;



cout << "The numbers in reverse order are: ";

for (counter = 4; counter >= 0; counter--)
cout << array[counter] << " ";

cout << endl;


//Next, add code to read 5 words from the Console (user input).
//Store these values into an array of string [].
//Make sure the string[] array is large enough to hold at least 6 values.
//Store the string constant “end_of_array” into the last element of the array. //
//Using a do…while() loop, print out the 1st and 3rd letters of each
//word (two letters per line) using the substring function.

//clean any otstanding stuff from standard input
char trash[256];
cin.getline(trash,256,'\n');



cout << "Please enter five words: ";

for (counter = 0; counter < 5; counter++)
{
cin >> words[counter];

}
words[5]=endWord;


counter=0;
cout << "Print out 1st and 3rd letter of each word: " << endl;

do
{
cout << words[counter].substr(0,1);
cout << words[counter].substr(2,1);
cout << endl;
counter++;

}while( words[counter]!=endWord);



//create text files(numbers.txt),then write values for reverse order second file, third file reads the words.Displaying the
//results to the console as described above.
//Not understanding how to piece this part together for the text file.

ifstream inData;
ofstream outData;


inData.open("numbers.txt");
outData.open("output.txt");


inData >> number;
inData >> counter;

outData << "Read five numbers " << number << endl;





inData.close();
outData.close();

return 0;
}


Please put your code into [c0de] your code here [/c0de] tags. This will make it easier for people to read, and you will find they help you alot easier.
Hi gem925,
U should use the # format to place UR codes.

As I have understood UR problem.
U r facing problem while executing below piece of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
fstream inData;
ofstream outData;

inData.open("numbers.txt");
outData.open("output.txt");

inData >> number;
inData >> counter;

outData << "Read five numbers " << number << endl;

inData.close();
outData.close();


For this the description is some how confusing.


//create text files(numbers.txt),then write values for reverse order second file, third file reads the words.Displaying the
//results to the console as described above.
//Not understanding how to piece this part together for the text file.


Please elaborate what do u want to do.
What is second file......?????
What is third file ????
etc......

So that it will be easier for some1 to help u out.
Last edited on
This is the whole assignment listed below it the last part where I am having trouble putting together. Any Help would be appreciated I hope this gives you a clearer understanding of what I am working on.
I am having major problems with my inData and outData files getting the correct information to those files.



First, write a simple program that will read 5 numbers from the Console (user input). Store these values into an array of int []. Using the length of the array in a for() loop and decrementing the loop counter, write the array values in reverse order to the screen. This is step one and may be enough for partial credit. If you have difficulty with this, try seeking some assistance from your instructor before going on to step two.

Next, add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function. This is step two and should ensure you at least a passing grade.

Finally, modify the above program to read the numbers in from a text file (numbers.txt), write the values in reverse order to a second file, and read the words in from a third file, displaying the results to the Console as described above. This is your completed project.

//I am having trouble with the bottom half of this program creating text files for the numbers and word. Basically know what
//and where to put things is very confusing to me.
//Any assistance on this part would be greatly appreciated!!!!!!


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

using namespace std;


int main()
{

//declare variables
int array[5];
int counter;
string words[6];
const string endWord("end_of_array");



//five numbers in array
cout << "Please enter five numbers: ";


for (counter = 0; counter < 5; counter++)
{
cin >> array[counter];
}

cout << endl;


cout << "The numbers are: ";

for(counter = 0; counter < 5;counter++)
{
cout << array[counter] << " ";
}

cout << endl;



cout << "The numbers in reverse order are: ";

for (counter = 4; counter >= 0; counter--)
cout << array[counter] << " ";

cout << endl;


//Next, add code to read 5 words from the Console (user input).
//Store these values into an array of string [].
//Make sure the string[] array is large enough to hold at least 6 values.
//Store the string constant “end_of_array” into the last element of the array. //
//Using a do…while() loop, print out the 1st and 3rd letters of each
//word (two letters per line) using the substring function.

//clean any otstanding stuff from standard input
char trash[256];
cin.getline(trash,256,'\n');



cout << "Please enter five words: ";

for (counter = 0; counter < 5; counter++)
{
cin >> words[counter];

}
words[5]=endWord;


counter=0;
cout << "Print out 1st and 3rd letter of each word: " << endl;

do
{
cout << words[counter].substr(0,1);
cout << words[counter].substr(2,1);
cout << endl;
counter++;

}while( words[counter]!=endWord);



//create text files(numbers.txt),then write values for reverse order second file, third file reads the words.Displaying the
//results to the console as described above.
//Not understanding how to piece this part together for the text file.

ifstream inData;
ofstream outData;


inData.open("numbers.txt");
outData.open("output.txt");

inData >> counter;
inData >> words[6];

outData << "Five numbers " << counter << endl;
outData << "Reverse numbers " << counter << endl;
outData << "Five words " << trash << endl;



inData.close();
outData.close();




return 0;
}



Last edited on
Topic archived. No new replies allowed.