Please help me find where I need to add count++ in order to make my calculation (avgDistanceToStart) correct. C++ #include #include #include #include #include #include using namespace std; int main( ) { ifstream input; ofstream output; string filename; string line1; string line2; string COMMAND; double startx = 0.0,starty = 0.0,stopx = 0.0,stopy = 0.0; double x1, y1, backx = 0.0, backy = 0.0; double distance = 0.0; double total = 0.0; double avgDistanceToStart = 0.0; bool START = false; bool STOP = false; int count = 0; do{ cout << "Please Enter The Name Of The Data File: "; cin >> filename; cout << filename; cout << endl; input.open(filename.c_str()); if (!input.is_open()){ cout << "Error: File failed to open." << endl; } } while(input.fail()); getline(input, line1); getline(input, line2); while(!START && input >> COMMAND >> x1 >> y1) { if(COMMAND == "START") { START = true; startx = x1; starty = y1; backx = x1; backy = y1; } } while(!STOP && input >> COMMAND >> x1 >> y1) { total += sqrt((pow(backx - x1, 2)) + pow(backy - y1, 2)); backx = x1; backy = y1; if(COMMAND == "STOP") { STOP = true; stopx = x1; stopy = y1; } } distance += sqrt(pow(startx - stopx, 2) + pow(starty-stopy, 2)); //need to add current x and current y to diffrentiate the two distances avgDistanceToStart = distance/count; output.open("GPS.report"); cout << setprecision(1) << fixed; output << setprecision(1) << fixed; cout << "Final Location: (" << stopx << ", " << stopy << ")" << endl; output << "Final Location: (" << stopx << ", " << stopy << ")" << endl; cout << "Total distance traveled " << total << endl; output << "Total distance traveled " << total << endl; cout << "Distance to starting point " << distance << endl; output << "Distance to starting point " << distance << endl; cout << "Average distance to start point " << avgDistanceToStart << endl; output << "Average distance to start point " << avgDistanceToStart << endl; output.close(); input.close(); return 0; }
Please help me find where I need to add count++ in order to make my calculation (avgDistanceToStart) correct. C++
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;
int main( )
{
ifstream input;
ofstream output;
string filename;
string line1;
string line2;
string COMMAND;
double startx = 0.0,starty = 0.0,stopx = 0.0,stopy = 0.0;
double x1, y1, backx = 0.0, backy = 0.0;
double distance = 0.0;
double total = 0.0;
double avgDistanceToStart = 0.0;
bool START = false;
bool STOP = false;
int count = 0;
do{
cout << "Please Enter The Name Of The Data File: ";
cin >> filename;
cout << filename;
cout << endl;
input.open(filename.c_str());
if (!input.is_open()){
cout << "Error: File failed to open." << endl;
}
}
while(input.fail());
getline(input, line1);
getline(input, line2);
while(!START && input >> COMMAND >> x1 >> y1)
{
if(COMMAND == "START")
{
START = true;
startx = x1;
starty = y1;
backx = x1;
backy = y1;
}
}
while(!STOP && input >> COMMAND >> x1 >> y1)
{
total += sqrt((pow(backx - x1, 2)) + pow(backy - y1, 2));
backx = x1;
backy = y1;
if(COMMAND == "STOP")
{
STOP = true;
stopx = x1;
stopy = y1;
}
}
distance += sqrt(pow(startx - stopx, 2) + pow(starty-stopy, 2));
//need to add current x and current y to diffrentiate the two distances
avgDistanceToStart = distance/count;
output.open("GPS.report");
cout << setprecision(1) << fixed;
output << setprecision(1) << fixed;
cout << "Final Location: (" << stopx << ", " << stopy
<< ")" << endl;
output << "Final Location: (" << stopx << ", " << stopy
<< ")" << endl;
cout << "Total distance traveled " << total << endl;
output << "Total distance traveled " << total << endl;
cout << "Distance to starting point " << distance << endl;
output << "Distance to starting point " << distance << endl;
cout << "Average distance to start point " << avgDistanceToStart << endl;
output << "Average distance to start point " << avgDistanceToStart << endl;
output.close();
input.close();
return 0;
}
Step by step
Solved in 4 steps