Author Topic: [C++] First Program  (Read 8901 times)

0 Members and 1 Guest are viewing this topic.

Offline a4r9

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
[C++] First Program
« on: June 14, 2009 »
Hello, this is my first C++ program. It should be pausing but does not pause. Instead it simply creates the window displaying the message and then quits. How can I make the window stay open to read the message? I'm using Dev-C++ under Windows 2k.

Code: [Select]
#include <iostream>
      
using namespace std;
      
int main()
{
 cout << "Hello World!" << endl;
          
 cin.get();
}
« Last Edit: June 14, 2009 by Pixel_Outlaw »

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] First Program
« Reply #1 on: June 14, 2009 »
I'm fairly new to C++ myself and your code works fine. I'm not quite sure what the problem is with your computer. I'm using Dev-C++ myself but running it in XP.
Challenge Trophies Won:

Offline mziskandar

  • C= 64
  • **
  • Posts: 46
  • Karma: 1
    • View Profile
Re: [C++] First Program
« Reply #2 on: June 14, 2009 »
Works fine with CodeBlocks (mingw) too..

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: [C++] First Program
« Reply #3 on: June 14, 2009 »
try replacing "cin.get();" with "System("PAUSE")"
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] First Program
« Reply #4 on: June 14, 2009 »
In general, there's no way to make it pause.  system("pause") will only work on PCs.

Welcome to the forum a4r9!

Jim
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: [C++] First Program
« Reply #5 on: June 14, 2009 »
Try to make a loop with do..while and check when user press enter for example.
Challenge Trophies Won:

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: [C++] First Program
« Reply #6 on: June 14, 2009 »
Welcome to the forum a4r9!

Welldone on your first c++ program and keep going  ;) :)

Offline a4r9

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: [C++] First Program
« Reply #7 on: June 14, 2009 »
Thanks ya'll, although it's still not working, still doing the same thing, I'm starting to think it's the OS

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: [C++] First Program
« Reply #8 on: June 14, 2009 »
What is doing the same things?
What error did you have?
what OS do you have?

Offline a4r9

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: [C++] First Program
« Reply #9 on: June 14, 2009 »
Is still coming up with a screen then going away instantly either saying already compiling or source could not compile. And I have Windows 2000 sadly.

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: [C++] First Program
« Reply #10 on: June 14, 2009 »
Strange.

try that as work very well with Visual C++ 2008 Express!

Code: [Select]
# include <iostream>
using namespace std;

int main()
{
cout <<"hello world";

// Pause the Screen
getchar();   // This line of code is from Programming C but still pause the screen and it is work!

return 0;
}

Offline a4r9

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: [C++] First Program
« Reply #11 on: June 14, 2009 »
That still doesn't work, I'm starting to think that maybe I should try it on my mom's computer, she runs XP, I miss it, was great for everything XD

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: [C++] First Program
« Reply #12 on: June 14, 2009 »
The program I have used above on DEV C++(I have check that) and Visual C++ 2008 Express work very well on either XP and Vista too but not sure about Window 2000 thought.  :)

let me know how you get on thought when you go on XP testing your program :)

Offline madsravn

  • ZX 81
  • *
  • Posts: 13
  • Karma: 0
    • View Profile
Re: [C++] First Program
« Reply #13 on: June 18, 2009 »
Sometimes cin.get() takes something from buffer (or something), I think I heard.

Try doing a cin.get(ch).get(ch);
Code: [Select]
#include <iostream>

int main()
{
std::cout << "Hello World" << std::endl;
char ch;
cin.get(ch).get(ch);
return 0;
}

I know it isn't pretty, but it should work. You would maybe be able to do cin.get().get(). I thought I could too, but g++ started whining about it.

/ Mads

Offline a4r9

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: [C++] First Program
« Reply #14 on: June 22, 2009 »
Indeed it works in XP, it must be the operating system then.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] First Program
« Reply #15 on: June 22, 2009 »
Congrats dude on getting it fixed.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] First Program
« Reply #16 on: June 23, 2009 »

Yes, I don't have any idea why it would not work on you other OS...

If you want a game based C++ book you might try C++ Programming for the Absolute Beginner just ignore the last chapter on DirectX as we all know it is one of the tendrils of the Microsoftoctopus. C++ for Dummies is also very extensive. After finishing Beginning C++ Game Programming I can honestly say that the later chapters of the book completely fall apart. The author stops commenting on code and starts introducing like 2 new concepts each exercise.
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: [C++] First Program
« Reply #17 on: June 23, 2009 »
I learned all I know of C++ from Preacher/Traction's source code and the OpenGL API from http://nehe.gamedev.net/, though GLSL I learned from the Orange Book.
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won: