`I am trying to limit the amount of characters that a user can enter for the address(/ in the input buffer):
for example (if maximum allowed input was 30 then ):
->4 Mary drive lane, Mt. Pyle, A<- when the user typed that I am trying to get the curser -> "_" to stop moving...
I have searched and have not come up with anything but I may have used different keywords than what was needed.

1. I am thinking that this has to do with stdin
2. I have tried getch put example: getch(50) put aparently getch takes no arguments...
3. Thank you in advance
4.If you don't understand my problem then I will try to explain it further
(I started to use fgets when I saw someone here explain to someone)

printf ("\n\t\t   Enter Address:");
    //scanf ("%34s",pat_Ptr->address);
    if ( fgets ( pat_Ptr->address, 50, stdin ) != NULL ) {
        fputs ( pat_Ptr->address, stdin ); /*this writes the input the user enters to a string*/
    }
    (pat_Ptr->address)[strcspn ( (pat_Ptr->address), "\n" )] = '\0';
    printf ("\n Address is:%s\n",records.address);

Edited 6 Years Ago by ShortYute: Please do not use CODE SNIPPED option to post your questions. Use FORUM THREAD

Think of console input as a two tier operation. At the top level is the shell (the console itself), which talks to the keyboard driver and buffers characters typed by the user. When the user signals the end of input (usually in the form of a newline), the shell stops gives control over to your program and the input stream begins buffering characters from the shell buffer.

The disconnect is at the newline. If the shell blocks until a newline, control doesn't reach your program until the user has signaled the end of input. Thus, there's no way your program can control how the user types characters without bypassing the shell and controlling input from the keyboard driver itself. This can be done, but not in a standard way. Here's one way using the conio.h library:

char base[31];
char *p = base;
int limit = 30;
int ch;

while ( --limit >= 0 && ( ch = getch() ) != '\r' )
  *p++ = ch;

Take note that by bypassing the shell buffer, you also need to manually handle things like backspaces.

Think of console input as a two tier operation. At the top level is the shell (the console itself), which talks to the keyboard driver and buffers characters typed by the user. When the user signals the end of input (usually in the form of a newline), the shell stops gives control over to your program and the input stream begins buffering characters from the shell buffer.

The disconnect is at the newline. If the shell blocks until a newline, control doesn't reach your program until the user has signaled the end of input. Thus, there's no way your program can control how the user types characters without bypassing the shell and controlling input from the keyboard driver itself. This can be done, but not in a standard way. Here's one way using the conio.h library:

char base[31];
char *p = base;
int limit = 30;
int ch;

while ( --limit >= 0 && ( ch = getch() ) != '\r' )
  *p++ = ch;

Take note that by bypassing the shell buffer, you also need to manually handle things like backspaces.

Thank you, but when i tried it

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

int main ()
{
	char base[31];
	char *p = base;
	int limit = 30;
	int ch;

   printf ("enter a large word\n");
   fgets ( base, 30, stdin );
	while ( --limit >= 0 && ( ch = getch() ) != '\r' )  {

  	*p++ = ch;
   printf ("enter a large word\n");
   //scanf ("%30s",base);
   }
   system ("pause");
}

It only reads it as input when I newline (press enter)

Thank you, but when i tried it

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

int main ()
{
	char base[31];
	char *p = base;
	int limit = 30;
	int ch;

   printf ("enter a large word\n");
  // fgets ( base, 30, stdin );
	while ( --limit >= 0 && ( ch = [B]getche[/B]() ) != '\r' )  {

  	*p++ = ch;
   printf ("enter a large word\n");
   //scanf ("%30s",base);
   }
   system ("pause");
}

It only reads it as input when I newline (press enter)

I changed it to getche (for echo) so I could see it on screen... I also understand what you are saying about the test for everything... thank you :) (I looked for an edit but I didn't see one... it seems it only appears just after you post... I'm not disobeying forum rules, I just didn't see the edit button...

Edited 6 Years Ago by ShortYute: n/a

This question has already been answered. Start a new discussion instead.