I use windows 8, and Code::Block 13.12.
I have code C language like that:
#include <stdio.h>
#include <conio.h>
main()
{
while (!kbhit())
printf("You haven't pressed a key.\n");
return 0;
}
When I press a key, It not Stop, i don't think my code wrong, have any suggest for me to fix this?
And this is my Screen: http://i.imgur.com/8Rsj8J7.png
Update: I tested on some another computer, It work! (same code)
From kbhit()
Explanation: This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.
Related
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.
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
I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists.
I want to use clrscr() and getch() function.
Can you please tell me the substitute of this header file in linux.
The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:
http://linux.die.net/man/3/getch
http://linux.die.net/man/3/erase
system("clear"); can be used in linux instead of clrscr();
# include <curses.h>
int erase(void);
int werase(WINDOW *win);
int clear(void);
int wclear(WINDOW *win);
int clrtobot(void);
int wclrtobot(WINDOW *win);
int clrtoeol(void);
int wclrtoeol(WINDOW *win);
DESCRIPTION
The erase and werase routines copy blanks to every position in
the window, clearing the screen.
I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.
Similarly, just use system("exit"); within your code:
#include<stdlib.h>
main()
{
system("clear"); //clears the screen
}
Checking the man-pages shows:
SYSTEM(3) Linux Programmer's Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.
It could also be the case that this question is a possible duplicate of the following:
How to implement getch() function of C in Linux?
Why can't I find <conio.h> on Linux?
GNU/Linux replacements for Turbo C functions `clrscr` and `cprintf`
Function clrscr in C and C++
"UNDEFINED REFRENCE TO clrscr();"
Substitute for getch(), gotoxy(), delay(), clrscr()
What is Equivalent to getch() & getche() in Linux?
Finally, take a look at the following for more details and examples:
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html#GETCHCLASS
http://ubuntuforums.org/showthread.php?t=549023
Apparently you didn't try googling.
There are no direct alternatives.
This blog post: http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ provides you with alternatives for getch() and getche()
Alternatively you can use libncurses to do what you want: http://tech.dir.groups.yahoo.com/group/linux/message/29221
curses.h is an alternative for conio.h.
install build-essentials and install libncurses5-dev.
Then you can work with that functions.
[http://ubuntuforums.org/showthread.php?t=880601][1]
I was tinkering around with some codes; after i installed ncurses, I inserted these codes:
#include <stdio.h>
#include <ncurses.h>
main ()
{
system ("clear");
getchar ();
}
There is another way to do it through C code instead system call.
void clrscr(void) {
fprintf(stdout, "\033[2J\033[0;0f");
fflush(stdout);
}
I found it long time ago and I've checked it on raspbian successfully.
And also:
void gotoxy(int x, int y) {
printf("%c[%d;%df",0x1B, y, x);
}
I hope it helps you.
Regards.
In G++ Compiler, we use system("clear") function defined in stdlib.h header File
#include<iostream>
#include<stdlib.h>
int main() {
std::cout<<"Hello Aliens:";
system("clear");
}
This question already has answers here:
Function clrscr in C and C++
(5 answers)
Closed 9 years ago.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,a,b,c,choice;
do
{
printf("enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("pressing one add the two numbers\nenter one if you want to\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
c=a+b;
printf("the sum of the entered numbers%.1f\n\n:",c);
break;
}
default:
printf("you have entered an invalid");
break;
}
clrscr();
}
while(i==1);
getch();
}
i do not know cause my classmate is using turbo c and it's fine,for me i am using dev c++ but looks like clrscr(); is not known to the compiler help please.
Your classmate is programming under DOS, obviously you don't ... conio.h comes with Turbo C and DOS ... So, remove the lines
#include<conio.h>
and
clrscr();
and
getch();
to make your program compile ...
... and do not use %.1f to print an int.
... and main() must return int
* and do not copy from your classmate ... he seems to be stuck in the stone age*
From Wiki:
conio.h is a C header file used mostly by MS-DOS compilers to provide
console input/output.1 It is not part of the C standard library, ISO
C nor is it defined by POSIX
Member functions
kbhit - Determines if a keyboard key was pressed.
getch - Reads a character directly from the console without buffer, and without echo.
getche - Reads a character directly from the console without buffer, but with echo.
ungetch - Puts the character c back into the keyboard buffers.
cgets - Reads a string directly from the console.
cscanf - Reads formatted values directly from the console.
putch - Writes a character directly to the console.
cputs - Writes a string directly to the console.
cprintf - Formats values and writes them directly to the console.
clrscr - Clears the screen.
Compilers provided later than 1989 have prepended an _ to the names,
to comply with the requisites of the ANSI C Standard.
conio.h is not part of the C standard. It is a Borland extension, and works only with Borland compilers (and perhaps some other commercial compilers). Dev-C++ uses GCC, the GNU Compiler Collection, as it's compiler. GCC is originally a UNIX compiler, and aims for portability and standards-compliance.
You can use Borland functions this way in Dev C++:
Include conio.h to your source, and add C:\Dev-C++\Lib\conio.o to "Linker Options" in Project Options (where C:\Dev-C++ is where you installed Dev-C++).
I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists.
I want to use clrscr() and getch() function.
Can you please tell me the substitute of this header file in linux.
The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:
http://linux.die.net/man/3/getch
http://linux.die.net/man/3/erase
system("clear"); can be used in linux instead of clrscr();
# include <curses.h>
int erase(void);
int werase(WINDOW *win);
int clear(void);
int wclear(WINDOW *win);
int clrtobot(void);
int wclrtobot(WINDOW *win);
int clrtoeol(void);
int wclrtoeol(WINDOW *win);
DESCRIPTION
The erase and werase routines copy blanks to every position in
the window, clearing the screen.
I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.
Similarly, just use system("exit"); within your code:
#include<stdlib.h>
main()
{
system("clear"); //clears the screen
}
Checking the man-pages shows:
SYSTEM(3) Linux Programmer's Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.
It could also be the case that this question is a possible duplicate of the following:
How to implement getch() function of C in Linux?
Why can't I find <conio.h> on Linux?
GNU/Linux replacements for Turbo C functions `clrscr` and `cprintf`
Function clrscr in C and C++
"UNDEFINED REFRENCE TO clrscr();"
Substitute for getch(), gotoxy(), delay(), clrscr()
What is Equivalent to getch() & getche() in Linux?
Finally, take a look at the following for more details and examples:
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html#GETCHCLASS
http://ubuntuforums.org/showthread.php?t=549023
Apparently you didn't try googling.
There are no direct alternatives.
This blog post: http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ provides you with alternatives for getch() and getche()
Alternatively you can use libncurses to do what you want: http://tech.dir.groups.yahoo.com/group/linux/message/29221
curses.h is an alternative for conio.h.
install build-essentials and install libncurses5-dev.
Then you can work with that functions.
[http://ubuntuforums.org/showthread.php?t=880601][1]
I was tinkering around with some codes; after i installed ncurses, I inserted these codes:
#include <stdio.h>
#include <ncurses.h>
main ()
{
system ("clear");
getchar ();
}
There is another way to do it through C code instead system call.
void clrscr(void) {
fprintf(stdout, "\033[2J\033[0;0f");
fflush(stdout);
}
I found it long time ago and I've checked it on raspbian successfully.
And also:
void gotoxy(int x, int y) {
printf("%c[%d;%df",0x1B, y, x);
}
I hope it helps you.
Regards.
In G++ Compiler, we use system("clear") function defined in stdlib.h header File
#include<iostream>
#include<stdlib.h>
int main() {
std::cout<<"Hello Aliens:";
system("clear");
}