Welcome to PlagueFest.com! Log in or Sign up to interact with the Plague Fest community.
  1. Welcome Guest! to interact with the community and gain access to all the site's features.

So it begins....

Discussion in Programming started by Merlin, Oct 21, 2014

  1. Oct 10, 2014
    Posts
    Using the link you attached in my last thread bender, I have begun reading through the introduction portion of C++ and have already stumbled across a question in the "structure of a program" section. Possibly a stupid question, but is there a difference between using "std::cout" and "cout"? It would seem that using either appear to have the same results.
  2. Feb 24, 2011
    Posts
    Don't trust me 200%, but i think that the "::" is calling the "namespace" of a class.
    "cout" is a method which belongs in the "std" class/library thingie.

    So, you could either do this in your .cpp:
    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
      cout << "Hello guv'no!";
      return 0;
    }
    


    Or this:
    Code:
    #include <iostream>
     
    int main()
    {
      std::cout << "Hello guv'no!";
      return 0;
    }
    
    If you don't do "using namespace std;" it wouldn't work to just call "cout".
    When you're using namespace std it pre-actively calls for the "std::" in the background. (I ASSUME THIS! I have no real clue if i've understood this concept correctly)
    I personally prefer to never use any namespaces and go directly with the "::", but it's all up to the developer and his/her needs.
    • Winner Winner x 2
    • Oct 10, 2014
      Posts
      ok, that actually makes a whole lot more sense to me now. I failed to see the section in the code "using namespace std;" when reading the example before.
    • Jun 11, 2012
      Posts
      What Luff said. It's to just shorten the call. This works if you are using a namespace not with a Class. For that you'll have to do add that extra for it.
    • Jul 13, 2008
      Posts
      Huragh someone on the server learning c++.... Luffaren is correct the 'using namespace std;' placed directly under your #include files removes the need for std::whatever.

      if you have vs2010 or vs2012 try this in console mode and make the console 80x50 (not standard 80x25)

      //vaders code doc watson
      // Program Template with clearscreen, gotoxy and textcolor functions
      // no collision detection
      #include <iostream>
      #include <conio.h>
      #include <windows.h>
      using namespace std;

      #define ESC 27

      void gotoxy(int,int); //declare a function called gotoxy
      void clrscr(void); //declare clear screen function
      void textcolor(int); //declare text colour function
      void hline(int,int,int,int);
      void invader1(int,int,int);//draw invader(x,y,colour)
      void invader2(int,int,int);//draw invader(x,y,colour)


      char b=219;
      void main (void)//main program start
      {
      char key;
      int x=1,vx=1,vt=0;
      DWORD t1,t2;

      t1 = GetTickCount();
      do
      {
      t2= GetTickCount();
      if(t2-t1>1000)
      {
      t1=t2;
      x=x+vx;
      if(x>68) vx=-vx;
      if(x<1) vx=-vx;
      clrscr();
      if(vt==0)//vt is a flip flop switch to draw vader1 then vader2
      {
      invader1(x,1,10);
      vt=1;
      }
      else
      {
      invader2(x,1,10);
      vt=0;
      }
      }
      }
      while(!kbhit());
      _getch();
      }//end of the code


      void invader1(int x,int y,int c)
      {
      textcolor(c);
      gotoxy(x+2,y);cout<<b;
      gotoxy(x+8,y);cout<<b;
      gotoxy(x+3,y+1);cout<<b;
      gotoxy(x+7,y+1);cout<<b;
      hline(x+2,y+2,7,c);
      hline(x+1,y+3,2,c);
      hline(x+4,y+3,3,c);
      hline(x+8,y+3,2,c);
      hline(x ,y+4,11,c);
      hline(x+2,y+5,7,c);
      gotoxy(x,y+5);cout<<b;
      gotoxy(x,y+6);cout<<b;
      gotoxy(x+2,y+6);cout<<b;
      gotoxy(x+3,y+7);cout<<b;
      gotoxy(x+4,y+7);cout<<b;
      gotoxy(x+6,y+7);cout<<b;
      gotoxy(x+7,y+7);cout<<b;
      gotoxy(x+8,y+6);cout<<b;
      gotoxy(x+10,y+6);cout<<b;
      gotoxy(x+10,y+5);cout<<b;
      }

      void invader2(int x,int y,int c)
      {
      textcolor(c);
      gotoxy(x+2,y);cout<<b;
      gotoxy(x+8,y);cout<<b;
      gotoxy(x+3,y+1);cout<<b;
      gotoxy(x+7,y+1);cout<<b;
      hline(x+2,y+2,7,c);
      hline(x+1,y+3,2,c);
      hline(x+4,y+3,3,c);
      hline(x+8,y+3,2,c);
      hline(x,y+4,11,c);
      hline(x+2,y+5,7,c);
      gotoxy(x ,y+3);cout<<b;//
      gotoxy(x ,y+2);cout<<b;//
      gotoxy(x+2 ,y+6);cout<<b;
      gotoxy(x+1 ,y+7);cout<<b;//
      gotoxy(x+9 ,y+7);cout<<b;
      gotoxy(x+8 ,y+6);cout<<b;
      gotoxy(x+10,y+3);cout<<b;//
      gotoxy(x+10,y+2);cout<<b;//
      }

      void hline(int x, int y,int l, int c)
      {
      int i;//notice i is moved from main() to hline()

      textcolor(c);
      for(i=0;i<l;i++)
      {
      gotoxy(i+x,y);
      cout<<b;
      }
      }

      void textcolor(int c)
      {
      HANDLE hConsole;
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hConsole, c);
      }
      void gotoxy(int x, int y) //define gotoxy function
      {
      static HANDLE hStdout = NULL;
      COORD coord;

      coord.X = x;
      coord.Y = y;
      if(!hStdout) {hStdout = GetStdHandle(STD_OUTPUT_HANDLE);}
      SetConsoleCursorPosition(hStdout,coord);
      }
      void clrscr(void)
      {
      static HANDLE hStdout = NULL;
      static CONSOLE_SCREEN_BUFFER_INFO csbi;
      const COORD startCoords = {0,0};
      DWORD dummy;

      if(!hStdout)
      {
      hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
      GetConsoleScreenBufferInfo(hStdout,&csbi);
      }
      FillConsoleOutputCharacter(hStdout,' ',csbi.dwSize.X * csbi.dwSize.Y,startCoords,&dummy);
      gotoxy(0,0);
      }
      • Wizard! Wizard! x 1
      • Oct 10, 2014
        Posts
        not going to lie, my head is spinning a little bit from all that haha