programming read write files

programming read write files what to dooooo

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string Name="";
string capac="";
bool openFile (ifstream & inFile);
class Location {
public:
	string locationName;
	int storageCap;
//default constructor
	Location ()
	{
		locationName="";
		storageCap=0;
	}
//assignment operator
	Location (string name, int capacity)
	{
		locationName = name;
		storageCap = capacity;
	}
};
//code to open the file
bool openFile (ifstream & inFile)
{
	inFile.open ("locations.txt");
	if (! inFile.is_open())
	{
		cout<<"Open file operation failed!"<<endl;
		return false;
	}
	else 
	{
		cout<<"Open file operation succeeded!"<<endl;
		return true;
	}
}
//code to read the file 
typedef enum state {start_state, within_name, end_name, within_capacity, end_capacity, Error};

 	state current = start_state;
	Location object[1024];
		int i=0;
bool readFile( ifstream & readSurvey)
{
	
	char c='\0';	
	while (! readSurvey.eof())
	{
		readSurvey.get(c);
		if (current == start_state)
		{
			if (isalpha (c) !=0)
			{
				Name=Name+c;
				current= within_name;
			}
			else 
			{
				current= Error;
			}
		}
		else if (current== within_name)
		{
			if (isalpha (c)!=0 || c==' ')
			{
				Name= Name +c;
			}
			else if (c==':')
			{
				object[i].locationName= Name;
				Name="";
				current= end_name;
			}
			else 
			{
				current= Error;
			}
		}
		else if (current==end_name)
		{
			if (c=' ')
			{
				current= end_name;
			}
			else if (isdigit(c)!=0)
			{
				capac= capac+c;
				current= within_capacity;
			}
			else 
			{
				current = Error;
			}
		}
		else if (current= within_capacity)
		{
			if (isdigit (c) !=0)
			{
				capac= capac+c;
			}
			else if (c=' ')
			{
				current= end_capacity;
			}
		}
		else if (current== end_capacity)
		{
			int b= atoi(capac.c_str());
			object[i].storageCap= b;
			b=0;
			i=i+1;
			while (c!='\n')
			{
				readSurvey.get(c);
			}//to skip cube meters
		}
	}
	cout<<i<< c<<endl;
	if (current= Error)
	{
		cout<<false;
		return false;
	}
readSurvey.close();//closing file
return true;
}
//compute total 

int totalcal ()
{
	int num=0;
	int sum=0;
	while (num<i)
	{
		sum = sum+ object[num].storageCap;
		num=num+1;
	}
	return sum;
}
int totalcal ();
int main ()
{
	ifstream infile;
	openFile(infile);
	bool read= readFile(infile);
	if (read== true)
	{
		cout<<i;
		int s= totalcal ();
		cout<<s;
		return 0;
	}
	else 
	{
		return -1;
	}

}
52
Last edited on
In more than one place you have = instead of ==,
such as line 84 if (c=' ')
should be if (c==' ')
Topic archived. No new replies allowed.