EOF function in c++ |c - c

for example there is code
#include <algorithm>
#include <stdio.h>
#include <iostream>
int intcomp(int *x,int *y) { return *x-*y;};
int a[10000];
int main(void){
int i; int n=0;
while (scanf("%d",&a[n])!=EOF)
n++;
qsort(a,n,sizeof(int),intcomp);
for (int i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
how tell computer that EOF is reached?

You mean when entering input interactively?
In a windows shell, ctrl+z on a line on its own. In a *nix shell, ctrl+d. Or just put your input in a file and pipe it, then not only will eof be detected at the appropriate time but also you can automate your testing.

You should use CTRL+Z combination (or somehow input character with code 26, for example, by pressing ALT+2+6 on additional keyboard)

Related

Avoid pressing enter with getch() on Linux ( GCC ) "No-echo"

In the next code: I don't have to press Enter to get the character with getch() and this is only applicable on Windows (mingw) . I am programming a simple stopwatch which reacts if a keyboard-key is pressed without the need to press ENTER, but the same thing doesn't work on Linux ( GCC ). And I have to find a solution only using getch() with no echo. I've been Googling around with no luck. Thank you in advance.
PS: I am a c/c++ beginner.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
printf("\n\t\tStopwatch \n\n \t Press S to start \n\n");
time_t start;
char c ;
struct tm tm ;
do {
c=getch(); /*get the Character without pressing ENTER*/
if (c!= 'S' && c!= 's') printf("\nWrong key. Please press'S' to Start\n");
} while(c!= 'S' && c!= 's');
start=time(NULL);
tm = *localtime(&start);
printf("\n Starting time :: %d:%d:%d \n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return 0;
}
Unix (and so Linux) have a concept of terminal (TTY) which can be pretty complicated.
Every process can be associated with a terminal (and lot actually are).
This terminal has different options like the ECHO you mentioned and the LINE BUFFER (which is the enter problem which you reported).
To set a terminal the low level APIs are termios (see "man termios").
Another more friendly API is ncurses (see "man ncurses"). For instance at https://www.mkssoftware.com/docs/man3/curs_inopts.3.asp you can see some function for different settings.
An easy example with ncurses (you can compile with "gcc -O2 source.c -o output -lncurses"):
#include <stdio.h>
#include <ncurses.h>
int main(void)
{
initscr();
cbreak();
noecho();
int n = getch();
printf("%d %c\n", n, n);
}

How to use `getch` function of c in Linux?

I had installed ncurses library in Linux mint and still I can't use getch function in c. I am using Linux mint 18.2.
Here is my program:
#include <stdio.h>
#include <curses.h>
int main() {
char k;
printf("how are you");
k = getch();
printf("%c",k);
}
and here is the output:
ram#ram$ gcc-7 test.c -lcurses
ram#ram$ ./a.out
how are you�ram#ram$
It does't wait for me to press any key and terminate to quickly. I don't want to install conio.h for Linux. How can I use getch and getche function in Linux? Please don't tell me to make my own function. I am still a noob. Or there must be alternatives.
Here's a "corrected" version, explaining what's wrong in the comments:
#include <curses.h>
#include <stdio.h>
int main(void)
{
// use the correct type, see https://linux.die.net/man/3/getch
int k;
// init curses:
initscr();
// in curses, you have to use curses functions for all terminal I/O
addstr("How are you?");
k = getch();
// end curses:
endwin();
printf("You entered %c\n", k);
return 0;
}
This still isn't good code, you should at least check whether you got a valid character from getch().
It's also important to note that getch() isn't a "function of C". It's part of curses, a well-known platform-independent API for console/terminal control, with implementations e.g. for *nix systems (ncurses) and Windows (pdcurses). It's not part of the language C.

drawing operation was attempted when there was no current window error while using getch() in C

In C there is a error I'm facing
that when ever I use getch() command in my code and run it either in codeblocks or the .exe file after everything is done and when the control goes to getch() command it shows an error pop up window saying
Drawing operation was attempted when there was no current window.
#include <stdio.h>
#include <conio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d", a);
getch();
return(0);
}
Using Code::Blocks 16.01.
Use _getch() instead of getch():
#include<conio.h>
_getch();
Source: https://learn.microsoft.com/cpp/c-runtime-library/reference/getch
I researched, what I understood was that the command getch is deprecated and the command you can use to replace it is the _getch.
There is more information at this link:
https://learn.microsoft.com/cpp/c-runtime-library/reference/getch

How to use System(const char*) in TC++

Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++

Why does the following program not generate any visible output?

The following C program doesn't printing anything on the screen.
I compiled the program with gcc:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
Most likely, stdout is line buffered. Your program does not call fflush or send a newline so the buffer does not get written out.
#include <stdio.h>
int main(void) {
printf("hai\n");
for(;;)
;
return 0;
}
See also question 12.4 and What's the correct declaration of main()? in the C FAQ.
This is caused by the buffering which takes place in stdio (i.e. it is not output immediately unless you tell it to by including a \n or fflush). Please refer to Write to stdout and printf output not interleaved which explains this.
(p.s. or the compiler is not happy about the typo in #include)
Standard output tends to be line buffered by default so the reason you're not seeing anything is because you haven't flushed the line.
This will work:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai\n");
for(;;)
;
return 0;
}
Alternatively, you could fflush standard out or just get rid of the infinite loop so the program exits:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai");
return 0;
}
but you probably want the newline there anyway.
Your for(;;) loop stops the stream from being flushed. As others have suggested, add a newline to the string being output, or flush the stream explicitly:
fflush( stdout );
after your printf. And correct the spelling of #include.

Resources