0

The purpose of the code is to prompt the user to enter how many connections/nets & write to a CSV file.

The code runs and writes to the CSV file on the first column but after the user is prompted to input the # of nets again, it writes on the same column instead of the next one which has the header "Connector 2" or Column B.

How do I get my code to write to the first column, move on to the next column and prompt the user to input the # of nets/connections?

Here is my code

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

using namespace std;

int main() {

int nets;

ofstream myFile;
myFile.open("My File.csv"); // Create file

myFile << "Connector 1" << "," << "Connector 2" << endl; // Headers

 while (true) {

    cout << "# of Nets: " << endl;
    cin >> nets;

    if (nets > 0) {

        for (int i = 1; i <= nets; i++) {

            myFile << i << endl;
            myFile << ",";
        
        }
    }

    else {

        break;

        }
    }
}
sfjr90
  • 3
  • 1
  • 3
  • If you don't want to move on to the next record, don't use `endl` in your loop. – Stephen Newell Oct 13 '20 at 05:04
  • Actually, don't use `endl` at all unless you explicitly want to flush the stream while writing a newline. Develop the habit of using `'\n'` instead. – paddy Oct 13 '20 at 05:08
  • You want to check `if (!myFile.open()) { /* handle error */ }` before you attempt to write to it. Why you don't want `std::endl`, see [C++: “std::endl” vs “\n”](https://stackoverflow.com/questions/213907/c-stdendl-vs-n) – David C. Rankin Oct 13 '20 at 05:08
  • Why not `for (int i = 0; i < nets; i++) { std::cout << "enter conn1 conn2: "; if (std::cin >> conn1 >> conn2) myFile << conn1 << "," << conn2 << '\n';`? – David C. Rankin Oct 13 '20 at 05:11

1 Answers1

2

The problem you have is that you want to write the data column by column while the CSV format reads them row by row. One way to make your program work is to store the data until the user is done, and then store it row by row. Ex :

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

using namespace std;

int main() {

    int nets;
    vector<int> netsList; // Will be used to save the numbers until the user is done
    int maxValue = -1; // The biggest value entered by the user, you could also look in the vector for this value

    ofstream myFile;
    myFile.open("My File.csv"); // Create file

    myFile << "Connector 1" << "," << "Connector 2" << endl; // Headers

    while (true) {

        cout << "# of Nets: " << endl;
        cin >> nets;

        if (nets > 0) {

            netsList.push_back(nets); // Save the number for later
            if (nets > maxValue) // Update the maximum value
                maxValue = nets;
        }
        else {

            // Now write to the file, you could open the file now if you wanted
            for (int i = 1; i <= maxValue; i++) // Loop until the biggest value is reached (these will be rows)
            {
                for (auto num : netsList) // Loop all the elements in the vector (columns)
                {
                    if (num >= i) // If the value entered is greater or equal to the current i (row) value
                        myFile << i << ","; // Write the number
                    else
                        myFile << " " << ","; // Write an empty case
                }
                myFile << "\n";
            }

            myFile.close(); // Close the file
            break; // Now quit
        }
    }
}