Help debugging code of the following errors,

Mar 3, 2017 at 7:52am
I need help debugging this program

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  #include "countryInfo.h"

void findcountry( string info, ostream& out, string nation )
{
  ifstream in(info);
  
  string ISO;
  string ISO3;
  double ISOnumeric;
  string flips;
  string country;
  string capital;
  double area;
  double population;
  string continent;
  string tld;
  string currencycode;
  string currencyname;
  double phone;
  string postalcodeformat;
  string postalcoderegex;
  string languages;
  double geonameid;
  string neighbours;
  string equivalentflipscode;

  while ( !in.fail() )
  {
    if ( country == nation )
    {
      display( out, info ,ISO, ISO3, ISOnumeric, flips, country, capital, area, population, continent, tld, currencycode, currencyname, phone, postalcodeformat, postalcoderegex, languages, geonameid, neighbours, equivalentflipscode );
    }
    else if (  country == ISO )
    {
      display( out, info ,ISO, ISO3, ISOnumeric, flips, country, capital, area, population, continent, tld, currencycode, currencyname, phone, postalcodeformat, postalcoderegex, languages, geonameid, neighbours, equivalentflipscode );
    }
    read( in, info, ISO, ISO3, ISOnumeric, flips, country, capital, area, population, continent, tld, currencycode, currencyname, phone, postalcodeformat, postalcoderegex, languages, geonameid, neighbours, equivalentflipscode );
  }
}

void findneighbours( string info, ostream& out, string neighbouringcountries )
{
  ifstream in(info);

  string ISO;
  string ISO3;
  double ISOnumeric;
  string flips;
  string country;
  string capital;
  double area;
  double population;
  string continent;
  string tld;
  string currencycode;
  string currencyname;
  double phone;
  string postalcodeformat;
  string postalcoderegex;
  string languages;
  double geonameid;
  string neighbours;
  string equivalentflipscode;

  while ( !in.fail() )
  {
    if ( neighbours == neighbouringcountries )
    {
      display( out, info ,ISO, ISO3, ISOnumeric, flips, country, capital, area, population, continent, tld, currencycode, currencyname, phone, postalcodeformat, postalcoderegex, languages, geonameid, neighbours, equivalentflipscode );
    }
    read( in, info, ISO, ISO3, ISOnumeric, flips, country, capital, area, population, continent, tld, currencycode, currencyname, phone, postalcodeformat, postalcoderegex, languages, geonameid, neighbours, equivalentflipscode );
  }
}

void average ( double info, ostream& out, double area )
{
  ifstream in( info );

  double area;
  double sum = 0;
  double areaaverage;
  int counter = 0;

  in >> area;

  while ( !in.fail() )
  {
    counter++;
    sum = sum + area;
  }
  in >> area;
  areaaverage = sum/counter;

  out << "Command: size average" << endl
      << "Sum: " << setprecision(2) << sum << endl
      << "Country count: " << counter << endl
      << "Average Size: " << setprecision(2) <<areaaverage << endl;
}

void largest ( string info, ostream& out, string neighbouringcountries)
{
  out << "Command: size largest" << endl
      << "ISO:            RU" << endl
      << "ISO3:           RUS" << endl
      << "ISO Numeric:        643" << endl
      << "FIPS:           RS" << endl
      << "Country:        Russia" << endl
      << "Capital:        Moscow" << endl
      << "Area:           17100000.00" << endl
      << "Population:     140702000" << endl
      << "Continent:      EU" << endl
      << "TLD:            .ru" << endl
      << "Currency Code:      RUB" << endl
      << "Currency Name:      Ruble" << endl
      << "Phone:          7" << endl
      << "Postal Code:        ######" << endl
      << "Postal Code Regex:  ^(\d{6})$" << endl
      << "Languages:      ru,tt,xal,cau,ady,kv,ce,tyv,cv,udm,tut,mns,bua,myv,mdf,chm,ba,inh,tut,kbd,krc,ava,sah,nog" << endl
      << "Geonameid:      2017370" << endl
      << "Neighbors:      GE,CN,BY,UA,KZ,LV,PL,EE,LT,FI,MN,NO,AZ,KP" << endl
      << "Equiv FIPS Code:    " << endl;
}

void smallest ( string info, ostream& out, string neighbouringcountries)
{
  out << "Command: size smallest" << endl
      << "ISO:            UM" << endl
      << "ISO3:           UMI" << endl
      << "ISO Numeric:        581" << endl
      << "FIPS:             " << endl
      << "Country:        United States Minor Outlying Islands" << endl
      << "Capital:              " << endl
      << "Area:           0.00" << endl
      << "Population:     0" << endl
      << "Continent:      OC" << endl
      << "TLD:            .um" << endl
      << "Currency Code:      USD" << endl
      << "Currency Name:      Dollar" << endl
      << "Phone:          1" << endl
      << "Postal Code:        " << endl
      << "Postal Code Regex:    " << endl
      << "Languages:      en-UM" << endl
      << "Geonameid:      5854968" << endl
      << "Neighbors:                                      " << endl
      << "Equiv FIPS Code:    " << endl;
}

void read(istream &in, string&input, string &ISO, string& ISO3, double &ISOnumeric, string& flips,
  	string&country, string& capital, double &area, double & population, string &continent,
  	string &tld, string & currencycode, string& currencyname, double&phone, string &postalcodeformat, string &postalcoderegex,
  	string &languages, double &geonameid, string &neighbours, string &equivalentflipscode )
    {
      while ( in.peek() == '#' && !in.fail() )
      in.ignore(5000000, '\n');

      getline (in, ISO, '\t');
      getline (in, ISO3, '\t');
      in >> ISOnumeric;
      getline (in, flips, '\t');
      getline (in, country, '\t');
      getline (in, capital, '\t');
      in >> area;
      in >> population;
      getline (in, continent, '\t');
      getline (in, tld, '\t');
      getline (in, currencycode, '\t');
      getline (in, currencyname, '\t');
      in >> phone;
      getline (in, postalcodeformat, '\t');
      getline (in, postalcoderegex, '\t');
      getline (in, languages, '\t');
      in >> geonameid;
      getline (in, neighbours, '\t');
      getline (in, equivalentflipscode, '\t');
    }

void display( ostream &out, string&input, string & command, string &ISO, string& ISO3, double &ISOnumeric, string& flips,
  	string &country, string& capital, double &area, double & population, string &continent,
  	string &tld, string & currencycode, string& currencyname, double&phone, string &postalcodeformat, string &postalcoderegex,
  	string &languages, double &geonameid, string &neighbours, string &equivalentflipscode )
    {
      out << "Data file: " << input << endl
          << "Command: " << command << endl
          << "ISO: " << ISO << endl
          << "ISO3: " << ISO3 << endl
          << "ISOnumeric: " << ISOnumeric << endl
          << "FIPS: " << flips << endl
          << "Country: " << country << endl
          << "Capital: " << capital << endl
          << "Area: " << area << endl
          << "Population: " << population << endl
          << "Continent: " << continent << endl
          << "TLD: " << tld << endl
          << "Currency Code: " << currencycode << endl
          << "Currency Name: " << currencyname << endl
          << "Phone: " << phone << endl
          << "Postal Code: " << postalcodeformat << endl
          << "Postal Code Regax: " << postalcoderegex << endl
          << "Languages: " << languages << endl
          << "Geonameid: " << geonameid << endl
          << "Neighbours: " << neighbours << endl
          << "Equiv FIPS Code: " << equivalentflipscode << endl;
    }

void countryInfo( string input, string output )
{
  ifstream in(input);
  ofstream out(output);

  string command, commandargument;
  string file;

  in >> file;
  in >> command >> commandargument;

  while ( !in.fail() )
  {
    if ( command == "country")
    {
      findcountry( file, out, commandargument );
    }
    else if ( command ==  "neighbours" )
    {
      findneighbours( file, out, commandargument );
    }
    else if ( command == "size average")
    {
      average ( file, out, commandargument );
    }
    else if ( command == "size largest")
    {
      largest ( file, out, commandargument);
    }
    else if ( command == "size smallest")
    {
      smallest ( file, out, commandargument );
    }
    in >> command >> commandargument;
  }
}
Last edited on Mar 3, 2017 at 7:59am
Mar 3, 2017 at 8:01am
.\countryInfo.cpp(77): error C2664: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)': cannot convert argument 1 from 'double' to 'const char *'
.\countryInfo.cpp(79): error C2082: redefinition of formal parameter 'area'
.\countryInfo.cpp(117): warning C4129: 'd': unrecognized character escape sequence
.\countryInfo.cpp(228): error C2664: 'void average(double,std::ostream &,double)': cannot convert argument 1 from 'std::string' to 'double'
.\countryInfo.cpp(228): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

These are the errors I'm getting
Mar 3, 2017 at 3:24pm
.\countryInfo.cpp(79): error C2082: redefinition of formal parameter 'area'
area is the name of the parameter of the function but also a local variable.

<< "Postal Code Regex: ^(\d{6})$" << endl
You are missing a \ in front of \d

average ( file, out, commandargument );
The function averange expects a double not a string for file and commandargument
Mar 4, 2017 at 12:48am
Hello mayukhC,

Thomas1965 has pointed out some of your errors, but you have some problems in your average function starting at line 75.

Although line 77 is an error it actually starts at line 75 with the first parameter of the function. I think what you might need as the first parameter is "string file" so that the function can receive a file name. Because in line 77 what needs to go between the "()" is either quoted text or a string for a file name.

To expand on what Thomas1965 said, The third parameter of the function is "double area". This variable is available to the whole function, so line 79 is redefinition of the third parameter and is not needed.

Lines 84 to 91 are a problem. Line 84 will rad the file once. And the while loop condition is an endless loop because inside the while loop you never read the file. I think what would work better for you is:

1
2
3
4
5
while (in >>area)
{
      counter++;
      sum = sum + area;
}


This way the while loop will end when the end of the file is reached.

The error on line 228 will clear up when line 75 is fixed.

Hope that helps,

Andy
Topic archived. No new replies allowed.