0

I am new to C programming. I was curious as to see how much I have learnt C. Therefore I thought of creating a program in which I could simply create a file and write in it. The name of the file, I thought, should be less that 100 chars. But it doesn't matter if it is a string or one word or a letter. I couldn't complete because I was stuck on fact that how to input a string for a file name(eg, Project work, New Doc1, etc)

So I wrote this;

int main()
{

    int a = 0;

    while(a != 5)
    {
        puts("Put a number: ");
        scanf("%i", &a);
        if(a == 1)
        {
            char name[30];
            printf("Put a name: ->>");
            for(int i = 0;i < 31 && name[i] != '\n';i++)
            {
                name[i] = getchar();

            }
            char ex[50] = ".txt";

            strcat(name,ex);
            printf("%s",name);

        }
    }

    return 0;
}

The problem is while inputting the name, it doesn't stop at the next (when I press enter) and some how it is not printing the right file name either.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jubjub
  • 1
  • 2
  • You increment i after the newline is written to `name[i]`, so the condition is not checking the value you want it to. – William Pursell Aug 23 '15 at 00:45
  • First time `name[i] != '\n'` is tested, `name[i]` is not initialized. – chux - Reinstate Monica Aug 23 '15 at 00:51
  • 1
    This is a very common problem — mixing `scanf()` with other I/O primitives. The 'number' input leaves a newline in the input buffer, so the first character read in the `for` loop is a newline. You are also comparing `name[0]` with newline before you've entered anything into it — bad idea (undefined behaviour). The next time around the loop, you're trying to compare an uninitialized character with newline again; still not a recipe for happiness. Also,your array is of size 30, but you're testing against 31; more trouble eventually, but you need to fix other things first. – Jonathan Leffler Aug 23 '15 at 00:55
  • 2
    So you have to use `getchar()` to meet your educational goals? It would be by far simpler to use `if (scanf("%25s", name) != 1) break;`. You need to allow a maximum of 25 user-entered characters because you're going to append `.txt` and you need a null terminator too (so 5 places must be reserved in the string). – Jonathan Leffler Aug 23 '15 at 01:00
  • Also, declaring your variables at the top of the function is a good convention! – cdonts Aug 23 '15 at 01:05
  • 1
    @cdonts declare your variables when you need them. – chux - Reinstate Monica Aug 23 '15 at 02:50
  • @cdonts Many paradigms are possible. I found local declarations easier to maintain as in http://stackoverflow.com/a/3773458/2410359 – chux - Reinstate Monica Aug 23 '15 at 03:03
  • 1
    @chux I'll take that into account. Thanks for the tip – cdonts Aug 23 '15 at 03:11

2 Answers2

1

There's a lot of problems with you approach.

  1. It's not good to mix scanf with another input primitives, you must flush stdin from any remaining characters.
  2. Any string must end in '\0' in order to mark that string as complete. So you must reserve space to this character.
  3. When you concat you must obey the limit of the string.
  4. If you use printf, the string will be only displayed after flushing the stdout (when you put '\n' in the end of the string, flush is done)

Try this:

#include <stdio.h>
#include <string.h>   

int main()
{

    int a = 0;

    while(a != 5)
    {
        int ch;
        puts("Put a number: ");
        scanf("%d", &a);
        /* flush any remaining characters */
        while ((ch=getchar()) != EOF && ch != '\n'); /* issue 1 */
        if(a == 1)
        {
            int i = 0;
            char name[30];
            printf("Put a name: ->>");
            fflush(stdout); /* issue 4 */

            while ((ch=getchar()) != EOF && ch != '\n' && i < 25) /* issue 3 */
                name[i++] = ch;
            name[i] = '\0'; /* issue 2 */

            /* flush any remaining characters [if input > 25 chars] */
            if (ch != EOF && ch != '\n') while ((ch=getchar()) != EOF && ch != '\n');

            char ex[50] = ".txt";

            strcat(name,ex); /* issue 3 */
            printf("%s\n",name);

        }
    }

    return 0;
}

Also, consider use getline and atoi instead of getchar and scanf

iuridiniz
  • 2,213
  • 24
  • 26
0
#include<stdio.h>
main()
{
    static char stop_char='y';
    char input=0;
    do{
        printf("please input a character\n");
        scanf("\n%c",&input);
    }while(input!=stop_char);
}
mike
  • 1