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.
Cœur
36.9k25 gold badges193 silver badges262 bronze badges
asked Jun 21, 2017 at 2:33
3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#include<graphics.h> #include<math.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h> #include<iostream> using namespace std; class ABC_fig { protected: int x0,y0,raz,color; public: ABC_fig() { cout <<"Created ABC_fig"; } virtual ~ABC_fig() { cout <<" Destructor_ABC_Fig"<<endl; } virtual void SetParam(int,int,int)=0; virtual void Draw(int)=0; }; class Square: public ABC_fig { protected: int x0,y0,raz,i,color; public: Square():ABC_fig () { cout<<"Creat Sguare" << endl; }; ~ Square() {cout<<"Destructor Square";} void SetParam(int,int,int); void Draw(int); }; class Circle: public ABC_fig { protected: int i; public: Circle(): ABC_fig () { cout<<"Creat Circle" << endl;}; ~ Circle() {cout<<"Destructor circle";} void SetParam(int,int,int); void Draw(int); }; void Square::SetParam (int x0,int y0,int raz) { this ->x0=x0; this ->y0=y0; this ->raz=raz; } void Circle::SetParam (int x0,int y0,int raz) { this ->x0=x0; this ->y0=y0; this ->raz=raz; } void Circle::Draw (int color) { setcolor(color); circle(x0,y0,raz); } void Square::Draw (int color) { setcolor(color); rectangle(x0-raz,y0-raz,x0+raz,y0+raz); } /////////////////////////////////////////////////////// int main() { int color,x,y,x1,y1,r; Square kv[3]; Circle cir[3]; ABC_fig *f[6]; getch(); int grdriver,gmode,errorcode; detectgraph(&grdriver,&gmode); initgraph(&grdriver,&gmode,"d:\bc\bgi"); errorcode=graphresult(); if(errorcode!=grOk) { grapherrormsg(errorcode); exit(1);} f[0]=&kv[0]; f[1]=&kv[1]; f[2]=&kv[2]; f[3]=&cir[0]; f[4]=&cir[1]; f[5]=&cir[2]; rand(); while (!kbhit()) { x1=320; y1=240; for (int z=0; z<6; z++) { color=rand(); x=x1+rand(); y=y1+rand(); r=rand(); { f[z]->SetParam(x,y,r); f[z]-> Draw(rand()); } } delay(400); cleardevice(); } closegraph(); system("pause"); } |
Перейти к контенту
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#include<graphics.h> #include<math.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h> #include<iostream> using namespace std; class ABC_fig { protected: int x0,y0,raz,color; public: ABC_fig() { cout <<"Created ABC_fig"; } virtual ~ABC_fig() { cout <<" Destructor_ABC_Fig"<<endl; } virtual void SetParam(int,int,int)=0; virtual void Draw(int)=0; }; class Square: public ABC_fig { protected: int x0,y0,raz,i,color; public: Square():ABC_fig () { cout<<"Creat Sguare" << endl; }; ~ Square() {cout<<"Destructor Square";} void SetParam(int,int,int); void Draw(int); }; class Circle: public ABC_fig { protected: int i; public: Circle(): ABC_fig () { cout<<"Creat Circle" << endl;}; ~ Circle() {cout<<"Destructor circle";} void SetParam(int,int,int); void Draw(int); }; void Square::SetParam (int x0,int y0,int raz) { this ->x0=x0; this ->y0=y0; this ->raz=raz; } void Circle::SetParam (int x0,int y0,int raz) { this ->x0=x0; this ->y0=y0; this ->raz=raz; } void Circle::Draw (int color) { setcolor(color); circle(x0,y0,raz); } void Square::Draw (int color) { setcolor(color); rectangle(x0-raz,y0-raz,x0+raz,y0+raz); } /////////////////////////////////////////////////////// int main() { int color,x,y,x1,y1,r; Square kv[3]; Circle cir[3]; ABC_fig *f[6]; getch(); int grdriver,gmode,errorcode; detectgraph(&grdriver,&gmode); initgraph(&grdriver,&gmode,"d:bcbgi"); errorcode=graphresult(); if(errorcode!=grOk) { grapherrormsg(errorcode); exit(1);} f[0]=&kv[0]; f[1]=&kv[1]; f[2]=&kv[2]; f[3]=&cir[0]; f[4]=&cir[1]; f[5]=&cir[2]; rand(); while (!kbhit()) { x1=320; y1=240; for (int z=0; z<6; z++) { color=rand(); x=x1+rand(); y=y1+rand(); r=rand(); { f[z]->SetParam(x,y,r); f[z]-> Draw(rand()); } } delay(400); cleardevice(); } closegraph(); system("pause"); } |
Почему я получаю ошибку во время выполнения
Вопрос:
Здравствуйте, я использую Code:: Blocks в Windows XP.
Когда я запускаю эту программу, я получаю ошибку времени выполнения как "drawing operation was attempted when there was no current window".
Я хотел бы знать, почему это происходит.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <windows.h>
#include <conio.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %dn",iret1);
printf("Thread 2 returns: %dn",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
char hello;
for(;;)
{
message = (char *) ptr;
printf("%s n", message);
Sleep(1000);
// break;
fflush(stdin);
/*drawing operation was attempted when there was no current window*/
//The happens from next line onwords
if(kbhit())
{
hello = getchar();
printf("The interrupt %d", hello);
}
}
}
Лучший ответ:
kbhit() устарел, используйте _kbhit() вместо этого. Возможно, это была причина.
Ответ №1
Ваша программа имеет Undefined Поведение.
Вызов fflush() on stdin не разрешен и является Undefined Поведением. Разрешается только вызывать стандартный поток вывода stdout.
Это может быть или не быть прямой причиной поведения, которое вы наблюдаете, но поскольку это Undefined Поведение, которое вы никогда не знаете…
C99 Стандарт 7.19.5.2/2:
Если поток указывает на выходной поток или поток обновлений, в котором самая последняя операция не была введена, функция fflush заставляет любые неписаные данные для этого потока доставляться в среду хоста, которая должна быть записана в файл; в противном случае поведение undefined
Can’t be able to use «graphics.h» in Code Block ?
I’m trying to use «graphics.h» just for learning purpose. I’m trying to draw a line but «Code Block 13.12» gives this error
«drawing operation was attempted when there was no current window»
http://postimg.org/image/hselc60ux/
Code I’m using:
#include<graphics.h>
int main()
{
line(0,0,640,480);
return 0;
}
As Code Block by default doesn’t have «graphics.h». So, I followed One tutorial tom setup graphics.h in Code Block which has the following steps.
1. Download «WinBGIm_Library6_0_Nov2005.rar» from http://winbgim.codecutter.org/
2. After extraction put «graphics.h» & «winbgim.h» in «C:Program FilesCodeBlocksMinGWinclude» folder
3. «libbgi.a» in «C:Program FilesCodeBlocksMinGWlib» folder
4. Setting -> Compiler -> Global compiler setting
5.under «Link Libraries» section add
«C:Program FilesCodeBlocksMinGWinclude»
6. under «Other linker options» section add
-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
7. Open «graphics.h» from «C:Program FilesCodeBlocksMinGWinclude» in notepad and jump to line 302 and edit it to
int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,
Please tell whats the problem
You didn’t initialise your window.
As Code Block by default doesn’t have «graphics.h».
And it is logical because graphics.h is non-standard external library.
you could have just put the file in the same folder as the .cbp file and in the main.cpp file said #include "graphics.h" .
Last edited on
@Duoas, Upon running this code
#include <graphics.h>
int main()
{
/* initialize graphics window at 400 x 300 */
initwindow(400, 300);
/* draw a line */
line(0, 0, 100, 100);
/* clean up */
getch();
closegraph();
return 0;
}
I’m getting this new error message
«Test.exe» has stopped working and
http://postimg.org/image/yh2utnypr/
I’ve installed WinBGIm myself and see the same problem — meaning something is wrong with the library. If I get time I’ll figure it out for you, but you might ask your teacher. It is unlikely you are the only student with the issue.
Alright.. I just compiled from source. Chances are there is a mismatch with the GCC version used to create the binary you got from the WinBGIm site…
Give me a few minutes and I’ll post the new binary for you (and your classmates) to grab.
Last edited on
Alright, you can grab the new WinBGIm here:
http://home.comcast.net/~michaelthomasgreer/temp/WinBGIm_GCC47.zip
Unzip and you’ll find a new
libbgi.a
to replace the one you have now.
I’ll keep this up for a while (at least a month), but not forever. (Otherwise the interwebs will start a whole bunch of people grabbing it all the time and Comcast will want to charge me more money.)
Hope this helps.
Kudos @Duoas
Thanx a lot, It worked. I’ve dwnloaded it. You can remove it now.
Well, let your classmates get it too… They’re probably having the same problem you are.
@Duos Oh no no not my classmates. Actually I’ve learned c++ way back 3-4 years. But didn’t learn this part. There is one old project that I have, So I was thinking to improve its user interface with little bit of graphics.
Ah, I missed that in your first post. :-/
Well, glad to have helped.
:O)
Topic archived. No new replies allowed.
Can’t be able to use «graphics.h» in Code Block ?
I’m trying to use «graphics.h» just for learning purpose. I’m trying to draw a line but «Code Block 13.12» gives this error
«drawing operation was attempted when there was no current window»
http://postimg.org/image/hselc60ux/
Code I’m using:
#include<graphics.h>
int main()
{
line(0,0,640,480);
return 0;
}
As Code Block by default doesn’t have «graphics.h». So, I followed One tutorial tom setup graphics.h in Code Block which has the following steps.
1. Download «WinBGIm_Library6_0_Nov2005.rar» from http://winbgim.codecutter.org/
2. After extraction put «graphics.h» & «winbgim.h» in «C:Program FilesCodeBlocksMinGWinclude» folder
3. «libbgi.a» in «C:Program FilesCodeBlocksMinGWlib» folder
4. Setting -> Compiler -> Global compiler setting
5.under «Link Libraries» section add
«C:Program FilesCodeBlocksMinGWinclude»
6. under «Other linker options» section add
-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
7. Open «graphics.h» from «C:Program FilesCodeBlocksMinGWinclude» in notepad and jump to line 302 and edit it to
int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,
Please tell whats the problem
You didn’t initialise your window.
As Code Block by default doesn’t have «graphics.h».
And it is logical because graphics.h is non-standard external library.
you could have just put the file in the same folder as the .cbp file and in the main.cpp file said #include "graphics.h" .
Last edited on
@Duoas, Upon running this code
#include <graphics.h>
int main()
{
/* initialize graphics window at 400 x 300 */
initwindow(400, 300);
/* draw a line */
line(0, 0, 100, 100);
/* clean up */
getch();
closegraph();
return 0;
}
I’m getting this new error message
«Test.exe» has stopped working and
http://postimg.org/image/yh2utnypr/
I’ve installed WinBGIm myself and see the same problem — meaning something is wrong with the library. If I get time I’ll figure it out for you, but you might ask your teacher. It is unlikely you are the only student with the issue.
Alright.. I just compiled from source. Chances are there is a mismatch with the GCC version used to create the binary you got from the WinBGIm site…
Give me a few minutes and I’ll post the new binary for you (and your classmates) to grab.
Last edited on
Alright, you can grab the new WinBGIm here:
http://home.comcast.net/~michaelthomasgreer/temp/WinBGIm_GCC47.zip
Unzip and you’ll find a new
libbgi.a
to replace the one you have now.
I’ll keep this up for a while (at least a month), but not forever. (Otherwise the interwebs will start a whole bunch of people grabbing it all the time and Comcast will want to charge me more money.)
Hope this helps.
Kudos @Duoas
Thanx a lot, It worked. I’ve dwnloaded it. You can remove it now.
Well, let your classmates get it too… They’re probably having the same problem you are.
@Duos Oh no no not my classmates. Actually I’ve learned c++ way back 3-4 years. But didn’t learn this part. There is one old project that I have, So I was thinking to improve its user interface with little bit of graphics.
Ah, I missed that in your first post. :-/
Well, glad to have helped.
:O)
Topic archived. No new replies allowed.
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <ctime>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void ship (int x, int y, int color1)
{
setcolor(7);
line(x+20,y+70,x+20,y+90);
line(x+20,y+90,x+90,y+90);
line(x+90,y+90,x+120,y+70);
line(x+20,y+70,x+120,y+70);
line(x+60,y+70,x+60,y+20);
line(x+30,y+30,x+90,y+30);
line(x+30,y+30,x+30,y+60);
line(x+30,y+60,x+90,y+60);
line(x+90,y+30,x+90,y+60);
setfillstyle (1,4);
bar(x+30,y+30,x+90,y+60);
setfillstyle(1,7);
bar(x+20,y+70,x+90,y+90);
setfillstyle(1,7);
setfillstyle(1,7);
floodfill(x+100,y+75,7);
line(x+90,y+70,x+120,y+70);
line(x+90,y+90,x+120,y+70);
line(x+90,y+70,x+90,y+90);
_getch();
closegraph();
}
void num()
{
int x, y,dor;
initwindow(800, 800);
setfillstyle(1, 11);
bar(0, 0, 799, 799);
x = 400; y = 400;
while (1)
{
ship ( x, y, 7);
int dor = getch();
if ( _getch()==27 ) break;
ship(x,y,11);
switch ( dor )
{
case 100: x --; break;
case 102: x ++; break;
case 104: y --; break;
case 98: y ++;
}
}
_getch();
closegraph();
}
int main()
{
setlocale(LC_ALL, "Russian");
cout << "Ñâîáîäíîå äâèæåíèå îáúåêòà - ââåäèòå 1" << endl;
cout << "Äâèæåíèå îáúåêòà ñ ïîìîùüþ êëàâèàòóðû - ââåäèòå 2" << endl;
cout << "Âûõîä èç ïðîãðàììû - ââåäèòå 3" << endl;
int c;
cin >> c;
switch (c)
{
case 1:
move(c);
break;
case 2:
num();
break;
default:
exit(0);
}
}
