Solving C++ Errors and Calculating Point Distances

  • Context: Comp Sci 
  • Thread starter Thread starter jimmy007
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting errors in a C++ program that calculates distances of points from the origin and outputs certain results based on those distances. Participants explore issues related to data types, function implementations, and program logic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports multiple compilation errors, including issues with adding a structure to an integer and problems with variable scope.
  • Another participant suggests that the first error indicates an attempt to add a Points structure to an integer, which is nonsensical.
  • There is a discussion about whether to use static_cast<> to resolve type issues, but the logic of adding a structure to an integer is questioned.
  • A participant clarifies that their task involves reading points from a file and calculating distances, emphasizing that this is a lab assignment rather than homework.
  • Participants discuss the intended functionality of the calculateAverage() function and its lack of reference to the distance() function.
  • Concerns are raised about the logic of the distance() function, particularly regarding how distances are stored and calculated.
  • Suggestions are made to either use a different array for distances or to compute distances directly while calculating the average.
  • There is confusion about the appropriate data type for storing distances, with a participant seeking clarification on whether to use a Points type or an integer.

Areas of Agreement / Disagreement

Participants express differing views on how to address the errors and the logic of the program. There is no consensus on the best approach to resolve the issues presented.

Contextual Notes

Participants note limitations in the program's logic, particularly regarding the handling of distances and the structure of the code. The discussion highlights unresolved questions about variable types and function interactions.

Who May Find This Useful

Individuals interested in C++ programming, particularly those dealing with data structures, file I/O, and distance calculations in programming assignments or labs.

jimmy007
Messages
8
Reaction score
0
please help me solving errors in this problem .

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
const size_t CAPACITY(8);

// struct needed for this program.
struct Points {
double x, y;
};

// function prototype to open the user file and make array.
void makeArray(Points[], size_t&);
// function prototype to calculate distance of points from origin.
void distance(Points[], const Points, size_t&);
double calculateAverage(Points[], double);
void outputFile(Points[], double, ofstream&);

int main()
{
Points array[CAPACITY];
Points dis;
size_t size(0);
ofstream outFile;
double average(0);
makeArray(array, size);
distance(array, dis, size);
average = calculateAverage(array, average);
outputFile(array, average, outFile);


}

// function to open the user choice of input file and fill in the array.
void makeArray(Points array[], size_t& size)
{
string fileName;
ifstream inFile;

// ask the user for input file.
count << "Enter a filename:";
cin >> fileName;

// open the user input file.
inFile.open(fileName.c_str());

// check if the file exists or not.
if(inFile.fail())
{
count << "Invalid filename.";
count << "Please enter a filename again:";
cin >> fileName;
inFile.clear();
inFile.open(fileName.c_str());
}
Points temp;

// read the points from the file.
while(inFile >> temp.x >> temp.y )
{
array[size] = temp;
size++;
}


}
// function to calculate the distance of the points from the origin.
void distance(Points array[], const Points dis, size_t& size)
{

// for loop to calculate the distance of each point from origin.
for(int i = 0; i < size; i++)
{
array = sqrt(static_cast<double>(pow( dis.x,2)+pow(dis.y,2) )) ;

}

}

// function to calculate the average of all the distance of points.
double calculateAverage(Points array[], double average)
{
int howMany(0);
int total(0);

// for loop to calculate the sum.
for(int i = 1; i < howMany; i++)
{
total += array;
howMany++;
}

// calculate the average of distances.
average = total/howMany;

return average;
}

// function to output if average is greater than the points.
void outputFile(Points array[], double average, ofstream& outFile)
{
if(average > array)
{
outFile.open("Output.txt");
outFile << array<<endl;
}
}
 
Physics news on Phys.org
Hi jimmy007,

We're not going to do your homework for you. We will not provide any assistance unless you show your work.

If you have some specific questions about this program, please describe what you're tried so far and why you are stuck. We'll try to point you in the right direction.

- Warren
 
what i am asking is that i cannot figure out what i did wrong. this is not actually a homework. please tell me what did i do wrong. the errors i get are c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(96): error C2677: binary '+=' : no global operator found which takes type 'Points' (or there is no acceptable conversion)
c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(81): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(112): error C3861: 'i': identifier not found, even with argument-dependent lookup

these are the erros i get please help me.
 
Well, let's look at the errors, one at a time. In the first error (which appears at line 91 to me), you're trying to add a Points structure to an integer. This, of course, doesn't make a lick of sense.

- Warren
 
so how do i fix it. should i use static_cast<>
 
It does not make logical sense to add a structure, composed of two doubles, to an integer. That's like trying to add five to an elephant.

I don't really know what your program is intending to do, so I cannot suggest what you should change.

Futhermore, I frankly don't believe that this is not homework. You do not need to lie to us to get help, and we don't really appreciate it.

- Warren
 
this is actually a lab that i have to do.

Like the Blue Book question from the last test:

Read a user chosen file of points (use the struct definition below for this) and store them in an array.
Keep prompting the user until the file can be opened.
Assume the file contains two ints per line which represent the x and y-axis values for a point.

Create an output file containing all the points that are further than the average distance of all points from the origin.

You must use our code for the distance function.
Here is it's prototype (declaration) :

double distance2origin( const Point &, const Point & );

Here's Point:

struct Point {
double x, y;
};

Since we do not give you the definition of our function, you can only compile your code.

Your lab worker must check that your code compiles and will read your code to see that you correctly broke the problem into subproblems and wrote functions for these subproblems. Your main must be "clean": create variables, arrays and so forth to hold the data and call functions using these data structures to solve the problem.



this is what my program is about.
 
Thanks for your honesty. Here on PF, we place a strong emphasis on academic intregrity.

Tell me what you intend for the calculateAverage() function to do. Is it supposed to calculate the average distance of all the points from the origin?

- Warren
 
yea you are right.
 
  • #10
Then I think it's curious that your calculateAverage() function doesn't reference your distance() function. Don't you?

Futhermore, I think it's curious that your distance() seems to be taking an array of points, but then is trying to stick the distance to those points into the same array. That seems odd to me, doesn't it to you?

- Warren
 
  • #11
yea then i should have different array right? so any suggestions.
 
  • #12
Sounds like a good plan, yes. Or, perhaps you don't need to store the distances at all, and can just calculate the distances as you're computing the average.

- Warren
 
  • #13
should the other array be type of points or int
 
  • #14
It's a distance, right? I'd say it's neither a point, nor an int. Turn on your brain for a minute.

- Warren
 
  • #15
i don't get it . i better contact my TA.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K