1

For my summer project I built a cheesy little encryption engine, I wanted to have a feature where the user could enter a string/char series and it would encrypt it right in the terminal but for some reason the program (I think its scanf) is only printing out the first word. It could be the way I determine the end of users input but I believe the problem is scanf and not my code (although I read this and it makes me question myself). Here is the code that is relevent:

printf("would you like to [e]ncrypt or [d]ecrypt, or type a word to encrypt\n");
char q[50];
for(int s = 0; s<50; s++){ //initialize string to '0'
    q[s] = '0';
}
scanf("%s \n", q); //reads input from user
for(int s = 0; s<50; s++){ //for loop to read every char and encrypt it
        if(q[s] == '0'){ //'0' determines the end of line
            break;
        }else{  
            union shifter sh = { 0 }; //uses a union to shift the char to unreadable (relatively) char
            sh.str = q[s];
            sh.i += 5;
            printf("%c", sh.str); //prints out chars one at a time
        }

    }
printf("\n"); //new line when done

inb4 you guys say that the user can't enter a 0, I know this, as a side question, could I initialize it to an unreadable char by doing say

char = 3;

and putting it as a unreadable char (end of test to be exact)

thanks for the help

  • 5
    Since `scanf()` with `%s` stops at the first white space — blank, tab, newline (etc) — there's no surprise that it only reads the first word. Use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html). Also, the trailing white space (the blank and newline in `"%s \n"`) is dangerous, especially for interactive input. See [trailing blank in `scanf()` format string](https://stackoverflow.com/questions/19499060/). – Jonathan Leffler Aug 30 '18 at 21:45
  • how do use fgets() to get user input then, as I have read its for files and not user input. – Quincy Mattor Aug 30 '18 at 21:56
  • 1
    my program now functions as intended, thank you – Quincy Mattor Aug 30 '18 at 22:01
  • 1
    regarding: `q[s] = '0';` The character '0' is 0x30, not 0x00. are you sure that is what you want? – user3629249 Aug 30 '18 at 23:14
  • *how do use fgets() to get user input* very simple. `if( fgets( q, sizeof( q ), stdin ) ) == NULL ) { perror( "fgets failed" ); exit( EXIT_FAILURE ); } // implied else fgets successful. // remove trailing newline: q[ strcspn( q, "\n" ) ] = '\0';` – user3629249 Aug 30 '18 at 23:22
  • how the user can enter a 0x00 character: press and hold then, on the keypad type 000 then release the then click 'enter' – user3629249 Aug 30 '18 at 23:27

1 Answers1

-1

your %s will only match the first string of non-whitespace characters, then discard the other characters after and including the first whitespace.

see http://www.cplusplus.com/reference/cstdio/scanf/

Srini
  • 1,619
  • 1
  • 19
  • 34
aaron
  • 1,746
  • 1
  • 13
  • 24
  • 2
    What will discard the rest of what? Please be more precise. The `"%s \n"` format will skip leading white space, read an unbounded sequence of non-white-space characters, and then the trailing white space in the format string keeps reading and ignoring white space (including newlines) until it comes across another non-white-space character, which it leaves in the input stream ready for the next input operation. – Jonathan Leffler Aug 30 '18 at 23:21
  • It doesn't answer the question. – 273K Jul 17 '23 at 05:20