laserlight
c is a char
laserlight
c is a char
Then that is incorrect. getchar() returns an int, and c should likewise be an int, not a char.Originally Posted by r00t
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
lol, it worked for me when I used char, and when I pressed ctrl+c, it quit from the loop,
and then I could work with an array
The problem is that char can be signed or unsigned, depending on the implementation. If char is signed, and if EOF is within the range of char, then everything is fine. However, if char is unsigned, or if EOF is not within the range of char, then you could have a problem.Originally Posted by r00t
hmm... so you are saying that CTRL+C does not exit the program, but instead appears to simulate EOF? What is your current program?Originally Posted by r00t
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the current program is the code in the first post, however i didn't implement any EOF... i don't know how it works with scanf.. any ideas?
again, i want to read strings line by line, then when I hit ctrl+c sort it and print it out.
This might not have been made clear to you, but on the Windows console, CTRL+Z is used to simulate EOF. You would press enter, press CTRL+Z, then press enter again. CTRL+C might work too, but then you'll get into a discussion about signal handling and such instead of just about simulating EOF as you want to do.Originally Posted by r00t
Firstly, if you want to read strings with scanf(), you should not use the %s format specifier by itself. You should also account for the maximum length of the string, in this case 9, so you would use scanf("%9s",sp[i]). Note that %s will only cause the input to be read until the first whitespace character. Here is a simple example:Originally Posted by r00t
That said, it may be better to compare with 1 instead of EOF since that is the expected return value of scanf for a successful read where the format string has only 1 format specifier.Code:#include <stdio.h> int main(void) { char str[10]; while (scanf("%9s", str) != EOF) { printf("%s\n", str); } puts("Done!"); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hey guys,
i've completed the assignment
so the task was to read strings line by line until Ctrl+D(EOF signal) is pressed.
then sort it.
max length of line is 100
max number of lines is 50
if number of lines>100 then print **truncated
if length of line>50 then sort the first 50 chars of string and then put **more than 50 errorr
here is the code
thanx to all!Code:#include <stdio.h> #include <stdlib.h> #define MAX 100 #define N 51 void sort_lines(char **line, int *err, int n); void swap(char **, char **); void swap_int(int *, int*); int main(void) { char **line; int i = 0, j = 0, *err; int c, n, k; err = malloc(N*sizeof(int)); line = malloc(N*sizeof(char*)); line[0] = malloc(MAX*sizeof(char)); while((c=getchar()) != EOF) { if(c=='\n') { if ( j>=MAX ) err[i]=1; j=0; i=i+1; line[i] = malloc((MAX+1)*sizeof(char)); } else { if (j>=MAX) continue; line[i][j] = c; j=j+1; } } sort_lines(line, err, i); for (k=0; k < i ; k++) { if (err[k]==1) printf("*** Error**length is more than 100\n"); else printf("%s\n",line[k]); if (k>50) {printf("*** Error** Truncated\n");break;} } return 0; } void sort_lines(char **line, int *err, int n) { int i = 0; int j = 0; int y = n; for(i = 0; i < y; ++i) for(j = i + 1; j < y; ++j) if(strcmp(line[i], line[j]) > 0) { swap(&line[i], &line[j]); swap_int(&err[i], &err[j]); } } void swap(char **p, char **q) { char *tmp; tmp = *p; *p = *q; *q = tmp; } void swap_int(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; }