Solving C++ Errors and Calculating Point Distances

  • Comp Sci
  • Thread starter jimmy007
  • Start date
In summary, the user inputs a filename for a point data file, and the program opens the file and reads in the points. It then calculates the average distance of all the points from the origin and writes the results to a file.
  • #1
jimmy007
8
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.
cout << "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())
{
cout << "Invalid filename.";
cout << "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
  • #2
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
 
  • #3
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.
 
  • #4
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
 
  • #5
so how do i fix it. should i use static_cast<>
 
  • #6
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
 
  • #7
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.
 
  • #8
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
 
  • #9
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.
 

1. What is C++ and why do I need help with it?

C++ is a high-level programming language commonly used for developing software, games, and applications. It is a complex language that requires a good understanding of programming concepts and syntax. Many people seek help with C++ because it can be challenging to learn and master.

2. Are there any resources available to help me learn C++?

Yes, there are plenty of resources available online and in books to help you learn C++. Some popular resources include online tutorials, coding forums, and video courses. It is also helpful to practice coding and actively seek out help from experienced programmers.

3. Can I use C++ for any type of programming?

Yes, C++ is a versatile language that can be used for a variety of programming tasks. It is commonly used for system and application programming, game development, and scientific computing. Its speed and efficiency make it a popular choice for many different types of projects.

4. Is C++ difficult to learn?

Learning any programming language can be challenging, but with patience and practice, C++ can be mastered. It may take some time to understand the syntax and concepts, but once you have a solid foundation, you can use C++ to create powerful and efficient programs.

5. How can I get help with C++ if I get stuck?

If you get stuck while learning or coding in C++, there are several options for getting help. You can seek assistance from online communities and forums, ask for help from more experienced programmers, or consult online documentation and tutorials. It is important to not get discouraged and to keep practicing and seeking help when needed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
  • Programming and Computer Science
Replies
31
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
Back
Top