Urgent read from file

Hi,

My task is to create like little databe which will hold date continent, country, citizen count.
Data must be writen in file and then it has to be read from file and printed on screen.
Also have to create a function which will calculate avarage citizen count in continent.

I think function addWorld is working correctly.
I have two problems.
1. i have an issue reading that information from file.
2. function is not working which should calculate avarage citizen count in continent, i think this is related to view issue.

Could you please review my code and tell me whats wrong in exact place preferable with code. already long way over deadline, but still fighting to solve this.

Thank you

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int n;
int total = 0;
void addWorld();
void viewWorld();
void funct();
void deleteWorld();

struct world_
{
	char continent[15];
	char country[15];
	int avg_cit;

}world;

int main()
{
	int option = 0;
	enum menuitems{ view = 1, add = 2, func = 3, del = 4, exit = 5 };

	while (option != exit)
	{
		cout << endl;
		cout << setw(50) << " Chose an option " << endl << endl;
		cout << setw(25) << view << " View entry's" << endl;
		cout << setw(25) << add << " Add new entry's" << endl;
		cout << setw(25) << func << " Calculate avarage citizen count in continent" << endl;
		cout << setw(25) << del << " Delete current data in file " << endl;
		cout << setw(25) << exit << " Exit!" << endl;

		cin >> option;

		switch (option)
		{
		case view: viewWorld();
			break;
		case add: addWorld();
			break;
		case func: funct();
			break;
		case del: deleteWorld();
		}

	}

	cout << endl << endl;
	return 0;
}

void addWorld()
{

	ofstream outfile("world.txt", ios::out | ios::app);

	if (!outfile)
	{
		cerr << " Error creating file! " << endl;
	}
	else
	{
		cerr << " File opened successfully! " << endl;

		cout << " How many entry's you would like to add? ";
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin.ignore();
			cout << " Enter continent: ";
			cin.getline(world.continent, 15);
			cout << " Enter country: ";
			cin.getline(world.country, 15);
			cout << " Enter citizen count: ";
			cin >> world.avg_cit;

		
			outfile <<  world.continent << endl;
			outfile <<  world.country << endl;
			outfile <<  world.avg_cit << endl;
			
			outfile << string(22, '-') << endl;
			outfile.flush();

			total = total + world.avg_cit;


		}

		cerr << " Data successfully writen to file " << endl;
		outfile.close();
		cerr << " File closed! " << endl;
	}
}

void viewWorld()
{

	ifstream infile("world.txt", ios::in);

	//	char buf[30];

	if (!infile)
	{
		cerr << " Error opening file! " << endl;
	}
	else
	{
		while (!infile.eof())
		{			
				infile >> world.continent;
				cout << " Continent: " << world.continent << endl;
				infile >> world.country;
				cout << " Country: " << world.country << endl;
				infile >> world.avg_cit;
				cout << " Citizen count: " << world.avg_cit << endl;

				break;		
			
		}
	}
}

void funct()
{
	ifstream infile("world.txt", ios::in);
	if (!infile)
	{
		cerr << " Error opening file! ";
	}
	else
	{
		
		char search[15];
		cout << " Search for continent: ";
		cin.getline(search, 15);
		cin.ignore();

		int average = 0;
		int counter = 0;
		for (int i = 0; i < n; i++)
		{
			counter++;
			if (strcmp(world.continent, search) == 0)
				average = counter / total;

		}
		cout << " Avarage citizen count in " << search << " = " << average;

	}


}

void deleteWorld()
{
	ofstream outfile("world.txt", ios_base::trunc);
	outfile.close();
	cout << " Content sucessfully deleted ";
}





Last edited on
Anyone?
1. i have an issue reading that information from file.
What is this issue?

2. function is not working which should calculate avarage citizen count in continent, i think this is related to view issue.
You calculate total but not average (which would be total / n in your case)

but it prints it out in endles loop and only result for continent
That's not possible. Due to line 26 the loop ends after the first reading.
If you'd remove line 26. What would happen if line 20 detects the eof?

1. issue is that my code does not read correctly information from file after i have added it.
when i tried to use function viewWorld it just prints parts:

cout << "Continent";
cout << "Country";
cout << "Citizen count";

and also entry of Continent, but looks messy.
This is how it looks when i use viewWorld

Continent: Continent:
Country: EU
Citizen count: 0

And in file i see this, looks correctly like i need, but doesnt show like that when i use viewWorld.

Continent: EU
Country: LAT
Citizen count: 1
----------------------

2. Average i calculate like this.

1. in total im saving the count of citizens which have been entered.
2. then in funct() i use counter++ to see how many cycles have passed and then divide counter with total count of citizens.

average = counter / total;

but you are right i can just divide total with n.

Also im not sure if search function works in funct();

but i think this all doesn't matter right now if im not able view those entry's which i have added. :/





Well, take a look at line 82 in addWorld(): The first word you write is "Continent: ". Thus it shouldn't be a surprise that the the first word you read is also "Continent:".

Unless you have a good reason just don't write these 'header' words. No empty lines nor any other useless information.
Last edited on
Ok, i have updated addWorld(); now it does read correctly from file.

But another thing, how can i print out all records from file?

now it does only print out the first record from file all the time.

i tried to add this loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (!infile.eof())
			{
                              for(int i = 0; i < n ; i++);
                                {
				infile >> world.continent;
				cout << " Continent: " << world.continent << endl;
				infile >> world.country;
				cout << " Country: " << world.country << endl;
				infile >> world.avg_cit;
				cout << " Citizen count: " << world.avg_cit << endl;

				break;
				}
			}


so it would go through the file as many times as n was entered.
But instead the endless loop starts and prints out just the first result from file endlessly.

I think that loop for(int i = 0; i < n; i++) is not the best approach to print out many records.
Because if i start program for the first time and i want to view whats in the file it will just print out the first record because n haven't even been set.

So im also trying to figure out solution for this.
Yes, the for loop doesn't make sense. Remove it. Also remove the break;

I don't see a reason why it should print just the first lines from the file?

This should print the whole content:
1
2
3
4
5
6
while (infile >> world.continent >> world.country >> world.avg_cit)
			{
				cout << " Continent: " << world.continent << endl;
				cout << " Country: " << world.country << endl;
				cout << " Citizen count: " << world.avg_cit << endl;
			}
Ok, i added it as you said, removed break;

and looks like problem was causing this:

outfile << string(22, '-') << endl;

Dont know why but i will have to think how to make looks better when writing to file.

because now everything in file looks like this:

qwe
asd
2
qwe
asd
2
ewq
asdasd
1

:)

Well i'm getting there.


the last one.

void funct()

when i run it, the endless loop starts.

SO first what i want to fix in void funct() is search.

created new array search[15]

so i can save in search my entry and then compare it to world.continent

1
2
if (strcmp(world.continent, search) == 0)
			cout << search;


but after i launch the funct nothing happens, just endless loop.

If search is fixed then i will have to calculate average citizen count from continent which i have found.

Well, funct() (choose a better name, honestly) is similar to viewWorld(). I.e. use the same loop. Within the loop do this:
1
2
3
4
5
			if (strcmp(world.continent, search) == 0)
{
				counter++;
				average += world.avg_cit;
}

The calculation is something like this:

cout << " Avarage citizen count in " << search << " = " << average / counter;

Notice that counter can be 0. In this case the continent to search for couldn't be found.

you mean like this?

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
void avarageWorld()
{
	ifstream infile("world.txt", ios::in);
	if (!infile)
	{
		cerr << " Error opening file! ";
	}
	else
	{

		char search[15];
		cout << " Search for continent: ";
		cin.getline(search, 15);
		cin.ignore();

		int average = 0;
		int counter = 2;
		
		while (infile >> world.continent >> world.country >> world.avg_cit)
		{
			if (strcmp(world.continent, search) == 0)
			{				
				//counter++;
				average += world.avg_cit;
			}
		cout << " Avarage citizen count in " << search << " = " << average / counter;
		}

		
		
	}
	
}


i set counter to for example 2, to see if function works, latter on will have to figure out how can increase that counter if two identical continents have been added and then use it as counter.

i think i have wrote something wrong, it doesn't even find the continent which i do input and then compare in if statement.

just endless loop again. :/
as it turns out cin.ignore(); was it not right place :) it had to be above cin.getline(search, 15);

Thank you for your help.
Topic archived. No new replies allowed.