0

I have a project that requires me to output the data from 2 files. Put them into their own arrays. Calculate the data from 1 of the files into a function. Then output the functions into a whole new file. This is what I have so far and right now I'm trying to figure out how to output the function output into a new file. When I try now, I get outputs in the file like "NULL NULL". I also have to align the data in columns.

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

using namespace std;

char states();
char stData();

char states()
{
    const int SIZE = 0;
    ifstream myfile("csc114_states.txt");
    string line, myArray[SIZE];
    if (myfile.is_open())
    {
        for (int i = 1; i < SIZE; i++)
        {
            myfile >> myArray[i];
            cout << myArray[i];
            cout << endl;
        }
        myfile.close();
    }
    myfile.open("csc114_states.txt");
    if (myfile.is_open())
    {
        string tp;
        while (getline(myfile, tp))
        {
            cout << tp << "\n";
        }
        myfile.close();
    }
    else
    {
        cout << "Unable to open file." << endl;
        return 0;
    }

    return 0;
}

char stDATA()
{
    const int size1 = 0;
    const int size2 = 0;
    string myArray2[size1][size2], line;
    ifstream myfile2("csc114_data.txt");

    if (myfile2.is_open())
    {
        for (int i = 0; i < size1; i++)
        {
            for (int j = 0; j < size2; j++)
            {
                myfile2 >> myArray2[i][j];
                cout << myArray2[i][j];
                cout << endl;
            }
        }
        myfile2.close();
    }
    myfile2.open("csc114_data.txt");
    if (myfile2.is_open())
    {
        string tp2;
        while (getline(myfile2, tp2))
        {
            cout << setw(0) << tp2 << "\n";
        }
        myfile2.close();
    }
    else
    {
        cout << "Unable to open file." << endl;
        return 0;
    }
    return 0;
}

int main()
{
    ofstream bigFile("csc114_output.txt");
    if (bigFile.is_open())
    {
        bigFile << states() << stDATA();

        bigFile.close();
    }
    else
    {
        cout << "Unable to open files.";
    }

    return 0;
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Adonis
  • 1
  • 2
  • 2
    `const int SIZE = 0; string line, myArray[SIZE];` - Zero sized arrays are invalid. Perhaps you should use a `std::vector myArray;` instead. – Ted Lyngmo Dec 10 '21 at 08:44
  • 2
    Fix the indentation, please. Your code is barely readable. – digito_evo Dec 10 '21 at 08:46
  • Using a vector might look similar to this: `std::vector v; std::string s; for(;;) { myfile >> s; v.push_back(std::move(s)); }` – and if you have a rough idea about how many strings might be in the file you might want to `v.reserve(estimatedNumber)` to reduce number of necessary re-allocations. – Aconcagua Dec 10 '21 at 09:09
  • You are just returning a single char value – and that one is set to 0, so that's all you can see in the output file then. Assume you switch to `std::vector`, then you should return that one and output it to file – though you cannot just `std::cout << theVector`, there's no suitable `operator<<` provided, thus you need to output the individual items on your own (-> loop). – Aconcagua Dec 10 '21 at 09:14
  • 1
    Off-topic: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Dec 10 '21 at 09:16
  • 1
    Some rules about indentation: Top-level units (`main`!) are not indented, units on same logical level indented equally (violated for very first `myfile.close();`), indentation *always* with the same number of spaces (you vary in between 2, 4 and 7), closing braces go onto their own line, same indentation level as opening one. – Aconcagua Dec 10 '21 at 09:40
  • 1
    Opening one *can* go onto its own line as well (-> [Allman style](https://en.wikipedia.org/wiki/Indentation_style#Allman_style)), that's what me personally prefers and that's what me personally has seen more often in foreign code as well, though admitted, that's not representative... If you remain with K&R/OTBS then I recommend adding a space in front of the opening brace for better readability. – Aconcagua Dec 10 '21 at 09:40
  • 1
    For visualisation: I adjusted formatting for you – now Allman style which is what my auto-formatter does (not going to change that one), if you prefer OTBS (opening braces on previous line) please re-adjust yourself. See the difference? Wouldn't you agree that now it's easier to read? – Aconcagua Dec 10 '21 at 09:48

0 Answers0