# Thread: Use a loop to restart a program

1. ## 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. Originally Posted by ben1355 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. 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. 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. 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. you just started a while loop,

also you removed 1/2 of your code!

like matticus said,

DO

code

while(user said yes)

end

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! 7. 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;
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 : ");

}
return 0;
}``` 8. Anybody can help to fix the while expression at the end? 9. 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. 10. Thank you all for your help! 11. Another question, I need to show decimal points on Area, but it doesn't. Any idea? 12. 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. Popular pages Recent additions 