What is the problem ? - only commas are being written to csv

I have dwelled in this program, someone tell me why is it that the code below is not writing the user data to the csv format text file ? - It is only writing the commas:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

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

using namespace std;


struct LandDetails { // declaring a struct

string Surname, OtherNames, LandNum,KraPin,OwnerIdCard, address,LandCounty, LandDist, LandDiv, Landloc, LandSubloc;

} entry;

LandDetails Fill_struct (LandDetails &entry);   //prototype---------inputs user data
void PrintEnteredDetails (LandDetails &enter);//prototype-----------displays entered data
void write_file (ofstream &openFile);  //prototype -----------writes entered data to a text file of csv format

int main (){
ofstream openFile ("Land Registry.txt", ios::app);

LandDetails create;

Fill_struct (create);
PrintEnteredDetails (create);
write_file (openFile);

system ("cls");
return 0;
}

LandDetails Fill_struct (LandDetails &entry){

cout<<"Welcome to the new lands management information system\n\n";


{
cout<<"Enter Surname \n";
getline (cin,entry.Surname);

cout<<"Enter other names\n";

getline (cin,entry.OtherNames);

cout<<"Enter Land number\n";
        getline (cin,entry.LandNum);

cout<<"Enter owner KRA pin\n";
	getline (cin,entry.KraPin);

cout<<"Enter Owner id card number\n";
	getline (cin, entry.OwnerIdCard);

cout<<"Enter owner address\n";
		getline (cin, entry.address);

cout<<"Enter Land county\n";
	
	getline (cin, entry.LandCounty);

cout<<"Enter Land district\n";
	getline (cin, entry.LandDist);

cout<<"Enter Land division\n";
	getline (cin, entry.LandDiv);

cout<<"Enter land location\n";
	getline (cin, entry.Landloc);


cout<<"Enter Land sub-location\n";
	getline (cin, entry.LandSubloc);



return entry;

} 
}

void PrintEnteredDetails (LandDetails &enter){

cout<<"______________________________________________________________________________"<<"\n";

cout<<"You have entered the following details:\n\n";

cout<<"Surname:"<<enter.Surname<<setw(3)<<"        "<<"OtherNames:"<<enter.OtherNames<<setw(3)<<"         "<<"Owner Id number:"<<enter.OwnerIdCard<<"\n";
	
cout<<"Land number:"<<enter.LandNum<<setw(3)<<"               "<<"KRA pin:"<<enter.KraPin<<"\n";


cout<<"Postal address:"<<enter.address<<setw(3)<<"               "<<"County:"<<enter.LandCounty<<"\n";


cout<<"District:"<<enter.LandDist<<setw(3)<<"               "<<"Division:"<<enter.LandDiv<<"\n";


cout<<"Location:"<<enter.Landloc<<setw(3)<<"               "<<"Sub-location:"<<enter.LandSubloc<<"\n";

cout<<"______________________________________________________________________________"<<"\n";

}

void write_file (ofstream &openFile) {

	

	//int i=0, no_of_members = 10;

	if (!openFile ){

		cout<<"Land register coud not be opened !";
		return;

	}

	else 
	{

		//for (i=0; i<no_of_members; i++)
         openFile<<entry.Surname<<","<<entry.OtherNames<<","<<entry. LandNum<<","<< 
		entry.KraPin<<","<<entry.OwnerIdCard<<","<< entry.address<<","<< 
		 entry.LandCounty<<","<< entry.LandDist<<","<< entry. LandDiv<<","<<
		 entry. Landloc<<","<<entry.LandSubloc<<endl;

	}

	cout <<"All members data have been successfully saved"<<endl;

	openFile.close ();

	}


	
Last edited on
When you define your LandDetails struct you also define a global LandDetails object named entry.

In most of your functions, you have a parameter named entry which is a reference to the LandDetails object in main that is named create. This object in main is the one that is populated. The global object entry is hidden by the local entry variable in the functions that take a reference to an object of type LandDetails as an argument.

However, in write_file you access the empty global LandDetails object named entry.
Last edited on
Oh my, thanks Cire ....It is working now !
I am trying to read and display the whole of the csv text file but it doesnt do, it even throws "access violation" error, i dont know what am doing wrong to this vector.... kindly correct me in the below function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void read_file (){

	std::vector<std::string> inserted;
	string readTo;

	ifstream opener ("Land Registry.txt");

	while (getline (opener, readTo ));

	{
		cout<<"Reading to display the whole register";

		inserted.push_back (readTo);
	
		std::vector<std::string> ::iterator it =  (inserted.begin (), inserted.end (), it++);

			cout<<*it;

	}

}

What are you trying to do on line 15?

If you want to see the last element inserted use back():

cout<<inserted.back();

http://www.cplusplus.com/reference/vector/vector/back/
I wanted to read the whole csv file .... i have done it with the below function but seems like an endless loop. Where do i need to modify it please ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void read_file (){

	string readTo;

	ifstream opener ("Land Registry.txt");

	if(!opener)
	{
	cout<<"Problem opening register to read";
	return;
        }
	else
	{
	while (opener>>readTo)


cout<<readTo;
	
	}
}

Another thing, i am reading 15 strings per row (all separated by commas), how can i improve the appearance of the output (of the whole displayed file), for example by using iomanip or any other object (may be sstreams) for the displayed file to appear like a table of fifteen columns (with the entered data) and undefined rows ?.
Last edited on
closed account (48T7M4Gy)
Why not try it yourself?

Perhaps if you take advantage of the tutorials and reference material on this site you might be able to make an attempt at writing the formatting code and then be in a position to narrow down any problems you might have instead of just asking people to write your program for you. You might even like to consider engaging someone on a fee for service basis.

Also, since you are asking about the same program I think it would be easier for all concerned to see the fully history of problems you are raising and keep to one thread. :)
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/193657/
Topic archived. No new replies allowed.