Greetings everyone,
So I posted earlier a similar problem which I wrote in python, but now after accomplishing that, I think ill try to write it up in C++. The link to the original question is here. If there are any other questions or if you need any more information on the problem feel free to ask if it is not posted on that link.
So since there is a wall of text on that first post regarding the guidelines I used, ill try to keep it short here.

I will have the program throw 3 darts at the board with the rings 3" apart up to 15", the radius of the smallest circle (the bullseye) being 3". After the 3 darts are thrown the score is added up to figure out the first players score. Score is determined by the values of the x,y coordinates of the circle. Then player 2 will throw 3 darts and same will happen. In Python I had to read in from a file called games.txt but in C++ I can just use cin >> to read from the terminal. I am going to have a score function take two float arguments and return an int value. Of course I will use the #include<cmath> at the top so I can use the sqrt function. I also read about an algorithm called Newtown's function which is used to calculate the square root. Does my process sound good, or am I on the right track? Any comments and help would be greatly appreciated.

So I am trying to use if and else if statements in my program but I keep getting an error reading "illegal else without matching if". I have the matching ifs there but it gives the error regardless.

if (r<=3){
		return 100;
	}else if 
		(r<=6);
		return 80;
	else if 
		(r<=9);
		return 60;
	else if 
		(r<=12);
		return 40;
	else if 
		(r<=15);
		return 20;
	else
		return 0;

Any thoughts?

That first return statement is shown indented a lot, but I have it fine in my code. It pasted strangely, I will do it again

if (r<=3){
		return 100;
	}else if 
		(r<=6);
		return 80;
	else if 
		(r<=9);
		return 60;
	else if 
		(r<=12);
		return 40;
	else if 
		(r<=15);
		return 20;
	else
		return 0;

Nevermind solved that part I believe. I need to figure out this Newton's function deal. Its supposed to be used to calculate distances and find the square root of x. Im gonna have a function called mysqrt taking a float for an argument and returning a float. Any thoughts or tips?

That first return statement is shown indented a lot, but I have it fine in my code. It pasted strangely,

Convert TABs to 4 SPACEs. TABs don't work well on forums.

So, you if else satements:

if (r<=3){
		return 100;
	}else if 
		(r<=6);
		return 80;
	else if 
		(r<=9);
		return 60;
	else if 
		(r<=12);
		return 40;
	else if 
		(r<=15);
		return 20;
	else
		return 0;

if you have more than one statement in any if/ if else statement, you must enclose them in braces. Like this:

if (x > sum_number)
{
    y = x + 5;
    return y;
}

Edited 6 Years Ago by hag++: n/a

I got all the if elses out of the way. For my newton's method I am writing this:

mysqrt(float )//mysqrt takes a float argument
		float x;
		float r;
		cout << "Enter number : ";
		cin >> x;
		r = x / 2.0;
// I have code missing here, and am not sure what i should do next
//was thinking a loop but not really sure..


		cout << "Number was : " << x << ", root is " << r << endl;

Does that make sense to anyone or am I just making it harder on myself?

I figured out the Newton's Method and it looks pretty good.
I am trying to figure out some errors im having. I posted a thread for the errors, hopefully it will get resolved, ill keep working on it. Here is the link. Any help would be greatly appreciated. Any comments or questions feel free to ask. Thanks.

So I think I got rid of the errors but now I need some help with the program. I am trying to have this read in x and y values, and then put them through the math to get r. Then I want r to run through the score function, and get that point value. Once it does it will tag the first set of coordinates to player1 and the 2nd to player2. Then determine the score of each player and decide the winner. Anyways I am stuck, and have been blanking on this for some time. If anyone could help, it would be appreciated.

#include <iostream>
using namespace std;
#include <cmath>
#include <cstdlib>




int score (float x, float y) { //Score function with 2 float arguments
	//The following code finds the values of r and assigns them with points based on what the input is.
	int r =sqrt(x*x + y*y);

	if (r<=3){
		return 100;
	}
	if (r<=6){
		return 80;
	}
	if (r<=9){
		return 60;
	}
	if (r<=12){
		return 40;
	}
	if (r<=15){
		return 20;
	}else{
		return 0;
	
	
	
	}}

int main () {
//Here I want it to read in the values do the math, then run it //through score and assign r to the point value, and then use that //for player1 and player2 below.
	float x;
	float y;
	//int r1 = score(5,10);
	cin >> x >> y;
	int r =sqrt(x*x+y*y);
	cout << r << "\n";
	


//Here I need something to have each player have their own score after the coordinates are read in and the score is calculated
	int player1 = ;
	int player2 = ;
	cout << "Player 1's score is " << player1 << "\n";
	cout << "Player 2's score is " << player2 << "\n";  //what goes here for the score of player 1 and 2.
	cout << "Final score is " << player1 << " to " << player2 << ".\n";
		if (player1 > player2){
			cout << "Player 1 wins!\n";
		}if (player2 > player1){
			cout << "Player 2 wins!\n";
		}if (player1 == player2){
			cout << "Tie Game\n";
		
		
		}}

*/
}

This is what I have so far...I know that some of it is incomplete and would bring up errors but those are the parts I am blanking on. Thanks again.

I just responded on your other thread. lol When you have a chance you should flag down a mod (you can hit your own flag bad post) and have them close one (or merge them, but I think closing's easier) so it doesn't get confusing.

Ha sorry, I figured asking for help in coding on a thread labeled with help on a syntax error would be pointless. So I posted it here as well, since it seems more relevant.

So I figured a piece of the problem I was having earlier out. Here is my code, can someone explain what I need to do so I dont have to keep typing out score(x1,y1) + score(x2,y2) + etc....
I know I need a while loop of some kind but how would that work? Or better yet, how would I get the while loop to continue reading in x and y coordinates up until the 6th set then stop and make that player1's score?

int main () {
	float x;
	float y;
	float x1;
	float y1;
	//int r1 = score(5,10);
	cin >> x >> y;
	cin >> x1 >> y1;
	int r =sqrt(x*x+y*y);
	//cout << r << "\n";
	
	


	int player1 = score(x,y) + score (x1,y1);
	//while 
	cout << "Player 1's score is " << player1 << "\n";

	cout << "Enter another set of coordinates: " << "\n";
	cin >> x >> y;
	cin >> x1 >> y1;
	int player2 = score(x,y) + score(x1,y1);
	cout << "Player 2's score is " << player2 << "\n";
	//what goes here for the score of player 1 and 2.
	cout << "Final score is " << player1 << " to " << player2 << ".\n";

		if (player1 > player2){
			cout << "Player 1 wins!\n";
		}if (player2 > player1){
			cout << "Player 2 wins!\n";
		}if (player1 == player2){
			cout << "Tie Game\n";

This, or use a for loop:

int i = 0;
int score1,score2;
while (i <6)
{
       cout <<"Player1's turn:"<<endl;
       cout <<"Enter x and y"
       cin >> x >> y;
      score1 +=score(x,y);

      //same thing for player 2
       i++;  
}

You could do something fancy where the loop only had 1 cin statement and updated the score in a 0 or 1 position of a 1 x 2 array depending on which player was up, but that might be too much of a headache (using % operator).

Edited 6 Years Ago by jonsca: n/a

When I run this program, It turns up that, It opens a command prompt, but then when I finish typing in the values for x and y, it brings up an error message reading "Run-Time Check Failure #3 - the variable score1 is being used without being intizialized."
Not sure what this means. Anyways here is my code. Maybe this will help.

int i = 0;
	int score1;
	int score2;
	while (i <6)
	{
		cout << "Player 1's turn:"<<endl;
		cout <<"Enter x and y: ";
		cin >> x >> y;
		score1 +=score(x,y);
		cout << "Player2's turn: "<<endl;
		cout << "Enter x and y; ";
		cin >> x >> y;
		score1 +=score(x,y);
		i++;
	}

int score1 = 0,score2 = 0; (put that in place of int score1; int score2; ) Now they have been initialized (pardon the omission on my part).

Edited 6 Years Ago by jonsca: Mr smiley creeping in smug son of a gun :)

Excellent, Thank you so much. That worked great. Now I just need to take out some commented stuff and bingo bango.
Thanks again.

So I got it to run, but it is calculating the math incorrectly. For example, when I type (-9,0) it returns 100, when it should be 60, and so on and so forth. This is my code.

#include <iostream>
using namespace std;
#include <cmath>
#include <cstdlib>




int score (float x, float y) { //Score function with 2 float arguments
	//The following code finds the values of r and assigns them with points based on what the input is.
	int r=0;

	if (r<=3){
		return 100;
	}
	if (r<=6){
		return 80;
	}
	if (r<=9){
		return 60;
	}
	if (r<=12){
		return 40;
	}
	if (r<=15){
		return 20;
	}else{
		return 0;
	
	
	
	}}

int main () {
	float x;
	float y;
	int i = 0;
	int score1 = 0;
	int score2 = 0;
	while (i <1)
	{
		cout << "Player 1's turn:"<<endl;
		cout <<"Enter x and y: ";
		cin >> x >> y;
		score1 +=score(x,y);
		cout << "Player2's turn: "<<endl;
		cout << "Enter x and y; ";
		cin >> x >> y;
		score2 +=score(x,y);
		i++;
	}


	int player1 = score1 ;
	cout << "Player 1's score is " << player1 << "\n";


	int player2 = score2;
	cout << "Player 2's score is " << player2 << "\n";
	//what goes here for the score of player 1 and 2.
	cout << "Final score is " << player1 << " to " << player2 << ".\n";

		if (player1 > player2){
			cout << "Player 1 wins!\n";
		}if (player2 > player1){
			cout << "Player 2 wins!\n";
		}if (player1 == player2){
			cout << "Tie Game\n";
		
		
		}}

Where did your calculation for r go? It looks like you set r = 0 in that method which will then hit the first if statement and return 100. I would change that chain into an else if construct rather than a bunch of ifs one after another.

You mean the "r=sqrt(x*x+y*y)" statement?
I entered that but my output remains a bit unaccurate sometimes. When I plug in some values I still get interesting points values.

Also I want to implement something to read in as such...if any input is -100 then the program quits immediately.

I thought the r statement was the meat and potatoes of your program though. Do you see why everything is less than or equal to 3 if r = 0? What kinds of weird values were you getting?

The quickest way (not necessarily the best way) would be to test x and y after the cin statement for being less than or equal to -100, if so then return 0; on the spot. There are probably cleaner ways to do it by breaking out of the while and testing for a flag, etc. etc.

I switched my r=0;
to r=sqrt(x*x+y*y);
seems to work a bit better, but still some really high values.
I tested some sample input that I used in my python program,
and the scores I get in this one are higher than those in the python version. I read in the same values too.

Nevermind the math works out fine, I apologize. Thanks again jonsca for everything. I couldnt have done it without you. I still have some more work, so I will post if I have more questions. But essentially... se fini.

Great. Keep on with it it'll turn out great. All you need are some air resistance calculations on the dart in mid-flight.

"All you need are some air resistance calculations on the dart in mid-flight."
- Haha good one.

You first code was ok, but you just needed to remove the semicolon after the if statement.
it should then become:

if (bool _condition)
something();

do not do:
if (bool _unsedCondition);
something();

In the later case, the if statement is useless.
something() will be called anyway.

Also as a good practice and coding convention, keep the

Also as a good practice and coding convention, keep the

That's the equivalent of "The...secret...of...the...universe...is" (*slumps over). Perhaps we'll never know.

That's the equivalent of "The...secret...of...the...universe...is" (*slumps over). Perhaps we'll never know.

I never say it's THE coding convention, i invite him to have "a" coding convention, because it's good practice. Obviously, that error could have been easily spotted with a simple and clear layout.
And basically, you'll find that many problems have a similar solution.

But this is obvious chat, thus not very interesting.

I just was joking that your sentence trailed off without being completed (unless it's a problem with my browser or something but that's where your post ends on my system). I wasn't poking fun at what you said.

I just was joking that your sentence trailed off without being completed (unless it's a problem with my browser or something but that's where your post ends on my system). I wasn't poking fun at what you said.

No harm done, my friend.

This article has been dead for over six months. Start a new discussion instead.