- Joined
- Nov 19, 2020
- Posts
- 2,683
- Reaction
- 4,446
- Points
- 1,546
Example code of input fields for last name , first name and middle name that transfers data to excel after clicking save using visual c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string firstName, lastName, middleName;
cout << "Enter first name: ";
getline(cin, firstName);
cout << "Enter middle name: ";
getline(cin, middleName);
cout << "Enter last name: ";
getline(cin, lastName);
string fileName = "data.csv";
ofstream outputFile;
outputFile.open(fileName, ios::app);
outputFile << firstName << "," << middleName << "," << lastName << "\n";
outputFile.close();
return 0;
}
what if I want it to be separated by different colums?Here's an example code for input fields for last name, first name and middle name that transfers data to Excel after clicking save using Visual C++:
C++:#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string firstName, lastName, middleName; cout << "Enter first name: "; getline(cin, firstName); cout << "Enter middle name: "; getline(cin, middleName); cout << "Enter last name: "; getline(cin, lastName); string fileName = "data.csv"; ofstream outputFile; outputFile.open(fileName, ios::app); outputFile << firstName << "," << middleName << "," << lastName << "\n"; outputFile.close(); return 0; }
This code prompts the user to enter the first name, middle name, and last name, and saves it to a CSV file named "data.csv". The data is separated by commas and each record is separated by a new line character.
You can customize this code to fit your needs and integrate it with Excel by importing the CSV file.
To separate the data by different columns, you can modify the code to use a different delimiter. For example, if you want to separate the data by tabs, you can use the '\t' character as the delimiter by replacing the comma in the output stream.what if I want it to be separated by different colums?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string firstName, lastName, middleName;
cout << "Enter first name: ";
getline(cin, firstName);
cout << "Enter middle name: ";
getline(cin, middleName);
cout << "Enter last name: ";
getline(cin, lastName);
string fileName = "data.csv";
ofstream outputFile;
outputFile.open(fileName, ios::app);
outputFile << firstName << "\t" << middleName << "\t" << lastName << "\n";
outputFile.close();
return 0;
}