Click here to Skip to main content
15,867,949 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Script Writing in AutoHotKey Pin
Richard MacCutchan19-Jan-18 4:00
mveRichard MacCutchan19-Jan-18 4:00 
GeneralRe: Script Writing in AutoHotKey Pin
ThatOldGuy19-Jan-18 4:12
ThatOldGuy19-Jan-18 4:12 
GeneralRe: Script Writing in AutoHotKey Pin
Rick York19-Jan-18 5:08
mveRick York19-Jan-18 5:08 
GeneralRe: Script Writing in AutoHotKey Pin
Richard MacCutchan19-Jan-18 5:29
mveRichard MacCutchan19-Jan-18 5:29 
GeneralRe: Script Writing in AutoHotKey Pin
ThatOldGuy19-Jan-18 7:12
ThatOldGuy19-Jan-18 7:12 
GeneralRe: Script Writing in AutoHotKey Pin
Richard MacCutchan19-Jan-18 8:06
mveRichard MacCutchan19-Jan-18 8:06 
GeneralRe: Script Writing in AutoHotKey Pin
ThatOldGuy19-Jan-18 8:49
ThatOldGuy19-Jan-18 8:49 
QuestionProblem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 0:24
Member 1363226319-Jan-18 0:24 
Hello,

I am trying to make a program in C with multiple bouncing balls. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library.

Thank you in advamce.

Here is my code:

C++
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
    FILE *input;
    int n, *num, i, total, j;
    if((input=fopen("data.txt", "r"))!=NULL)
    {
        fscanf(input, "%d", &n);
        total=n*7;
        num=(int *) malloc(total*sizeof(int));
        if(num!=NULL)
        {
            for(i=0; i<total; i++)
           {
               fscanf(input, "%d", &num[i]);
           }
           fclose(input);

           clock_t start,finish, previous;
           double step;
           int gdriver = DETECT, gmode, errorcode;
           initgraph(&gdriver, &gmode, "");
           errorcode = graphresult();
           if (errorcode != grOk)
           {
               printf("Graphics error: %s\n", grapherrormsg(errorcode));
               system ("pause");
               exit(1);
           }
           start=clock();
           previous=start;
           do
           {
               j=0;
               for(i=0; i<=n; i++)
               {
                   finish = clock();
                   step = (finish-previous)*1.0/CLOCKS_PER_SEC;
                   if (step >= 0.03)
                   {
                       previous = finish;
                       setfillstyle(SOLID_FILL,BLACK);
                       setcolor(BLACK);
                       fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                       num[j]+= num[j+5]*step;
                       num[j+1]+= num[j+6]*step;
                       if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
                       {
                           num[j+5] *= -1;
                       }
                       if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
                       {
                           num[j+6] *= -1;
                       }
                       setfillstyle(SOLID_FILL,RED);
                       setcolor(RED);
                       fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                       j=j+7;
                   }
               }
           }
           while (!kbhit());
           closegraph();
        }
        else
        {
            printf("Could not allocate memory\n\n");
        }
    }
    else
    {
        printf("Could not open file\n\n");
    }


    system("pause");
    return 0;
}

AnswerRe: Problem with multiple bouncing balls program in C Pin
Richard MacCutchan19-Jan-18 0:36
mveRichard MacCutchan19-Jan-18 0:36 
AnswerRe: Problem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 0:59
Member 1363226319-Jan-18 0:59 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Richard MacCutchan19-Jan-18 1:05
mveRichard MacCutchan19-Jan-18 1:05 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 1:22
Member 1363226319-Jan-18 1:22 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Richard MacCutchan19-Jan-18 1:23
mveRichard MacCutchan19-Jan-18 1:23 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 1:54
Member 1363226319-Jan-18 1:54 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Richard MacCutchan19-Jan-18 2:51
mveRichard MacCutchan19-Jan-18 2:51 
AnswerRe: Problem with multiple bouncing balls program in C Pin
Jochen Arndt19-Jan-18 1:07
professionalJochen Arndt19-Jan-18 1:07 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 1:53
Member 1363226319-Jan-18 1:53 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Jochen Arndt19-Jan-18 2:21
professionalJochen Arndt19-Jan-18 2:21 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Member 1363226319-Jan-18 2:30
Member 1363226319-Jan-18 2:30 
GeneralRe: Problem with multiple bouncing balls program in C Pin
Jochen Arndt19-Jan-18 2:56
professionalJochen Arndt19-Jan-18 2:56 
AnswerRe: Problem with multiple bouncing balls program in C Pin
Victor Nijegorodov19-Jan-18 21:21
Victor Nijegorodov19-Jan-18 21:21 
AnswerRe: Problem with multiple bouncing balls program in C Pin
Rick York24-Jan-18 7:17
mveRick York24-Jan-18 7:17 
Questionconvert feet and inches to centimeters Pin
nithiin_sai18-Jan-18 14:42
nithiin_sai18-Jan-18 14:42 
SuggestionRe: convert feet and inches to centimeters Pin
Jim Meadors18-Jan-18 15:49
Jim Meadors18-Jan-18 15:49 
AnswerRe: convert feet and inches to centimeters Pin
Victor Nijegorodov18-Jan-18 20:38
Victor Nijegorodov18-Jan-18 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.