Homework Help

My assignment is to read a file, skipping the first two lines, and given three commands START, DATA, and STOP, I must calculate total distance traveled and the distance between the start and stopping points. START and STOP both indicate that the next two values are the beginning x and y coordinates or the ending x and y coordinates. The DATA command just indicates a new x and y coordinate.

My text file I'm reading looks like this:

GPS Data File
Command x-coordinate y-coordinate
START 0 0
DATA 24 24
DATA 4 24
DATA 4 124
DATA -25 12
DATA 0 0
STOP 195 215

For some reason, if I don't have the first two lines there and I just have data immediately on the first line, my code works. Why is my program not skipping these first two lines?


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
#include <iostream>
#include <fstream>    //For ofstream and ifstream variables
#include <cmath>      //For sqrt(), pow()
#include <iomanip>    //For fixed, showpoint, setprecision
#include <string>     //For string objects

using namespace std;

int main()
{

  //Data Abstraction:

  double x_start        = 0.0,
         x_end          = 0.0,
         x_data_1       = 0.0,
         x_data_2       = 0.0,
         y_start        = 0.0,
         y_end          = 0.0,
         y_data_1       = 0.0,
         y_data_2       = 0.0,
         distance       = 0.0,
         total_distance = 0.0;

  int    attempts       = 0;

  string file_name;
  string command;
  ifstream in;
  ofstream out;


  //Input:

  do
  {

    cout << "Please Enter The Name Of The Data File: ";
    cin  >> file_name;
    in.open(file_name.c_str());

      if(!in.is_open() && attempts < 3)
        cout << "ERROR: File did NOT open - try again." << endl;

    attempts++;
  }
  while (!in.is_open() && attempts < 3);

  if(!in.is_open())
    cout << "\nERROR: File did NOT open in 3 attempts." << endl;

  //Open File and calculate distances

  else
  {
    in.ignore( 13, '\n' );
    in.ignore( 80, '\n' );

  //Reading File

      while (in >> command)
      {
          if (command == "START")
          {
            in >> x_start >> y_start;
            in >> command;

             if (command == "DATA")
             {
               in >> x_data_1 >> y_data_1;
               total_distance += sqrt(pow((x_data_1 - x_start), 2.0)
                               + pow((y_data_1 - y_start), 2.0));
             }

             else
             {
               in >> x_end >> y_end;
               total_distance += sqrt(pow((x_start - x_end), 2.0)
                              + pow((y_start - y_end), 2.0));
             }

          }

          if (command == "DATA")
          {
            in >> x_data_1 >> y_data_1;
            in >> command;

              if(command == "DATA")
              {
                in >> x_data_2 >> y_data_2;

                total_distance += sqrt(pow((x_data_2 - x_data_1), 2.0)
                                + pow((y_data_2 - y_data_1), 2.0));
              }

              else
              {
                in >> x_end >> y_end;

                total_distance += sqrt(pow((x_data_1 - x_end), 2.0)
                               + pow((y_data_1 - y_end), 2.0));
              }

          }

          if(command == "STOP")
          {
            in >> x_end >> y_end;

            total_distance += sqrt(pow((x_data_1 - x_end), 2.0)
                           + pow((y_data_1 - y_end), 2.0));
          }

      }

    distance += sqrt(pow((x_start - x_end), 2.0)
             + pow((y_start - y_end), 2.0));

  //Displaying Results to stdout and output file

    out.open("GPS.report.txt");
    cout << fixed << showpoint << setprecision(1);
    out  << fixed << showpoint << setprecision(1);

    cout << "\nFinal Location: " << "(" << x_end << ", "
         << y_end << ")" << endl;
    cout << "Total distance traveled: " << total_distance << endl;
    cout << "Distance to starting point " << distance << endl;

    out << "Final Location: " << "(" << x_end << ", "
        << y_end << ")" << endl;
    out << "Total distance traveled: " << total_distance << endl;
    out << "Distance to starting point " << distance;

    out.close();
    in.close();

  }

  return 0;

}

Last edited on
Well, it reads as if after each command there will be only a single pair of coordinates, but you always read four values. Why so?
@coder777 oh shoot, thanks. What I was trying to do was get the next set of coordinates so I could use the distance formula to update total_distance. I think what's happening is that when it reads the next set of coordinates, it's actually reading the command on the next line.

How could I get it to where it reads the next set of data, then the next command, then the next coordinates and calculate?
I updated my code but I'm still getting that funky read :/
You think to complicated. How about 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
      while (in >> command)
      {
          if (command == "START")
          {
            in >> x_start >> y_start;
            in >> x0 >> y0;
          }

          else if (command == "DATA")
          {
            in >> x1 >> y1;

                total_distance += sqrt(pow((x1 - x0), 2.0)
                               + pow((y1 - y0), 2.0));
            x0 = x1;
            y0 = y1;
          }

          else if(command == "STOP")
          {
            in >> x_end >> y_end;

            total_distance += sqrt(pow((x0 - x_end), 2.0)
                           + pow((y0 - y_end), 2.0));
          }

      }
Topic archived. No new replies allowed.