1

I have already looked at this similar question but i am still wondering if there is another way to stop 1) the terminal echoing with portabilty as this is an assignment and I have already had one java program crash and burn on my teachers computer 2) in my program i search for a '\n' char then if it isn't the first char use getchar then putchar till the next '\n' char which works fine when using redirected stdin but when I try using the program without redirection the enter key is always echoed, is this to do with the terminal echoing or do i need to check for a diffrent char apart from '\n'? I have also tried including '/r' and done lots of googling but it seems the answer to the echo is can't be done with portabilty?

#include <stdio.h>
#include <string.h>

int first_line(char);
int main(){
    
char c;

while((c = getchar())!=EOF){
    first_line(c);
}   

return 0;
}

int first_line(char c){
if (c != '\n'||c != '\r'){
    putchar(c);

    do{
        c = getchar();
        putchar(c);}
    while( c !='\n');
}

return 0;
}

Thanks Lachlan

Community
  • 1
  • 1
UNECS
  • 533
  • 1
  • 9
  • 20
  • do you get the echo in the `do-while` if so convert to `while((c=getchar()) != '\n')` – keety Apr 17 '12 at 05:35
  • There is multi-platform portable way of turning off echo. For UNIX and similar platforms you probably have to use termcap, but that wont work for Windows. – Some programmer dude Apr 17 '12 at 05:55
  • Using `termios` to control character-at-a-time input and echoing is portable between systems that claim `posix` compatibility. Use of termios is shown in the second answer on the `similar question` page mentioned at the start of your question at http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar Also, note `getchar()` returns an `int` not a `char`. – Brian Swift Apr 17 '12 at 08:36
  • @BrianSwift; Thanks I did see that but I don't want to risk a no compile at this stage if it isn't compatible but will give that a go just for interests sake anyway. – UNECS Apr 17 '12 at 10:39
  • @UNECS if the instructor doesn't provide a system with the same build tools environment used for evaluating your submissions on which you can test your code before submitting, I'd suggest you configure a system (perhaps in a virtual machine) to match the instructors configuration as closely as practical. – Brian Swift Apr 17 '12 at 16:44

1 Answers1

1

For a start try with the following :

1) the condition should be if (c != '\n' && c != '\r')

2) and the while loop ,in case if terminal is line buffered then you are better of using getchfrom ncurses library the library packages should be there for most platforms.

    while((c =getchar())!='\n') {
       putchar(c);
    }
keety
  • 17,231
  • 4
  • 51
  • 56
  • If I use 1 as soon as there is a new line eg at the end of a line with text the program will exit an with 3 it will skip the newline at the end of a line resulting in lines not having correct spacing between them – UNECS Apr 17 '12 at 06:06
  • thanks I've been looking for getch but could only find it In c.... Library will try curses – UNECS Apr 17 '12 at 06:23
  • Thanks for the ncurses idea but as i have to send the assignment in uncompiled it won't work without using the -lncurses arg at the gcc complier will it? [link](http://math.hws.edu/orr/s04/cpsc225/curses.html) – UNECS Apr 17 '12 at 10:37