Help FIX C++ PROGRAM // Header files #include <iostream> #include <iomanip> #inc
ID: 668803 • Letter: H
Question
Help FIX C++ PROGRAM
// Header files
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
// maximum size of array
const int MAX = 500;
// structure for Restaurant
struct Restaurant
{
string name;
double area;
int popCount;
double popDensity;
};
// function prototypes
int readData(Restaurant rests[]);
void printData(Restaurant rests[], int count);
void quickSort(Restaurant rests[], int left, int right);
void sortDataByDensity(Restaurant rests[], int count);
// start main function
int main()
{
// create an array of Restaurants
Restaurant rests[MAX];
int count = 0;
// read data from the input file
count = readData(rests);
// sort the data by population density
sortDataByDensity(rests, count);
// print data into the output file
printData(rests, count);
system("pause");
return 0;
} // end of main function
// readData function implementation
int readData(Restaurant rests[])
{
ifstream infile;
infile.open("pop.txt");
if(!infile)
{
cout << "Input file is not found!" << endl;
system("pause");
exit(EXIT_FAILURE);
}
string line;
int count = 0;
int pos = -1;
getline(infile, line);
while(infile)
{
pos = line.find('|');
rests[count].name = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find('|');
rests[count].area = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find('|');
rests[count].popCount = line.substr(0, pos);
line = line.substr(pos + 1);
rests[count].popDensity = line;
count++;
getline(infile, line);
}
return count;
} // end of readData function
// readData function implementation
void sortDataByDensity(Restaurant rests[], int count)
{
quickSort(rests, 0, count - 1);
} // end of printData function
// quickSort function implementation
void quickSort(Restaurant rests[], int left, int right)
{
int i = left;
int j = right;
Restaurant pivot = rests[(left + right) / 2];
while (i <= j)
{
while(rests[i].popDensity < pivot.popDensity)
i++;
while(rests[j].popDensity > pivot.popDensity)
j--;
if(i <= j)
{
Restaurant temp = rests[i];
rests[i] = rests[j];
rests[j] = temp;
i++;
j--;
}
};
if (left < j)
quickSort(rests, left, j);
if (i < right)
quickSort(rests, i, right);
} // end of quickSort function
// printData function implementation
void printData(Restaurant rests[], int count)
{
ofstream outfile;
outfile.open("pop.txt");
outfile << setprecision(15);
for(int i = 0; i < count; i++)
{
outfile << rests[i].name << "|" << rests[i].area << "|" << rests[i].popCount << "|" << rests[i].popDensity << endl;
}
} // end of printData function
Errors: Fix needed
main.cpp: In function ‘int readData(Restaurant*)’:
main.cpp:71:41: error: cannot convert ‘std::basic_string<char>’ to ‘double’ in assignment
main.cpp:75:45: error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment
main.cpp:78:29: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘double’ in assignment
Explanation / Answer
For fixing the program: Just convert the double of area and popdensity to string variable with this convert the int of popcount to string variable so that the program gets execute and display the ouput. Try this for linux machine as well.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.