-1

So, I have a Program which checks if a Triangle is valid, here is the code.

#include <stdio.h>  

int main()  
{  
    int side1, side2, side3; 

    /* Initially assume that the triangle is not valid */
    int valid = 0;

    /* Input all three sides of a triangle */  
    printf("Enter three sides of triangle: \n");  
    scanf("%d%d%d", &side1, &side2, &side3);  

    if((side1 + side2) > side3)  
    {  
        if((side2 + side3) > side1)  
        {  
            if((side1 + side3) > side2)
            {  
                /*
                 * If side1 + side2 > side3 and
                 *    side2 + side3 > side1 and
                 *    side1 + side3 > side2 then
                 * the triangle is valid. Hence set
                 * valid variable to 1.
                 */
                valid = 1;
            }  
        }
    }  

    /* Check valid flag variable */
    if(valid == 1)
    {
        printf("Triangle is valid.");
    }
    else
    {
        printf("Triangle is not valid.");
    }

    return 0;
}

But what I want to do is add a Prompt to ask how many triangles to check, so I would add something like this

#include <stdio.h>  

int main()  
{  
    int repeats; 

    /* Input the amount of repeats  */  
    printf(" the amount of repeats: \n");  
    scanf("%", &repeats);
...

The amount of entered should be the amount of triangles the user wants to check.

Bonus Question: How to check that the user entered only a number and no letter. Thanks in advance.

K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71
  • 2
    check the return value of [`scanf`](https://linux.die.net/man/3/scanf) .. you'll also want to double check your format specifier. – yano Oct 22 '17 at 15:01

2 Answers2

1

The

scanf("%", &repeats);

must've been

scanf("%d", &repeats);

as repeats is an int.

You could just place the block of code that you want to execute repeatedly in a loop like

for(i=0; i<repeats; ++i)
{
    //your code
}

To check whether the user entered a number and not a character check the return value of scanf(). It returns the number of successful assignments it did.

Since the format specifier is %d in

scanf("%d", &repeats);

scanf() expects an integer. But if a character was given out instead, it won't assign (and would return 0 in this case) and the input (which is not a number) will remain unconsumed in the input buffer.

if( scanf("%d", &repeats)!=1 )
{
    //value was not read into 'repeats'
}

This might be of interest to you.

Now if the invalid input remains in the input buffer, it can cause problems with the next scanf(). So it must somehow be consumed.

You could do something like

int ch;
while( (ch=getchar())!='\n' && ch!=EOF );

This would consume from the input buffer till the next new line (\n).

Have a look here.

And your

if((side1 + side2) > side3)  
{  
    if((side2 + side3) > side1)  
    {  
        if((side1 + side3) > side2)
        {  
            valid = 1;
        }  
    }
} 

could be made

if( (side1 + side2)>side3 && (side2 + side3)>side1 &&  (side1+side3)>side2 )
{  
    valid = 1;
} 

and you don't really need the extra parenthesis because arithmetic operators have greater precedence than relational operators which in turn have greater precedence than logical operators.

Look here.

J...S
  • 5,079
  • 1
  • 20
  • 35
0

you could achieve that simply by wrapping a do while around your code so that it looks something like that

#include <stdio.h>  

int main()  
{  
    int repeats = 0, counter = 0; 

    /* Input the amount of repeats  */  
    printf(" the amount of repeats: \n");  
    scanf("%d", &repeats);

    do {
     ...
     check here if its a triangle
     ...
     counter++;

    } while(counter < repeats);
inxoy
  • 3,484
  • 2
  • 13
  • 21