171k views
3 votes
Write a simple compilable program that outputs to an Excel-compatible XLS file these two rows: (1) a header row with headings for each column and (2) a data row with one set of data. You may have addition data rows if you wish.

Include at least 3 columns of information of any kind you like, with real or made-up data. For example, a student record, a stock quote, a weather forecast, etc.
Hint: use the character \t for column breaks and \\ for row breaks.

1 Answer

2 votes

Answer:

#include <iostream>

#include <fstream>

using namespace std;

struct student

{

string name;

int id;

double average;

};

int main()

{

string filename = "data.csv";

student s[3] = { {"Peter", 101, 65.4},

{"Mary", 102, 60.4},

{"Jed", 103, 55.4}};

ofstream outFile(filename.c_str());

outFile << "Name,ID,Average" << endl;

for(int i = 0; i < 3; i++)

{

outFile << s[i].name << "," << s[i].id << "," << s[i].average << endl;

}

outFile.close();

cout << "Please open " << filename << " in Excel" << endl;

}

Step-by-step explanation:

Name ID Average

Peter 101 65.4

Mary 102 60.4

Jed 103 55.4

User HMM
by
5.5k points