So, I'm working on this program and I've encountered a problem with scanf. I'm scanning the input from the user in the form of a string, however if the user just presses enter, scanf doesn't pick up on it and the cursor just moves on waiting for whatever specified input I programmed it to look for. Is there anyway I can skirt around this with like a comparison to 0 or 1, or do I need to go about this in another way? Yes, this is the same program from earlier and I thought to mention this in the other post I made, but I figured this was a different problem in itself so it deserved another post separate from my other problem earlier.
/*
* Scott English
* CS 1412
* tconv
* 1/28/15
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *
input_from_args(int argc, const char *argv[])
{
if (argc == 1){
return stdin;
}
else {
return fopen(argv[1], "r");
}
}
void
rot_13(FILE *src, FILE *dest)
{
int c,j;
while ((c = getc(src)) != EOF)
{
if(c>= 'A' && c <= 'Z')
{
if((j = c + 13) <= 'Z')
c = j;
else
{
j = c - 13;
c = j;
}
}
else if(c >= 'a' && c <= 'z')
{
if((j = c + 13) <= 'z')
c = j;
else
{
j = c - 13;
c = j;
}
}
else
c = c;
fprintf(dest, "%c", c);
}
}
void
convert_all_upper(FILE *src, FILE *dest)
{
int c;
while ((c = fgetc(src)) != EOF)
{
fprintf(dest, "%c", toupper(c));
}
}
void
convert_all_lower(FILE *src, FILE *dest)
{
int c;
while ((c = fgetc(src)) != EOF)
{
fprintf(dest, "%c", tolower(c));
}
}
void
print_all(FILE *src, FILE *dest)
{
int c;
while ((c = fgetc(src)) != EOF)
{
fprintf(dest, "%c", c);
}
}
int
main(int argc, const char *argv[])
{
char answer[4];
FILE *src = input_from_args(argc, argv);
FILE *dest = stdout;
printf("Please enter which conversion -r -u -l\n");
scanf("%s", answer);
if (src == NULL)
{
fprintf(stderr, "%s: unable to open %s\n", argv [0], argv[1]);
exit(EXIT_FAILURE);
}
else if (strcmp(answer,"-r") == 0)
{
rot_13(src, dest);
}
else if (strcmp(answer, "-u") == 0)
{
convert_all_upper(src, dest);
}
else if (strcmp(answer, "-l") == 0)
{
convert_all_lower(src, dest);
}
else
{
printf("%s: is unsupported\n", answer);
}
fclose(src);
return EXIT_SUCCESS;
}