Good evening all. I have a quick quick problem in my code. The build is successful but my debug shows up the errors:

First-chance exception at 0x013c43e6 in week2_project1_gamephysics.exe: 0xC0000005: Access violation writing location 0xcccccccc.

Unhandled exception at 0x013c43e6 in week2_project1_gamephysics.exe: 0xC0000005: Access violation writing location 0xcccccccc.

The program '[9888] week2_project1_gamephysics.exe: Native' has exited with code -1073741819 (0xc0000005).

Can anyone please help me solve this? I need it for my mathematical programming and physics teacher. She only teaches us the math, but does not know how to code it. Please and Thank You again.

#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
	float sphere1[3], sphere2[3];

	string first_sphere[3], second_sphere[3];
	stringstream stream, stream2;

	cout << "What is the x,y,z for the 1st sphere? ";
	getline(cin, first_sphere[3]);

	stream << first_sphere[0];
	stream >> sphere1[0];

	stream << first_sphere[1];
	stream >> sphere1[1];

	stream << first_sphere[2];
	stream >> sphere1[2];

	cout << "What is the x, y, z for the 2nd sphere? ";
	getline(cin, second_sphere[3]);

	stream2 << second_sphere[0];
	stream2 >> sphere2[0];

	stream2 << second_sphere[1];
	stream2 >> sphere2[1];

	stream2 << second_sphere[2];
	stream2 >> sphere2[2];

		float RadiusofTwoSpheres(float *sphere1, float *sphere2);
	{
		cout << "The Radius for Sphere's 1 and 2 is... " << endl;
		cout << endl;
		cout << "R = " << (float)sqrt(pow(sphere2[0] - sphere1[0], 2) + pow(sphere2[1] - sphere1[1], 2) + pow(sphere2[2] - sphere1[2], 2)) << endl;
	};
	system("pause");
	return 0;
}

Recommended Answers

All 6 Replies

Hmm... I'm not sure about how you use the getline() function. Does the getline() also attempts to add "\0" to the end of your array when you have already filled up each array with 3 values?

Line 14...

string first_sphere[3];

Array size is 3, so valid indexes are 0, 1, 2.

Line 18...

getline(cin, first_sphere[3]);

Index is 3. Again from above...


Array size is 3, so valid indexes are 0, 1, 2.


The array index must be less than the array size.

can you show me so it can run successfully?

You cannot define a function ( RadiusofTwoSpheres ) in the middle of another function ( main() )

>>getline(cin, first_sphere[3]);
You want: cin >> first_sphere[0] >> first_sphere[1] >> first_sphere[2] Also as suggested, move this code

float RadiusofTwoSpheres(float *sphere1, float *sphere2);
	{
		cout << "The Radius for Sphere's 1 and 2 is... " << endl;
		cout << endl;
		cout << "R = " << (float)sqrt(pow(sphere2[0] - sphere1[0], 2) + pow(sphere2[1] - sphere1[1], 2) + pow(sphere2[2] - sphere1[2], 2)) << endl;
	};

before int main(){}

commented: Thank You so much for helping me. =) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.