Here's the code:

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

string getData(ifstream &fin, string array[]); 

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

void outputData(ofstream &fout, string array[], int numNames); 

//void outputData(ofstream &fout, string array[], int numNames) 



int main()
{
    ifstream fin;
    ofstream fout;

    fin.open("names.txt"); 
    //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 nN=350; //int nN=350;
    string array[350];
   
    getData(fin, array);

    outputData(fout, array, nN);

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

string getData(ifstream &fin, string array[])
{
   int i=0;
   while(i<350 && fin >> array[i])
       i-1;

    return array[i];
}

//string getData(ifstream &fin, string array[]) 
//{
//   int i=0;
//   while(i<listofnames && fin >> array[i])
//       i++;
//
//    return i;
//}

void outputData(ofstream &fout, string array[], int numNames) 
{
    for(int i=0;i<numNames;i++)
        fout << array[i] << endl;
        cout << "Did it work?\n";
}

//void outputData(ofstream &fout, string array[], int numNames) 
//{
//    for(int i=0;i<numNames;i++)
//        fout << array[i] << endl;
//        cout << "Did it work?\n";
//}

All I need to do is get the output made by this code to display in reverse order. Anyone know how to do that? Anything marked by a "//" isn't being used in the program.

Recommended Answers

All 3 Replies

I explained how to do it in your other thread. Use a loop with an int counter that starts at the highest valid index and decrements the counter by one each time through the loop, just like if I told you to count backwards by one starting at 10 until you got to zero. In the body of the loop output the element whose index is the counter of the loop.

I explained how to do it in your other thread. Use a loop with an int counter that starts at the highest valid index and decrements the counter by one each time through the loop, just like if I told you to count backwards by one starting at 10 until you got to zero. In the body of the loop output the element whose index is the counter of the loop.

Care to elaborate? I'm doing this:

void outputData(ofstream &fout, string array[], int numNames) 
{
    int i=0, j=350, k=0;
    while(i<=j)
    {
        i++;
        k=j-i;
    }
    fout << array[k] << endl;
    //cout << "Did it work?\n";
}

And this isn't working.

Go back to your other thread. The syntax has been demonstrated there. Or try this from within a program.

const int MAX = 3;
int array[MAX];
for(int i = 0; i < MAX; ++i)
  array[i] = i;
for(int j = MAX - 1; j > -1; --j)
  cout << array[j] << endl;
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.