simple steps to run a simple c++ program

Asked by hadi

I installed ubuntu 7.10 and now I want simple steps to compile and run a simple program like this:

#include <iostream.h>

int main()
{
int a;
cin >> a ;

}

it seems there is no any c++ editor in ubuntu by default

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
hadi
Solved:
Last query:
Last reply:
Revision history for this message
oeolartep (oeolartep) said :
#1

Hi, first of all install g++, type in a terminal,

sudo apt-get install g++

then write your code in your favorite editor, in this case you can use gedit, in a terminal type,

gedit example.cpp

copy the lines you post above and save it.

Finally, compile it

g++ -o example example.cpp

and run it,

./example

Revision history for this message
Vojtěch Trefný (vojtech.trefny) said :
#2

You can you use some IDE (for example Anjuta, not installed by default but it is in repository) or GNU C++ Compiler (g++).

Revision history for this message
hadi (modarres-zadeh-gmail) said :
#3

ok thanks for your aid

at this time i have g++ and anjuta installed on my system.

Now i have another question

when i write my program in a editor like BorlandC in windows seems to be different from the codes in an standard compiler like g++
because when i going to compile a code written and worked in BorlandC the program encounters dozens of errors
now what can i do ?

Revision history for this message
Vojtěch Trefný (vojtech.trefny) said :
#4

Maybe you are using some library which is not supported/presented in Ubuntu...

Revision history for this message
hadi (modarres-zadeh-gmail) said :
#5

how can i add other libraries to the ubuntu?

Revision history for this message
smuschel (simon-smuschel) said :
#6

Hi,
could you post the error messages from the compiler to see what's going wrong?

Revision history for this message
hadi (modarres-zadeh-gmail) said :
#7

this is my simple snake program :
================================================================================
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>

unsigned short int far *screen=(unsigned short int far *)0xb8000000;
const int height=80, width=80, maxLength=50; // these would be constant during the execution
int xHead=40, yHead=25, snake[maxLength];
void initialize( int direction, int food);
void setFood(int food);
void setGround(void);
void setSnake(int direction);
unsigned int getPoint(int xScreen, int yScreen);
int isFunctionKey(char &order);
void move(int length, int direction);
void drawHead(void);
void changeHead(int order);
void findTail(int xHead, int yHead, int length, int &xTail, int &yTail);
void run(int &order, int &endOfGame);
int o ;
int a=1;

//===MAIN==============================================================
void main(void)
{
int endOfGame=0, direction=1, length=3;
 char food='1', order='M';
 randomize();
 initialize(direction, food);

 while(!endOfGame)
 { /*/ my delay function
  int q,g=1,e,w;
  for (q=0;q<27;q++)
   for (w=0;w<30000;w++)
    for (e=1;e<20;e++)
     g=g*e;
  //====================
  */
  delay(200);
  switch (direction)
  { case 1 :xHead++; break;
   case 2 :yHead++; break;
   case 3 :yHead--; break;
   case 4 :xHead--; break;
  }
  switch (screen[yHead*80+xHead])
  { case 20*256+49:
    length = length+1;
    a=a+1;
    setFood(a);
   break;
   case 20*256+50:
    length = length+2;
    a=a+1;
    setFood(a);
   break;
   case 20*256+51 :
    length = length+3;
    a=a+1;
    setFood(a);
   break;
   case 20*256+52 :
    length = length+4;
    a=a+1;
    setFood(a);
   break;
   case 20*256+53 :
    length=length+5;
    a=a+1;
    setFood(a);
   break;
   case 20*256+54 :
    length = length+6;
    a=a+1;
    setFood(a);
   break;
   case 20*256+55:
    length = length+7;
    a=a+1;
    setFood(a);
   break;
   case 2*256:
    endOfGame=1;
   break;
  }

  move(length,direction);
  if(kbhit())
  { int order=getch();
   switch (order )
   {
    case 77 : direction=1 ; break;
    case 75 : direction=4 ; break;
    case 80 : direction=2 ; break;
    case 72 : direction=3 ; break;

    }

   run(order, endOfGame);
  }
 }
 getchar();
}

//===INITIALIZE=========================================================
void initialize( int direction, int food)
{ clrscr();
 setGround();
 setSnake(direction);
 screen[25*80+41]=2*256 ;
 screen[25*80+42]=2*256;
 screen[25*80+40]=2*256;
 snake[1]=25*80+40;
 snake[2]=25*80+41;
 snake[3]=25*80+42;
 setFood(a);
// drawCommands();

}
//===SETGROUND==========================================================
void setGround(void)
{int H;
 for (H=1;H<4000 ;H++)
  screen[H]=119*256;// white

}
//===SETSNAKE===========================================================
void setSnake(int direction)
{ xHead=42;
 yHead=25;
 screen[yHead*80+xHead]=2*256;
 // for(int i=1; i<3; i++)
// snake[i]=1; //right
  return;
}
//===SETFOOD============================================================
void setFood(int food)
{ int i=rand()%78;
 int j=rand()%49;
 switch (food)
 {
 case 1 : screen[j*80+i]=20*256+49;
 break ;
 case 2 : screen[j*80+i]=20*256+50;
 break;
 case 3 : screen[j*80+i]=20*256+51;
 break;
 case 4 : screen[j*80+i]=20*256+52;
 break ;
 case 5 : screen[j*80+i]=20*256+53;
 break ;
 case 6 : screen[j*80+i]=20*256+54;
 break;
 case 7 : screen[j*80+i]=20*256+55;
 break ;
 case 8 : screen[j*80+i]=20*256+56;
 break ;
 }
 return;
}
//===DRAWCOMMANDS=======================================================
//===MOVE===============================================================
void move(int length, int direction)
{ int s ;
   { for (int i=1;i<length+1;i++)
   {
   snake[i-1]=snake[i] ;
   }

   s=0;
   if (xHead<0 && direction==4)
   {
    xHead=79;
   }

   if (xHead>79 && direction==1)
   { xHead=0;
   }

   if (yHead>49 && direction==2)
   {
    yHead=0;
   }
   if (yHead<0 && direction==3)
   {
 yHead=49;
   }
   snake[length]=yHead*80+xHead;
   screen[snake[length]]=2*256;

   if (s!=1)
   {
   screen[snake[0]]=119*256;
   }
   }

  }

//===GETPOINT===========================================================
unsigned int getPoint(int xScreen, int yScreen)
{ return *(screen+yScreen*height+xScreen);

}
//===ISFUNCTIONKEY======================================================
int isFunctionKey(char &order)
{ if(order=='\x0') //k is a function key
 { order=getch();
  return 1;
 }
 else
  return 0;
}
//===CHANGEHEAD=========================================================
void changeHead(int order)
{ switch(order)
 { case 'H': yHead++;
  case 'M': xHead++;
  case 'P': yHead--;
  case 'K': xHead--;
 }
 return;
}
//===DRAWHEAD=============================================================
void drawHead(void)
{ screen[25*80+40]=65;
}
//===FINDTAIL=============================================================
void findTail(int xHead, int yHead, int length, int &xTail, int &yTail)
{
 xTail=xHead;
 yTail=yHead;
 for(int i=0; i<length; i++)
 { switch(snake[i])
  { case 0 : yTail++; break;
   case 1 : xTail++; break;
   case 2 : yTail--; break;
   case 3 : xTail--; break;
  }

 }
}
//===DOCLEARHEAD==========================================================
//===CLEARTAIL============================================================

//===RUN==================================================================
void run(int &order, int &endOfGame)
{ if(order == 27)
  endOfGame=1;
}
===========================================================================

and these are the errors occurd in SciTE text editor when i try to compile it :

>g++ -pedantic -Os -c tet3.cpp -o tet3.o
tet3.cpp:6:19: error: conio.h: No such file or directory
tet3.cpp:10:17: error: dos.h: No such file or directory
tet3.cpp:12: error: expected initializer before ‘*’ token
tet3.cpp:30: error: ‘::main’ must return ‘int’
tet3.cpp: In function ‘int main()’:
tet3.cpp:34: error: ‘randomize’ was not declared in this scope
tet3.cpp:46: error: ‘delay’ was not declared in this scope
tet3.cpp:53: error: ‘screen’ was not declared in this scope
tet3.cpp:95: error: ‘kbhit’ was not declared in this scope
tet3.cpp:96: error: ‘getch’ was not declared in this scope
tet3.cpp: In function ‘void initialize(int, int)’:
tet3.cpp:114: error: ‘clrscr’ was not declared in this scope
tet3.cpp:117: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘void setGround()’:
tet3.cpp:131: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘void setSnake(int)’:
tet3.cpp:138: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘void setFood(int)’:
tet3.cpp:149: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘void move(int, int)’:
tet3.cpp:197: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘unsigned int getPoint(int, int)’:
tet3.cpp:211: error: ‘screen’ was not declared in this scope
tet3.cpp: In function ‘int isFunctionKey(char&)’:
tet3.cpp:217: error: ‘getch’ was not declared in this scope
tet3.cpp: In function ‘void drawHead()’:
tet3.cpp:235: error: ‘screen’ was not declared in this scope
>Exit code: 1

Revision history for this message
smuschel (simon-smuschel) said :
#8

I had a quick look at your example. This seems to be a DOS program, correct? I don't think dos.h and conio.h exist on linux systems. I found something on google about curses.h and ncurses.h as replacement for conio.h. Don't know if there is a linux equivalent for dos.h.
Next problem may be the way the program writes to the screen ( using the far pointer that seems not to be supported on Linux ). I don't know if this can be done on linux the same way you did it on DOS, think I can't help you there.

Revision history for this message
hadi (modarres-zadeh-gmail) said :
#9

there are some programs called dos emulator in the ubuntu repository
can they be useful?

Revision history for this message
hadi (modarres-zadeh-gmail) said :
#10

 I installed one of those dos emulators and installed the borlandc from there
thanks problem solved.