| | |
getchar()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I've been trying to read characters from the keyboard using 'getchar()' but my code always misses one character. Can anyone tell me why this is
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char string[50]; int i; printf("Enter a string: "); for (i=0; i < strlen(string); i++){ string[i] = getchar(); } printf("%s", string); }
string[i]=getchar(); I don't think this will work properly. getchar() expects you to hit enter after the input of each character which means that '\n' will be left in the input buffer which will be read the next time getchar() executes. Which means that each character will alternate with a '\n'.I might be wrong though. Feel free to correct me.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
string[i]=getchar();I might be wrong though. Feel free to correct me.
Salem is right. Once you get past the concern he pointed out, there are also additional problems that the code has no error checking, nor does it terminate the input string with a zero byte.
It should be like this (yeah, I like writing nonesence):
c Syntax (Toggle Plain Text)
int i = 0; while ((string[i++] = getchar())!='\n'); string[i]='\0';
You could do it like that:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 50 int main(int argc, char *argv[]) { char string[SIZE], c; int i; // just a test printf("len = %d\n", strlen(string)); // len = 0 printf("Enter a string: "); for (i=0; i < SIZE; i++){ c = getchar(); if (c == '\n'){ string[i] = '\0'; break; } string[i] = c; } printf("string entered = %s", string); }
In C you can code our own bugs, in C++ you can inherit them.
Just modifying Sci@phy's code...
This will do the trick...
c Syntax (Toggle Plain Text)
while ((string[i++] = getchar())!='\n' && i < SIZE); string[i]='\0';
This will do the trick...
regards,
Ahamed.
Ahamed.
•
•
•
•
I've been trying to read characters from the keyboard using 'getchar()' but my code always misses one character. Can anyone tell me why this is
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char string[50]; int i; printf("Enter a string: "); for (i=0; i < strlen(string); i++){ string[i] = getchar(); } printf("%s", string); }
c Syntax (Toggle Plain Text)
for (i=0; i < strlen(string); i++){ string[i] = getchar(); }
•
•
•
•
> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.
The rest is getting better though.
String is uninitialised... you get junk for len... for sure...
•
•
•
•
You could do it like that:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 50 int main(int argc, char *argv[]) { char string[SIZE], c; int i; // just a test printf("len = %d\n", strlen(string)); // len = 0 printf("Enter a string: "); for (i=0; i < SIZE; i++){ c = getchar(); if (c == '\n'){ string[i] = '\0'; break; } string[i] = c; } printf("string entered = %s", string); }
better way...
C Syntax (Toggle Plain Text)
while ((string[i++] = getchar())!='\n' && i < SIZE); string[i]='\0';
regards,
Ahamed.
Ahamed.
![]() |
Similar Threads
- Difference betn getch(),getche(),getchar() functions (C)
- Java equivalant to C's getchar() (Java)
- query about getchar() (C++)
- help on 'Segmentation Fault' (C)
- Clear getchar in a loop (C)
Other Threads in the C Forum
- Previous Thread: PROBLEM: getchar() does not work within a function.
- Next Thread: pthread
Views: 2578 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






