tags:

views:

1377

answers:

10
#include <stdio.h>
#include <conio.h>
main()
{
    char ch,name[20];
    int i=0;
    clrscr();
    printf("Enter a string:");
    while((ch=getch())!='\n')
    {
     name[i]=ch;
     i++;
    }
name[i] = '\0';
    printf("%s",name);
}

when I give abc as input and if I press enter its not working. Can anyone let me know why the condition ch=getch() != '\n' is not becoming false when I press enter. I have also observed that ch is taking \r instead of \n. Kindly let me know. Thanks

+5  A: 

Try \r instead of \n

\n is the new line character (0x0A) or 10 decimal, \r is the carrige return character (0x0D) or 13 decimal.

The return key is a carrige return.

Binary Worrier
A: 

on some system the newline is "\r\n" carriage return (enter) is "\r"

A: 

That is because the return key on your keyboard is represented internally as '\r' not '\n'. In order for that specific example to work, you would need to trap '\r' instead.

Matthew Brubaker
A: 

An enter is actually "\r\n" (carriage return and line feed) on Windows. getchar() will return '\n' I think.

Spikolynn
+6  A: 

Use '\r' and terminate your string with '\0'.

Additionally, you might try to use getche() to give a visual echo to the user and do some other general corrections:

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

#define MAX_NAME_LENGTH 20

int main()
{
    char ch, name[MAX_NAME_LENGTH];
    int i=0;
    clrscr();
    printf("Enter a string:");
    while ( ((ch=getche())!='\r') && (i < MAX_NAME_LENGTH - 1) )
    {
        name[i]=ch;
        i++;
    }
    name[i] = '\0';
    printf("%s\n",name);

    return 0;
}
schnaader
Perhaps it's a formatting issue, but you meant to terminate with null - "\0", right...
jmanning2k
It's the same thing.
Cirno de Bergerac
Yes, for chars, '\0', NULL and 0 mean the same, but using '\0' makes it more clear, agreed and corrected.
schnaader
As long a you're correcting things, you might want to cap that while loop at 19 iterations, make main() explicitly "int main(void)", flush stdout after the prompting printf(), and return a value.
Cirno de Bergerac
Thanks, corrected these.
schnaader
A: 

I think it would be better to simply use:

scanf("%19s", name);

instead of getch thing.

empi
but only if you want buffer overflows...
Alnitak
when using while you can get the same buffer overflow if you don't control value of i. what's the difference?
empi
@Alnitak: i updated the code to prevent buffer overflow, you thought about that or is there anything wrong with scanf?
empi
A: 

Thank your for your immediate replies. I have read the following input using the instruction scanf("%[^EOF]",name);

abc (enter) cdf (enter) ctrl+z

when I have observed the array "name". the array is having like this abc\ncdf\n. So I am confused why sometimes enter key is represented as \r and sometimes \n. is this difference lies with the getch and scanf/getchar. please let me know.Thanks alot in advance

A: 

It depends on your system. Also you must know that getch() it's a part of the XSI Curses Standard issued by X-Open or OpenGroup and not related to the Borland specific conio.h header.

You must use getchar(3) or getc(3), which all of them belong to the ISO/IEC 9899:1990 standard. Both calls retreives just one byte from the controlling terminal/console or the input stream, respectively.

If you are calling them from a Windows platform, you retreive with the Enter key, the '\r', '\n' sequence, instead of just '\n'. Then you must create a loop arround the getchar(3) or getc(3) calls. Also, a call to fgets(3) over stdin would work to obtain the '\r', '\n' sequence.

Another point, is that some platforms needs flush the stding stream, then you must apply the fflush(3) call to the stdin stream.

fgets(3) and fflush(3) belongs to the ISO/IEC 9899:1990 standard.

daniel
A: 

Please make me clear about the following. Is it the right way to read a multi line string using the following instruction scanf("%[^EOF]",name); . please suggest me if there is any alternative. Thanks

certainlly not. just use *read()* or *fgets()*
daniel
A: 

***Solution to getch()!='\n' problem:

keyboard return key is: the carriage return, that is CR. this is found by running the program and analyzing the results.

when u press enter key, the ascii value of Enter key is not returned but CR is returned!

Hence (ch=getch())!='\n' should be changed to any of the following statements:

ch=getch())!='\r'

ch=getch())!=13 // ASCII for CR: decimal

ch=getch())!=0xd // hexadecimal for CR

ch=getch())!=0xD // hexadecimal for CR

ch=getch())!=015 // octal for CR


Prof. M.Raja