Link to home
Start Free TrialLog in
Avatar of Gehteuch Nixan
Gehteuch Nixan

asked on

How can I make a program not output anything when ctrl +d is

I have a C program and when I press ctrl +d right after the program starts, it ends in an infinite loop. Should I press ctrl +d in the course of the program it still gives me an output.

Is there a way to make the program exit without any output when CTRL +D is pressed

This is my code:

         ...
    
       {
            int repeats = 0, counter = 0;
            //Amount of triangles
            repeats = checkInput0();
            int i;
            
            
            // scanf("%d", &repeats);  //num is the user input
            for(i=0; i<repeats; i++)
            {
                //printf("enter first number");
                a_arr[i] = checkInput1();
                //printf("enter first number");
                b_arr[i] = checkInput2();
                //printf("enter first number");
                c_arr[i] = checkInput3();
            }
            
            
            float a_sqrd;
            float b_sqrd;
            float c_sqrd;
            
            
            //     checking if given triangle is valid
            do {
                a = a_arr[counter];
                b = b_arr[counter];
                c = c_arr[counter];
                a_sqrd = a*a;
                b_sqrd = b*b;
                c_sqrd = c*c;
                
                if((a + b > c) && (a + c > b) && (b + c > a))
                {
                    // Checking for special cases of triangle
                    
                    if(a==b && b==c)
                    {
                        /* If all sides are equal */
                        printf("Triplet %d (a = %f b = %f c = %f) is a triangle. \n It is an Equilateral triangle \n", counter+1 , a , b ,c );
                    }
                    else if(a==b || a==c || b==c)
                    {
                        /* If any two sides are equal */
                        printf("Triplet %d (a = %f b = %f c = %f) is a triangle. \n It is an Isosceles triangle \n", counter+1 , a , b , c );
                    }
                    else if ((a_sqrd + b_sqrd) == c_sqrd || (b_sqrd + c_sqrd) == a_sqrd || (a_sqrd + c_sqrd == b_sqrd))
                    {
                        printf("Triplet %d (a = %f b = %f c = %f) is a triangle. \n It is a right angle triangle \n", counter+1 , a , b, c );
                    }
                else
                    printf("Triplet %d (a = %f b = %f c = %f) is a triangle.\n", counter+1 , a , b, c );
                }
                else
                {
                    printf("Triplet %d (a = %f b = %f c = %f) is NO Triangle. \n", counter+1 , a , b , c );
                }
                
                
                counter++;
            }while(counter < repeats);
            
            
            
            return 0;
        }
        
        // Validation
        // Validate the number of triangles
        
        
        int checkInput0(void){
            int option0,check0;
            char c;
            
            do{
                printf("Enter the amount of triangles you want to check: \n");
                
                if(scanf("%d%c",&option0,&c) == 0 || c != '\n' || option0 == 0 ){
                    while((check0 = getchar()) != 0 && check0 != '\n' && check0 != EOF);
                    printf("[ERR] Invalid number of triangles.\n");
                }else{
                    break;
                }
            }while(1);
            return option0;
        }
        
        // Validate Value of a
        
        float checkInput1(void){
            float option1,check1;
            char c;
            
            do{
                printf("Enter the first side of the triangle: ");
                
                if(scanf("%f%c",&option1,&c) == 0 || c != '\n'){
                    while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
                    printf("\t[ERR] Invalid number for the triplet.\n");
                }else{
                    break;
                }
            }while(1);
            return option1;
        }
        
        // Validate Value of b
        float checkInput2(void){
            float option2,check2;
            char c;
            do{
                printf("Enter the second side of the triangle: ");
                
                if(scanf("%f%c",&option2,&c) == 0 || c != '\n'){
                    while((check2 = getchar()) != 0 && check2 != '\n' && check2 != EOF);
                    printf("\t[ERR] Invalid number for the triplet.\n");
                }else{
                    break;
                }
            }while(1);
            return option2;
        }
        // Validate Value of c
        float checkInput3(void){
            float option3,check3;
            char c;
            
            do{
                printf("Enter the third side of the triangle: ");
                
                if(scanf("%f%c",&option3,&c) == 0 || c != '\n'){
                    while((check3 = getchar()) != 0 && check3 != '\n' && check3 != EOF);
                    printf("\t[ERR] Invalid number for the triplet.\n");
                }else{
                    break;
                }
            }while(1);
            return option3;
        }

Open in new window

Output:

    Enter the amount of triangles you want to check:
if I press ctrl + d immediately after this appears I get an endless loop of:

    Enter the amount of triangles you want to check:
    [ERR] Invalid number of triangles.

But if I add a value for

    Enter the amount of triangles you want to check:
like 2
then I get this output after prssing ctrl + d

    Enter the first side of the triangle: Enter the second side of the triangle: Enter the third side of the triangle: Enter the first side of the triangle: Enter the second side of the triangle: Enter the third side of the triangle: Triplet 1 (a = 0.000000 b = 0.000000 c = 0.000000) is a triangle.
     It is an Equilateral triangle
    Triplet 2 (a = 0.000000 b = 0.000000 c = 0.000000) is a triangle.
     It is an Equilateral triangle
    Program ended with exit code: 0

But I would like the program to end without an output when ctrl + d is pressed.

Thanks in advance.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

scanf doesn't do any error checking.. Ergo Garbage in gives you garbage out..
If you are trying to exit the program use ctrl+BREAK or ctrl-C
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.