Thread: Loop Issues

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    6

    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 answer;
    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);
        sscanf(line,"%i",&answer);
        
        if (answer==y) {
               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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since I do not see a do anywhere in your code, this must be an infinite loop with an empty body:
    Code:
    while(1);
    Was this intentional?

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    So since that issue is solved (thanks btw, that was a really easy fix), does anyone have any suggestions for my loop?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. Describe, step by step, what your code is supposed to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    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. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    Thanks. I got it to work with a lot of tinkering and some conversion from int to char and string. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop issues
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-14-2010, 05:29 PM
  2. Loop issues.
    By student0806 in forum C# Programming
    Replies: 1
    Last Post: 10-13-2010, 08:02 PM
  3. For Loop Issues
    By jtkhoskinson in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2010, 01:28 PM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. Newb needs help with loop issues
    By FatalError in forum C Programming
    Replies: 15
    Last Post: 09-04-2004, 02:19 PM