1. ## Loop Issues

Hey everyone,

So I'm new to programming, and my teacher isn't very helpful when it comes to his students running into issues. I have to make this loop (its asking for two separate points to measure the distance) using "if". We are not allowed to use for. I've been experimenting for hours and don't have much to show for it.

Code:
```#include <math.h>
#include <stdio.h>

char line [100];

double xc0; /*X coordinate of first point*/
double xc1; /*X  coordinate of second point*/

double yc0; /*Y coordinate of first point*/
double yc1; /*Y coordinate of second point*/

double xsqrd, ysqrd, distsqrt;

int y;
int n;

int main () {

printf("What is the x coordinate of the first point?"); /*First X Point*/
fgets(line, sizeof(line), stdin);
sscanf(line, "%lf", &xc0);

printf("What is the y coordinate of the first point?"); /*First Y Point*/
fgets(line, sizeof(line), stdin);
sscanf(line, "%lf", &yc0);

printf("What is the x coordinate of the second point?"); /*Second X Point*/
fgets(line, sizeof(line), stdin);
sscanf(line, "%lf", &xc1);

printf("What is the y coordinate of the second point?"); /*Second Y Point*/
fgets(line, sizeof(line), stdin);
sscanf(line, "%lf", &yc1);

xsqrd= pow((xc1-xc0),2); /*First part of formula*/
ysqrd= pow((yc1-yc0),2); /*Second part of forumla*/
distsqrt= xsqrd+ysqrd; /*X and Y values total, will later be under square root*/

printf("The distance between the two points is %lf.\n", sqrt(distsqrt));

printf("Would you like to compute another distance between two points? (y/n)");
fgets(line, sizeof(line), stdin);

printf("Nugget.\n"); /*Example*/
}
else  {
printf("Goodbye.\n");
}

while(1);
return(0);
}```
I can get it to say Nugget, but I can't get it to say goodbye. Also, in the end, I need to replace Nugget with the initial program so that it can be repeated over and over again. Can anyone help me out? Thanks.

2. Since I do not see a do anywhere in your code, this must be an infinite loop with an empty body:
Code:
`while(1);`

Originally Posted by monekychef
I have to make this loop (its asking for two separate points to measure the distance) using "if". We are not allowed to use for.
You are supposed to use a while loop instead?

This is likely to be wrong:
Code:
`if (answer==y)`
My guess is that you want the character literal 'y' instead.

3. The reason for while(1) is because if it isn't included then my command prompt window always closes. I haven't figured out a way around that.

For the second question, yes I am looking for the literal 'y' character, which means I should probably change that variable for int to char.

4. Originally Posted by monekychef
The reason for while(1) is because if it isn't included then my command prompt window always closes. I haven't figured out a way around that.
Ah. Refer to: pause console.

5. So since that issue is solved (thanks btw, that was a really easy fix), does anyone have any suggestions for my loop?

6. Your code doesn't have any loops buddy. Especially if you removed the while loop you were using to pause.

I don't want to be rude but make sure you know the nomenclature. Everyone makes mistakes but if you just don't know, you end up talking nonsense.

7. I know that my code works for the purpose of finding the distance between two points, I have tested it a bit and I know that for sure. The issue is that our professor has only touched on loops and "if" "else" "while" statements for about 5 minutes, but he did not tell us how to incorporate them. I've been trying to read online to figure out how to use these commands, and while a lot of things seem to make sense, I have not had any success in getting my program to loop or respond correctly to my "if" statements.

So what I'm basically trying to say is that yes, I am speaking nonsense about loops because I have no idea how to use them in my current code.

8. Okay. Describe, step by step, what your code is supposed to do.

9. Alright, here we go.

1. Asks for the coordinates of two points.
2. Calculates the distance between two points using a given equation.
3. Once calculations are done, program asks if user would like to use the program again to find the distance between two more coordinates. If user says yes, the program repeats. If the user says no, the program ends.

10. Ah. It looks like you are on track for the first two. For the third: read up on how to write a do while loop. A while loop can also work.

11. Thanks. I got it to work with a lot of tinkering and some conversion from int to char and string. Thanks for the help.