Just need to get this to output in reverse.

Category: C++ Questions    |    1 views
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.

Share/Save/Bookmark

1 Star2 Stars
Loading ... Loading ...

0 responses so far!

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word