User input into Specified CSV Column

Hello all, I've been lurking on this forum for about a month or so as I've been learning C++ and you have all be a wealth of information here. I finally got stumped enough that I needed some help.

I'm trying to create a program that will take user input and put it into a CSV file. While doing so I got everything to work, except I want the program to put each user input into a column rather than down the first row. Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;



int main()
{
	//Setting the Getline command
	string mystr;
	
	//OPen Excel File
ofstream MyExcelFile;

	//Set the name of the CSV File
MyExcelFile.open("Pole Survey.csv", ios::out | ios::ate | ios::app) ;

	//Header for Program, please update as things get changed
cout << "Welcome to Pole Survey 1.0";
cout << "" << endl << "";
cout << "" << endl << "";

	//Pole Number request:
cout << "Pole Number:";
getline (cin,mystr);
MyExcelFile << mystr << endl;




	//Birth Mark request:
cout << "Birth Mark ";
getline (cin,mystr);
MyExcelFile << mystr << endl;



	//Street Address request:
cout << "Street Address ";
getline (cin,mystr);
MyExcelFile << mystr << endl;



	//Excel file gets closed:
MyExcelFile.close();
cout << "" << endl << "";
cout << "" << endl << "";



	//Lets me know the file was saved, remove once program is in iOS:
cout << "Account Details Saved";
return 0;
}


Essentially I want the program to put the Pole Number, Birth Mark, Street Address etc to a CSV file like this:

Pole Number  Birth Mark  Street Address
Pole Number  Birth Mark  Street Address
Pole Number  Birth Mark  Street Address


Instead of how it does now:
Pole Number
Birth Mark
Street Address
Pole Number
Birth Mark
Street Address


If you can show me how to go about this I'd greatly appreciate it!
Normally a CSV file has the fields separated by a comma, that's the C in CSV.

If you don't want every thing on it's own line you shouldn't be using the endl.

Something like:

FileStream >> variable1 >> ',' >> variable2 >> ',' >> lastVariable >> endl;

Would probably be what you're looking for.
Last edited on
I apologize if this is completely stupid but I don't quite follow what you mean.

I tried putting everything to "\n" instead of endl and that didn't change anything.

Now I will say, and again, dealing with csv is new to me as is FileStream so I apologize if I'm completely ignorant here but I'm not sure where to use the FileStream code you gave me above. If you can tell me where to use it that'd be great.
std::endl or '\n' is used to end a line. If you want to do something other than end a line, then output something other than std::endl or '\n'.

1
2
3
4
5
6
7
8
9
10
11
    cout << "Pole Number:";
    getline(cin, mystr);
    MyExcelFile << mystr << ',';

    cout << "Birth Mark ";
    getline(cin, mystr);
    MyExcelFile << mystr << ',';

    cout << "Street Address ";
    getline(cin, mystr);
    MyExcelFile << mystr << '\n';
The '\n' is basically the same as endl, although endl does also flush the output buffer. If you don't want everything on it's own line, don't print this new line character (endl/ '\n').

You may want to study the following link: http://www.cplusplus.com/doc/tutorial/files/

Jim


Topic archived. No new replies allowed.