0

I wrote a code to implement Julias Caesar Cryptography in C.
The problem is that my fgets() is not waiting for input and skipping to fputs().

#include<stdio.h>
int main(){
    int size;
    printf("Enter the size of the string: ");
    scanf("%d",&size);

    int key;
    printf("Enter the key: ");
    scanf("%d",&key);

    char a[size];
    printf("Enter the string: ");
    fgets(a,size,stdin);

    for(int i=0;i<size;i++){
        a[i]+=key;
    }

    fputs(a,stdout);
    return 0;
}

I used CodeBlocks 17.12 to compile this C program.
My OS is Windows 7 (32-bit)

Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

1

Welcome to stackoverflow.

When you enter your answer, there is a newline char at the end (\n) in the buffer. When fgets() reads your input it reads the newline to. You can remove the newline, or use a regex to skip it, or fgets() once on the line so that you can use scanf() once more as suggested in this other answer that may help you.

And, please, remember to search in stackoverflow before posting a question!

scoudert
  • 189
  • 2
  • 9