Here is the code:

// All this has to do is take the "names.txt" file, and send it to "backwards.txt"
// in reverse order, as an array. No idea why its not working, sent all necessary files with it.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

const int listofnames = 350; // This is the number of spaces in the array.

int getData(ifstream &fin, int array[]); // Here is the function that finds each piece of the array.

//string getData(ifstream &fin, string array[]); This is what it SHOULD look like.

void outputData(ofstream &fout, int array[], int numNames); // This is the output function.

//void outputData(ofstream &fout, string array[], int numNames) Also what this one should look like

//Please note, I'm only guessing right now as to what everything should be.

int main()
{
    ifstream fin;
    ofstream fout;

    fin.open("names.txt"); // I need to use "names.txt", to display each name as apart of the array.
    //fin.open("numbers.txt");
    fout.open("backwards.txt");

    if(fin.fail())
    {
        cerr << "Failed to open Input File, program terminated\n";
        exit(2);
    }
    if(fout.fail())
    {
        cerr << "Failed to open Output File, program terminated\n";
        exit(3);
    }

    int array[listofnames];
   
    int numNames=getData(fin, array);

    outputData(fout, array, numNames);

    fin.close();
    fout.close();
    return 0;
}

int getData(ifstream &fin, int array[]) // Here, being an int, it takes "i" and uses it as the variable to count each block of the array. See next.
{
   int i=0;
   while(i<listofnames && fin >> array[i])
       i++;

    return i;
}

//string getData(ifstream &fin, string array[]) // This is the non-functional string version
//{
//   int i=0;
//   while(i<listofnames && fin >> array[i])
//       i++;
//
//    return i;
//}

void outputData(ofstream &fout, int array[], int numNames) // This function (in int form) sends the output.
{
    for(int i=0;i<numNames;i++)
        fout << array[i] << endl;
        cout << "Did it work?\n";
}

//void outputData(ofstream &fout, string array[], int numNames) // This function (in string form) sends the output.
//{
//    for(int i=0;i<numNames;i++)
//        fout << array[i] << endl;
//        cout << "Did it work?\n";
//} For some reason this doesn't work.

All this has to do is take the strings in "names.txt", and place them in reverse order. Nothing I've tried has worked, and the closest I've gotten is placed in the "//" fields.

Can anyone help? All I really need to know is how to make this coding work for a string instead of a int. I cannot use anything else, rules are rules.

You may want to consider this process--

-Pull in lines from target read file and store them in a stack<string>
-pop strings from stack and write them to file

That's if the strings need to be listed in reverse order.

If you have to reverse the strings and list them the process can be done the same way except with a queue instead of a stack, and of course the queue would only store the reversed version of the string before writing to the file.

Hopefully this helps.

change array to be of type string, not type int.

to evaluate/scan/print an array backward start with the largest valid index used and decrement the index each time through the loop instead of increment from the beginning.

commented: Yes, that was a very brief and correct solution that meets the specs! +5

You may want to consider this process--

-Pull in lines from target read file and store them in a stack<string>
-pop strings from stack and write them to file

That's if the strings need to be listed in reverse order.

If you have to reverse the strings and list them the process can be done the same way except with a queue instead of a stack, and of course the queue would only store the reversed version of the string before writing to the file.

Hopefully this helps.

It sounds like your on to something, only I don't know what it is your saying :(

What do you mean "pull in lines from target read file and store them in a stack<string>"? You mean write a "while" statement, assigning the value of all the information in the column to a string? Wouldn't that go against the idea of using an array?

And whats a "queue"?

Use a loop that decreases its-self. for(int i = numNames; i > 0; i--)

commented: Aye, that would work! =) +5

It sounds like your on to something, only I don't know what it is your saying :(

What do you mean "pull in lines from target read file and store them in a stack<string>"? You mean write a "while" statement, assigning the value of all the information in the column to a string? Wouldn't that go against the idea of using an array?

And whats a "queue"?

My apologies.

Apparently I missed the portion of your first statement "I cannot use anything else, rules are rules." Please forget my previous comment.

Use a loop that decreases its-self. for(int i = numNames; i > 0; i--)

For some reason, and I don't know why, that crashes the code.

My time to code is up. Thanks for your help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.