Note that printf wants a cstring and not std::string. You must use std::string's method, c_str() w/c returns it's c-string equivalent. fprintf_s( pfile, "%s,%s\n", students[i].name.c_str(), students[i].house.c_str() );
You also won't need typedef in the struct definition, you can write struct student { }; instead.
#include <iostream>
#include <fstream>
#include "structs.h"
usingnamespace std;
int main(void)
{
constint STUDENTS = 3; // or constexpr in C++11
student students[STUDENTS]; // you can also use std::vector<student> for a resizable array
// populate students with user input
for (int i = 0; i < STUDENTS; i++)
{
cout << "Student's name: ";
cin >> students[i].name;
cout << "Student's house: ";
cin >> students[i].house;
}
// save students to disk
ofstream file( "students.csv" );
for (int i = 0; i < STUDENTS; i++)
{
file << students[i].name << ',' students[i].house << '\n';
}
}
An absolutely fantastic answer. I have learned so much. I now know the difference between a c-string and a c++-string and how important it is to know and understand the syntax.