0

I am trying to make an input that scans two strings and saves them in specific sizes. If the size is exceeded, the program should print an error statement, if no number is given, and error statement is given. The first input is only one word, while the second is a statement. If the first one does not have input, then it stops running. here is what I have.

    char name[51];
    char sentance[141];

    printf("Enter Username: ");
    if (scanf("%50s", username) !=1){
        printf("NOT THE INTENDED LENGTH\n");
        exit(1);
    }
    

    printf("Enter the user's tweet: ");
    getchar();
    if (fgets(tweet, 141, stdin)==NULL){
        printf("Failed to read");
        exit(1);

For some reason It wont give the error statements when its longer, and I have no idea how to make an error statement for when it is shorter. Any ideas?

Jacob G
  • 31
  • 3
  • You cannot "make it so that the input of the string is not zero, or longer then intended".. What a user types it up to them - something beyond code control. What code can do is read input up to a `'\n'` and then determine how much of that to save and report back if it is not acceptable. – chux - Reinstate Monica Apr 01 '22 at 01:58
  • "For some reason It wont give the error statements when its longer" is because code makes no attempt to detect excessively long input. – chux - Reinstate Monica Apr 01 '22 at 01:59
  • What do you want to happen to the excessive input? leave for next input, consume it and throw away, stop code? – chux - Reinstate Monica Apr 01 '22 at 02:01

0 Answers0