mardi 30 juin 2015

Web Browser Turbo C++

So I was browsing the Internet for a project and stumbled upon this piece of code. It runs perfectly until the point you enter a website(include "www" :P) and press Enter and then boom! The program terminates, no error message, nothing.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <dos.h>
#include <string.h>

void initialize_graphics_mode();
int get_key();
void draw();

union REGS i, o;

main()
{
  int key, i = 0, xpos, ypos, button;
  char arr[200], temp[5], *ptr;
  char a[] = "C:\\Progra~1\\Mozill~1\\firefox ";

  strcpy(arr,a);

  i = strlen(a);

  initialize_graphics_mode();

  draw();

  while(1)
  {
    if(kbhit())
      key = get_key();

    if((key>=97&&key<=122)||(key>=65&&key<=90)||key==46||key==47||key==63)
    {
      arr[i] = key;
      sprintf(temp,"%c",arr[i]);
      outtext(temp);
      if(getx()>470)
      {
    clearviewport();
    moveto(5,2);
      }
      i++;
    }
    else if ( key == 13 )
    {
      arr[i] = '\0';
      system(arr);
      break;
    }
    else if ( key == 27 )
    {
      closegraph();
      exit(EXIT_SUCCESS);
    }
    if(button==1&&xpos>=150&&xpos<=480&&ypos>=300&&ypos<=330)
    {
      system("C:\\Progra~1\\Mozill~1\\firefox programmingsimplified.com");
      break;
    }
    key = -1;
  }

  closegraph();
  return 0;
}

void initialize_graphics_mode()
{
  int gd = DETECT, gm, errorcode;

  initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
  errorcode = graphresult();

  if( errorcode != grOk )
  {
    printf("Graphics error : %s\n",grapherrormsg(errorcode));

    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_FAILURE);
  }
}

int get_key()
{
  i.h.ah = 0;
  int86(22,&i,&o);

  return( o.h.al );
}

void draw()
{
  settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
  outtextxy(275,11,"Web Browser");
  outtextxy(155,451,"<a> href=http://ift.tt/18KHKlF"">http://ift.tt/1g8cqCe;");
  outtextxy(5,105,"Enter URL : ");
  rectangle(120,100,600,130);
  setviewport(121,101,599,129,1);
  moveto(5,1);
}

Could someone help me in understanding why this happens? and possibly a fix for this?

NOTE: + It is supposed to work if you have Mozilla Firefox. If you have any other browser, please edit the code accordingly.

  • I am running this on Turbo C++, what would I have to do in order to convert this code for Dev-C++?

Thanks in advance.

P.S: I'm new to C++ so elaborate as much as possible.




Aucun commentaire:

Enregistrer un commentaire