-6

Before posting, I've tried just about everything anyone's mentioned on C to fix my problem, and it hasn't worked (i.e: adding fgets&sscanf in place of scanf, adding getchar() at end (not a good practice), etc., but nothing has worked.

This small sample is a section of code I am working on for a larger project (I normally write a small segment, so troubleshooting is easier), and I have a weird problem of having to 'enter' a response to the singular user input twice, to run (and exit) the program. It doesn't matter what the second number is, as the first answer is the one valued and written to the file (for example, I enter '2' and then a prompt is displayed again so I enter '4', but only the result for the correct entry of '2' is entered into the output file). The whole program works great, except that I have to enter one more value to cause the program to run (and, thus end). A space doesn't get it, and just enter doesn't get it (it will keep prompting until it gets that second number entry). You have to enter a value to terminate the program. Does anyone see why (code follows)? (FWIW, the code does run properly and the correct value is given, and serves my program's greater purpose - if I can just stop that required 'extra value' at the prompt).

Interestingly enough, if I remove the last 'return(0)' from the program, it will take the LAST value entered for x (of the two prompts I get) and store that result in the output file, not the first prompt value. Leave it in, and it will store the first.

I've never had this problem before, and can't make heads-or-tails of why it is taking me two responses to a prompt to run (and end) the program.

Anyone see something that I am obviously blind to on this? (this is C, compiled on Linux, and tested to work other than the glaring 'double prompt issue')

Thanks.

#include <stdio.h>
#include <stdlib.h>

int main()

{

    FILE *encfile;
    int x;
    char val001[]="AAAA";
    char val002[]="AAAB";
    char val003[]="AAAC";
    char val004[]="AAAD";

    encfile = fopen("encoded.enx","w");
                      //opens output file "encfile" in write mode

    printf ("Enter a value from 1 - 4: \n");
                      //prompts for user choice
    scanf ("%i\n",&x);
                      //takes value of 1-4 prompt from user

    if (x == 1)
                      //if input is '1', 

    {
       fprintf(encfile,"%s\n", val001);
                      //prints val001 to file 'encfile'
        return (0);

    }

    if (x == 2)
                      //if input is '2', 

    {
       fprintf(encfile,"%s\n", val002);
                      //prints val002 to file 'encfile'
        return (0);

    }

    if (x == 3)
                      //if input is '3', 

    {
       fprintf(encfile,"%s\n", val003);
                      //prints val003 to file 'encfile'
        return (0);

    }

    if (x == 4)
                      //if input is '4', 

    {
       fprintf(encfile,"%s\n", val004);
                      //prints val004 to file 'encfile'
        return (0);

    }

    else
    if ((x << 1) || ( x >> 4))
                      //if input selection is out of range, 

    {
        printf("You have chosen an invalid number\n");
                     //prints 'out of range' message
        return (0);
    }

    fclose (encfile);
                     //write file closed

    return (0);                                             

}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150

1 Answers1

3

Try removing '\n' from the scanf, like this:

scanf ("%i",&x);

Take a look at this discussion for additional info on using '\n' in a scanf:

Using “\n” in scanf() in C

Community
  • 1
  • 1
  • PERFECT!!! I was tired and put that '\n' on that scanf, and didn't even realize it. As I figured, super-simple problem that I overlooked (and my neighbor who also codes is out of town right now, or I would have made a call first). – Skeetersaurus Mar 29 '16 at 09:59
  • You should accept the answer if that's what you needed. – Lukesivi Mar 29 '16 at 11:40