I can't log out the output C program - c

I am developping a game with allegro library. I wrote a starter C code then I compile the program on Ubuntu Terminal.After that When I open the output program,the program open but not closed.Cancel Button(X) is not working.
Here is my starter code:
#include <allegro.h>
void Baslat();
void Bitir();
int main(){
Baslat();
while(!key[KEY_ESC]){
}
Bitir();
return 0;
}
END_OF_MAIN()
void Baslat(){
int depth,res;
allegro_init();
depth = desktop_color_depth();
if(depth == 0) depth=32;
set_color_depth(depth);
res=set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0);
if(res!=0){
allegro_message("%s\n", allegro_error); exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
}
void Bitir(){
clear_keybuf();
}

To respond to a user pressing the X button in allegro5, you must catch ALLEGRO_EVENT_DISPLAY_CLOSE. This tutorial provides an example of responding to that event.

Related

MathGL library using pthread in C crashes

I have directly tried to convert C++ example of mathGL Into C code here:
#include <mgl2/mgl_cf.h>
#include <mgl2/wnd_cf.h>
#include <mgl2/fltk.h>
#include <pthread.h>
#include <synchapi.h>
//mglFLTK *gr=NULL; // pointer to window
HMGL gr = NULL;//HMGL mgl_create_graph_fltk
void *calc(void *) // function with calculations
{
// mglPoint pnt; // some data for plot
// HMDT dat;
// dat= mgl_create_data () ;
for(long i=0;;i++) // do calculation
{
// long_calculations(); // which can be very long
// Sleep(200);
for(uint64_t i=0;i<200000;i++);
// pnt.Set(2*mgl_rnd()-1,2*mgl_rnd()-1);
if(gr)
{
// gr->Clf(); // make new drawing
mgl_clf ( gr) ;
// draw something
// gr->Line(mglPoint(),pnt,"Ar2");
mgl_line(gr,0,0,0,2*mgl_rnd()-1,2*mgl_rnd()-1,0,"Ar2",2);
char str[16]; snprintf(str,15,"i=%ld",i);
// gr->Puts(mglPoint(),str);
mgl_puts(gr,0,0,0,str,":C",-.7);
// don’t forgot to update window
// gr->Update();
mgl_wnd_update(gr);
}
}
}
But it doesn't work. I've added printf before mgl_wnd_update to printing i in console and it only prints one time: i=0. I'm using windows 10 and installed minGW GCC and mathgl by MSYS2 through eclipse IDE. Though I've created the not threaded one here:
int main(int argc,char **argv)
{
gr = mgl_create_graph_fltk(NULL, "First C graph", NULL, NULL);
mgl_fltk_thr ();
while(1)
{
Sleep(100);
mgl_clf ( gr) ;
// draw something
// gr->Line(mglPoint(),pnt,"Ar2");
mgl_line(gr,0,0,0,2*mgl_rnd()-1,2*mgl_rnd()-1,0,"Ar2",-.5);
char str1[16]; snprintf(str1,15,"i=%ld",1);
// gr->Puts(mglPoint(),str);
mgl_puts(gr,0,0,0,str1,":C",-.5);
// don’t forgot to update window
// gr->Update();
mgl_wnd_update(gr);
}
return 0;
}
And this is works. What is wrong with that pthreaded code? It seems that the thread loop only works once. But this loops works well on none-threade program!!

how to change the program to do the same task without the program crashing?

I have an assignment to produce a program that will compare students answer to the answer key, and display the incorrect answers. The program then produces a report of the students incorrect answers and his final grade. The Program must use arrays and functions.
Currently I am trying to code two functions one to read the students answer file and store it in an array and the other to read answer key file and store it in another array. Then the functions will return both arrays to the main function later to be sent to another function to compare their contents(not yet done).
My problem with this code after pressing F11 to compile and run, i get a blank execution screen and a notification saying that the program has stopped working.
If my code contains a mistake or my approach is incorrect please tell me how to fix it.
note: this is my first semester learning C programming.
Thank you.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
//Modules
char* readstudent()
{
FILE*s_ans;
int i,j;
static char arrs[20];
s_ans=fopen("trial2.txt","r");
if (s_ans == NULL)//check if file can be opened
{
printf("error student");
}
while(!feof(s_ans))
{
for(j=0;j<20;j++)
{
fscanf(s_ans,"%s",arrs[j]);
}
}
printf("ReadStudent\n");
for(i=0;i<20;i++)
{
printf("%d\t %s\n",i+1,arrs[i]);
}
return arrs;
}
char* readcorrect()
{
FILE*c_ans;
int x,i;
static char arrc[20];
c_ans=fopen("CorrectAnswers.txt","r");
if (c_ans == NULL)//check if file can be opened
{
printf("error correct");
}
while(!feof(c_ans))
{
for(x=0;x<20;x++)
{
fscanf(c_ans,"%s",arrc[x]);
}
}
printf("ReadCorrect\n");
for(i=0;i<20;i++)
{
printf("%d\t %s\n",i+1,arrc[i]);
}
return arrc;
}
//Main
int main()
{
int i,j,n,x;
char* as_ans=readstudent();
char* ac_ans=readcorrect();
printf("Main");
for(i=0;i<20;i++)
{
printf("%s",as_ans[i]);
}
return 0;
}

IAM getting stuck with SIGSEGV runtime error in codechef when i submit my code

This is my code
#include<stdio.h>
long int a[100001],b[100001];
int main()
{
long int n=0,m=0,bp=0,bn=0,i=0,x=0,Result=0;//declaration
char ch='\0';
FILE *fp;
scanf("%ld %ld",&n,&m);
fp=fopen("array.txt","w");//file reading
for(i=0;i<=n;i++)
{
ch=getchar();
fputc(ch,fp);
}
fclose(fp);
fp=fopen("array.txt","r");//file scaning to array
for(i=0;i<n;i++)
{
fscanf(fp,"%1d",&a[i]);
}
fclose(fp);
while(m>0)
{
scanf("%ld",&x);
bp=0;
bn=0;
for(i=0;i<x-1;i++)
{
b[i]=a[x-1]-a[i];
if(b[i]>0)
bp = bp+b[i];
else
bn = bn+b[i];
}
Result=bp-bn;
printf("%ld\n",Result);
m--;
}
return (0);
}
when i run this code in my system it is giving correct output but in codechef it is showing SIGSEGV runtime error... please help me to write a efficient program
the linkto problem is http://www.codechef.com/APRIL14/problems/ADIGIT
You are not allowed to open file for reading/writing on Codechef. That is why you are getting SIGSEGV.
I don't understand the need to do file I/O for the problem.
Also, you should delete the code, because this is a problem from running contest.

C compile error: Id returned 1 exit status

For some reason, when I try compiling a program, the compiler says permission denied and Id returned 1 exit status. Could anyone tell me what that means? Thank you
#include <stdio.h> /* Library inclusions */
#include "genlib.h"
#include "simpio.h"
int binSearch(int val, int numbers[], int size1); /* prototypes */
void sortArray (int numbers[], int size1);
int indexMax (int numbers[], int low, int high);
void swap (int numbers[], int loc, int loc1);
void getArray (int numbers[], int size1);
void displayArray (int numbers[], int size1);
main()
{
int value, size1;
printf("Enter the number of elements: ");
size1=GetInteger();
int numbers[size1];
getArray(numbers, size1);
sortArray(numbers, size1);
displayArray(numbers, size1);
printf("\nEnter value to find: ");
value=GetInteger();
binSearch(value, numbers, size1);
getchar();
}
void sortArray (int numbers[], int size1) /*Function sortArray*/
{
int i , maxInd;
for (i= size1-1; i>=0;i--)
{
maxInd=indexMax(numbers, 0, i);
swap (numbers, i, maxInd);
}
}
void displayArray (int numbers[], int size1) /*Function displayArray*/
{
int i;
printf("This is the sorted set of numbers: \n");
for (i=0; i< size1; i++)
{
printf ("%d\t", numbers[i]);
}
}
void getArray (int numbers[], int size1) /*Function getArray*/
{
int i;
for (i=0; i<size1; i++)
{
printf ("Enter the values of the %d elements: ", size1);
numbers[i]=GetInteger();
}
}
int indexMax (int numbers[], int low, int high) /*Function indexMax*/
{
int i, maxInd;
maxInd=high;
for (i=low;i<=high;i++)
{
if (numbers[i]>numbers[maxInd])
{
maxInd =i;
}
}
return (maxInd);
}
void swap (int numbers[], int loc, int loc1) /*Function swap*/
{
int temp;
temp=numbers[loc];
numbers[loc]=numbers[loc1];
numbers[loc1]=temp;
}
int binSearch(int val, int numbers[], int size1) /*Function binSearch*/
{
int low, high, mid;
low=0;
high=size1-1;
while(low<=high)
{
mid=(low+high)/2;
if(val<numbers[mid])
{
high=mid-1;
}
else if(val>numbers[mid])
{
low=mid+1;
}
else if(val==numbers[mid])
{
printf("Your number is in location %d\n", mid+1);break;
}
else
{
printf("Your value is not in the array.");
}
}
}
The above is the binary search algorithm code I tried to compile.
I may guess, the old instance of your program is still running. Windows does not allow to change the files which are currently "in use" and your linker cannot write the new .exe on the top of the running one. Try stopping/killing your program.
I bet for sure, that this is because you didn't close the running instance of the program before trying to re-compile it.
Generally, ld.exe returns 1 when it can't access required files. This usually includes
Can't find the object file to be linked (or Access denied)
Can't find one or more symbols to link
Can't open the executable for writing (or AD)
The program looks completely fine, so the second point should not hit. In usual cases, it's impossible for ld to fail to open the object file (unless you have a faulty drive and a dirty filesystem), so the first point is also nearly impossible.
Now we get to the third point. Note that Windows not allow writing to a file when it's in use, so the running instance of your program prevents ld.exe from writing the new linked program to it.
So next time be sure to close running programs before compiling.
Using code::blocks , I have solved this error by doing :
workspace properties > build target > build target files
and checking every project file.
You may compiling your program while another program may be running in background.
Firstly, see if another program is running .Close it and then try ro compile.
Just try " gcc filename.c -lm" while compiling the program ...it worked for me
1d returned 1 exit status error
First of all you have to create a project by clicking file new and then project and give project name select the language c or c++ and select empty also.
Then your program is under that project... And then give a program name save it.... Ensure that your under some project to compile and run a program...
Is a simple MAYUS word. verify the log.
it could be that you just said main{....I use int main{ when I start my main.
This answer is written for C++ developers, because I was haunted by such problem as one. Here is the solution:
Instead of
main()
{
}
please type
int main()
{
}
so the main function can be executed.
By the way,
if you compile a C/C++ source file with no main function to execute,
there will definitely be a bug message saying:
"[Error] Id returned 1 exist status"
But sometimes we just don't need main function in the file,
in such a case, just ignore the bug message.
My solution is to try to open another file that you can successfully run that you do at another PC, open that file and run it, after that copy that file and make a new file. Try to run it.
it seems as if it comes when u have an previous compiled version of your program running

Linker can't find initgraph() and closegraph()?

The linker gives error that overflow at initgraph and close graph
#include<dos.h>
#define DETECT 0
union REGS in,out;
void detectmouse()//no declaration(prototype)?
{
in.x.ax=0;
int86(0x33,&in,&out);
if(out.x.ax==0)
{
printf("Fail to initialize the mouse.");
}
else
{
printf("Mouse succesfully initialized.");
}
getch();
}
void showmousegraphics()//show mouse in graphics mode
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"c:\\tc\\bgi");
in.x.ax=1;
int86(0X33,&in,&out);
getch();
closegraph();
}
void main()
{
detectmouse();
showmousegraphics();
}
Ahh, you're probably using some old compiler for DOS. In that case, you have to choose large memory model when compiling, so that you can have more than 64kB of code.

Resources