Thread: Use a loop to restart a program

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    9

    Use a loop to restart a program

    I've just started to learn C programming. In following program I need to use a loop, so at the end, it asks the user that if wants to try other numbers or not. If yes program restarts from beginning.Thank you for any help.

    Code:
    /*
    A program who checks three positive integers to be 3 sides of a triangle and calculates the area and shows the triangle type.
    */
    
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main(void)
    {
        int a, b, c, s, area;
        printf("Input 3 positive integers: \n");
        scanf("%d %d %d", &a, &b, &c);
        
        if ((a + b <= c) || (a + c <= b) || (c + b <= a))
            printf("These are not a valid triangle's sides\n %d %d %d\n", a, b, c);
        else
        {
            {
                if ((a == b) && (a == c) && (b == c))
                printf("This is an equilateral triangle\n");
                else if ((a == b) || (a == c) || (b == c))
                    printf("This is an isosceles triangle\n");
                else if ((a != b) || (a != c) || (b != c))
                    printf("This is a scalene triangle\n");
            }
            s = (a + b + c) / 2.0;
            area = sqrt(s*(s - a)*(s - b)*(s - c));
            printf("The area of triangle is: %d \n", s);
            return 0;
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by ben1355 View Post
    I've just started to learn C programming. In following program I need to use a loop, so at the end, it asks the user that if wants to try other numbers or not. If yes program restarts from beginning.Thank you for any help.

    Code:
    /*
    A program who checks three positive integers to be 3 sides of a triangle and calculates the area and shows the triangle type.
    */
    
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main(void)
    {
        int a, b, c, s, area;
        printf("Input 3 positive integers: \n");
        scanf("%d %d %d", &a, &b, &c);
        
        if ((a + b <= c) || (a + c <= b) || (c + b <= a))
            printf("These are not a valid triangle's sides\n %d %d %d\n", a, b, c);
        else
        {
            {
                if ((a == b) && (a == c) && (b == c))
                printf("This is an equilateral triangle\n");
                else if ((a == b) || (a == c) || (b == c))
                    printf("This is an isosceles triangle\n");
                else if ((a != b) || (a != c) || (b != c))
                    printf("This is a scalene triangle\n");
            }
            s = (a + b + c) / 2.0;
            area = sqrt(s*(s - a)*(s - b)*(s - c));
            printf("The area of triangle is: %d \n", s);
            return 0;
        }
    }
    Welcome to the forum. You didn't actually ask a question though.

    And there are no loops in the program you provided.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    Sorry my question wasn't clear. I need to know where should I put my loop in program. I prefer to use a do-while loop but can you tell me how to do it?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Loops are intuitive in that, you encompass everything you want repeat into a loop.

    In your case, if you want to "ask the user that if wants to try other numbers or not", it pretty much means most (but not all!) of your code should be in a loop.

    I prefer to use a do-while loop but can you tell me how to do it?
    Well, you already have an idea of which loop you want to use, which suggests you understand the basics behind it.

    "do" ... followed by the code you want to repeat ... followed by a prompt asking if the user wants to keep going ... followed by a "while" that checks the input and determines whether to keep looping or not.

    You should have a basic example of a "do-while" loop in your course notes. Or at least your textbook. And even if you're learning from tutorials, you can get the gist of it online (i.e. see the "do-while" description here: For, While and Do While Loops in C - Cprogramming.com).

    It's just a matter of understanding how the loop construct works and plugging in your code.

    So read up on "do-while" loops and give it a shot. Try a few simpler practice problems to play with the ideas. Then try coding it and post your attempt. If you're stuck, ask questions about where you're stuck and we can give you more specific advice.

    But don't give up - if you give it a fair shot and let us know if and where you have problems, we'll help you through them.

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    I tried to write some codes. First: How to restart with prompt? I cannot get floating points on area?
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        int a = 3, b = 3, c = 3, s, area;
        
        while ((a > 0) && (b > 0) && (c > 0))
        {
            printf("Input 3 positive integers: \n");
            scanf("%d %d %d", &a, &b, &c);
    
            if ((a + b <= c) || (a + c <= b) || (c + b <= a)){
                printf("These are not a valid triangle's sides\n %d %d %d\n", a, b, c);
    
            }
            else if ((a == b) && (a == c) && (b == c)){
                printf("This is an equilateral triangle\n");
    
            }
            else if ((a == b) || (a == c) || (b == c)){
                printf("This is an isosceles triangle\n");
    
            }
            else if ((a != b) || (a != c) || (b != c)){
                printf("This is a scalene triangle\n");
    
            }
            s = (a + b + c) / 2.0;
            area = sqrt(s*(s - a)*(s - b)*(s - c));
            printf("The area of triangle is: %d \n", s);
        }
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    you just started a while loop,

    also you removed 1/2 of your code!

    like matticus said,

    DO

    code

    ask if want to repeat

    while(user said yes)

    end



    /----added----/

    also note that a given triangle can be more than one type at the same time. For example, a scalene triangle (no sides the same length) can have one interior angle 90, making it also a right triangle. This would be called a "right scalene triangle", where as your coding would not reflect this!
    Last edited by Crossfire; 02-22-2015 at 05:41 AM.

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    I cannot figure out the ending while. It goes to an infinite loop!?
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        int a, b, c, s, area;
        char answer;
        do {
            printf("Input 3 positive integers: \n");
            scanf("%d %d %d", &a, &b, &c);
    
            if ((a + b <= c) || (a + c <= b) || (c + b <= a)){
                printf("These are not a valid triangle's sides\n %d %d %d\n", a, b, c);
            }
            else
            {
                if ((a == b) && (a == c) && (b == c)){
                    printf("This is an equilateral triangle\n");
                }
                else if ((a == b) || (a == c) || (b == c)){
                    printf("This is an isosceles triangle\n");
                }
                else if ((a != b) || (a != c) || (b != c)){
                    printf("This is a scalene triangle\n");
                }
    
                s = (a + b + c) / 2.0;
                area = sqrt(s*(s - a)*(s - b)*(s - c));
                printf("The area of triangle is: %d \n", s);
            }
            printf("If you need to try another integers type y otherwise n : ");
            scanf("%d", &answer);
    
        }
        while (answer != 'y');
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    Anybody can help to fix the while expression at the end?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,906
    Try changing this line from

    Code:
    scanf("%d", &answer);
    to
    Code:
    scanf(" %c", &answer);
    The space is needed before the "c" to skip possible newlines.

    FYI: the letter Y is not a number.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    Thank you all for your help!

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    9
    Another question, I need to show decimal points on Area, but it doesn't. Any idea?

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That is because you are telling printf to print an integer. Take a look at printf() and pay attention to format specifiers!

    In fact:
    Code:
         area = sqrt(s*(s - a)*(s - b)*(s - c));            
         printf("The area of triangle is: %d \n", s);
    Doesn't even look like you are printing the area.
    Last edited by AndrewHunter; 02-23-2015 at 06:51 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Restart the c++ program
    By Alban in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2013, 03:26 AM
  2. Program will not restart properly
    By nelly26 in forum C Programming
    Replies: 12
    Last Post: 04-22-2012, 06:25 PM
  3. How to restart a program using loop?
    By NewbieCProgramm in forum C Programming
    Replies: 4
    Last Post: 12-05-2011, 10:41 AM
  4. restart the program
    By kellymart87 in forum C++ Programming
    Replies: 15
    Last Post: 04-17-2007, 10:04 PM
  5. restart program
    By kellymart87 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 11:18 PM