Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loop in C clipboard program doesn't work properly?

Tags:

c

while-loop

I want this program to first ask user to start or exit which works fine:

#include <stdio.h>

int main()
{
    int ask;
    char ask2;
    char clip[100];
    
    printf("1.start\n2.exit\n");
    scanf("%d", &ask);
    
    while (ask == 1)
    {
        //options
        printf("what you want to do :\nA.Enter on Clipboard\nB.Check or Edit Clipboard\nC.Exit\n");
        printf("enter A/a,B/b,C/c to select option : ");
        scanf("%s", &ask2);
        printf("\n");
    
        if (ask2 == 'A' || ask2 == 'a')
        {
            //option 1
            printf(":");
            scanf("%s", &clip);
        }
    
        //option 2
        else if (ask2 == 'B' || ask2 == 'b')
        {
            printf("Clipboard:->%s", clip);
        }
    
        //exit
        else if (ask2 =='C' || ask2 == 'c' )
        {
            break;
        }
    
        //error handling
        //else if (ask2 != 'A' || ask2 != 'a' || ask2 != 'B' || ask2 != 'b' || ask2 != 'C' || ask2 != 'c')
        //      {
        //          printf("wrong input !");
        //      }
    }
    
        return 0;
}

...but after that when i input take for while loop the loop runs only once but I want it to ask again and again for input unless user types exit but it terminates the loop after one input which i don't want, help. also there were no errors only while loop gets terminated on first run.

like image 986
Aadil Avatar asked Dec 13 '25 03:12

Aadil


1 Answers

As noted in the good comments, you probably need to get a bit more familiar with the entry of characters as opposed to strings utilizing the "scanf" function. As noted, one would want to utilize the " %c" formatting for entry of a single character, "%s" for a single string, and "%[^\n]%*c" if multiple words are to be entered into a character array (e.g. a string).

With that in mind, following is a refactored version of your code to include the good suggestions noted in the comments.

#include <stdio.h>

int main()
{
    int ask;
    char ask2, c;       /* Added work character variable "c"    */
    char clip[100];

    printf("1.start\n2.exit\n");
    scanf("%d", &ask);

    while (ask == 1)
    {
        //options
        printf("What you want to do :\nA.Enter on Clipboard\nB.Check or Edit Clipboard\nC.Exit\n");
        printf("Enter A/a,B/b,C/c to select option : ");
        scanf(" %c", &ask2);                /* Entry of character per the good comments     */
        printf("\n");

        printf("Selection: %c\n", ask2);    /* Needed this to clear out the buffer          */

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

        if (ask2 == 'A' || ask2 == 'a')
        {
            //option 1
            printf("Enter your text: ");
            scanf(" %[^\n]%*c", clip);       /* As noted in the comments for multiple words  */
        }

        //option 2
        else if (ask2 == 'B' || ask2 == 'b')
        {
            printf("Clipboard:-> %s\n", clip);
        }

        //exit
        else if (ask2 =='C' || ask2 == 'c' )
        {
            break;
        }

        //error handling
        //else if (ask2 != 'A' || ask2 != 'a' || ask2 != 'B' || ask2 != 'b' || ask2 != 'C' || ask2 != 'c')
        //      {
        //          printf("wrong input !");
        //      }
    }

    return 0;
}

One other thing to note, when I was running the program on my Linux system, the program would not prompt for the entry of data into the clipboard character array due to leftover data in the input buffer. Therefore, that is the reason for adding the statement "while ((c = getchar()) != '\n' && c != EOF);" to make sure the buffer was clear and ready for the entry of a word or words.

With those bits of refactoring, following is sample input and output at the terminal.

craig@Vera:~/C_Programs/Console/Clip/bin/Release$ ./Clip 
1.start
2.exit
1
What you want to do :
A.Enter on Clipboard
B.Check or Edit Clipboard
C.Exit
Enter A/a,B/b,C/c to select option : a

Selection: a
Enter your text: A short sentence
What you want to do :
A.Enter on Clipboard
B.Check or Edit Clipboard
C.Exit
Enter A/a,B/b,C/c to select option : b

Selection: b
Clipboard:-> A short sentence
What you want to do :
A.Enter on Clipboard
B.Check or Edit Clipboard
C.Exit
Enter A/a,B/b,C/c to select option : c

Selection: c

FYI, I did multiple selections of input and printing of the clipboard variable contents.

The main takeaway from this is probably delve into some more tutorials on the input of characters and/or strings.

like image 51
NoDakker Avatar answered Dec 15 '25 18:12

NoDakker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!