String has contents even though there is no input from user

I am trying to "trap" keyboard inputs from user, meaning the code will prevent them from entering certain characters, which in this case prevents the input of numbers and special characters.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

char buffer[30];
void in_text();

int main(void)
{
in_text();
printf ("\nThe string you entered is: %s", buffer);

    return 0;
}


void in_text(void)
{
char buffe[30]; 
char c1;
int x = 0, hit = 0;

printf("Enter string: ");


while (hit!=1)
{   
    c1 = getch();

    if (c1=='\b' && x!=0)
    {
        buffe[--x] = '\0';
        printf ("\b \b");
    }

    if (isalpha(c1) || isspace(c1) && c1!='\r') 
    {
        buffe[x++] = c1;
        buffe[x+1] = '\0';
        printf("%c", c1);
    }   

    if (c1=='\r' && strlen(buffe)==0)
    {
        buffe[x+1] = '\0';
    }   
    else if (c1=='\r' && strlen(buffe)>1)
    {
        printf ("%d", strlen(buffe));
        printf ("\n%s", buffe);
        hit = 1;
    }   
    else{
    }       
}

strcpy(buffer, buffe);

return 1;
}

This code tries to mimic the scanf input style that one can see and press backspace to delete a previously entered character. I want to "trap" the enter key such that when the user presses "Enter", the program checks if the current string (buffe) contains at least one valid character and that when the program checks for input and finds out that the user pressed the Enter key and the string does not contain anything, the loop continues.

The problem with my code is that when I do press Enter, immediately after the program starts, the strlen function returns a value larger than 0, which I assume is not normal since there is no input from the user yet, aside from that "Enter" key that the user pressed.

Any thoughts on this?

Answers


Reading uninitialised variables like done to buffe here:

if (c1=='\r' && strlen(buffe)==0)

provokes undefined behaviour, anything could happen afterwards. Do not do this.

Always initialise variable before reading them.

In this case you might like to simply do:

char buffe[32] = "";

or (as already proposed by others) the more generic way:

char buffer[32] = {0};

More complicated but also valid would be to do:

char buffer[32];
strcpy(buffe, "");

or

char buffer[32];
memset(buffe, 0, sizeof(buffe));

May be try

char buffe[30] = {0};

(I have not tried this thouggh)


Memory always contains something. The space used by your variable contains something, and calling strlen() on that just so happens not to blow up somewhere.

Note that the operating system usually reads a full line (and allows editing it), shipping it to the reading application only on ENTER. To handle this is significatly harder than plain reading. Are you sure that it isn't good enough to get a line, check it and complain or go ahead?


since your buffe will get memory on stack it will contain some garbage value you better initialize it with zero or better way is to use memset() function.


Need Your Help

How to find proxy server and password from .pac file

proxy pac

I am using dynatrace ajax edition and it requires an proxy server and password to connect to dynatrace server.

4 queries on the same table, a better way?

sql mysql query-optimization

Currently I am doing one query, with 3 sub queries,