New Posts  All Forums:Forum Nav:

[C++] Hello world

post #1 of 27
Thread Starter 
I am trying to learn C++ and obviously the first task is "hello world." This is the code I have:
Code:

#include <iostream>

int main()
{
        std::cout << "Hello, World!";
}


It builds fine and then runs but it executes so fast I can't even see the text on the command prompt. Is there a way to like make it last for ten seconds or pause till I hit ENTER?

Thanks
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
post #2 of 27
Code:
#include <iostream>

int main()
{
        std::cout << "Hello, World!";

        std::cin.ignore(256, '\n'); // Add this, do NOT use system("pause");
}
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
post #3 of 27
Thread Starter 
Quote:
Originally Posted by Joakitex View Post

Code:
#include <iostream>
int main()
{
        std::cout << "Hello, World!";
        std::cin.ignore(256, '\n'); // Add this, do NOT use system("pause");
}

Thanks, will try this now biggrin.gif

Working great, thanks.

+REP
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
post #4 of 27
Why don't you just execute the program from the command line?
In fact any Linux IDEs have a test console that remains active even when the program has finished.
post #5 of 27
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

Why don't you just execute the program from the command line?
In fact any Linux IDEs have a test console that remains active even when the program has finished.

I am on my laptop though and using Windows 7. I know. Don't hate on me xD

I am using Visual Studio so this is just the way it will be plan frown.gif
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
post #6 of 27
Quote:
Originally Posted by joshd View Post

I am on my laptop though and using Windows 7. I know. Don't hate on me xD
I am using Visual Studio so this is just the way it will be plan frown.gif

Wait...if you're using an IDE such as Visual Studio, you can just execute the code from within it and it should keep the window open. The IDE should handle the "console" part by itself.

Or did I not understand your question correctly?
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #7 of 27
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

Wait...if you're using an IDE such as Visual Studio, you can just execute the code from within it and it should keep the window open. The IDE should handle the "console" part by itself.
Or did I not understand your question correctly?

I'm unsure lol

When I build the code, then press F5 to run it a black console window opens up and in like half a second closes. Now however I am forcing it to stay open and it displays the words "hello world" and then I press enter to close it...

Does that help?
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
post #8 of 27
Oh yeah, ok fine then. I suppose the method you are using now is fine.
There are a few other ways to do this but your current code (inputting character and ignoring it) seems the best way to achieve what you want to do.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #9 of 27
I'm still in the learning phase of C++ as well... what's up with the std::cin and all that? I put using namespace std or whatever at the top of my code and just type cin.get etc.

Am I doing it wrong? or am I going to have to eventually start putting std:: in front of everything in the future? Is that a class thing?
Beast of Burden
(19 items)
 
Cool story, bro!
(16 items)
 
 
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77X-UD5H Gigabyte HD 7970 Reference Ripjaws Z 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Caviar Black ASUS DVD XSPC Raystorm 
CoolingCoolingCoolingOS
EK FC-7970 w/ Backplate Copper/Acetal RX360 w/ 3x Yate Loon High Speed in Push XSPC Dual Bay Res w/ D5 Pump Windows 7 
MonitorMonitorKeyboardPower
ASUS VE226H ASUS VE226H Cooler Master Quickfire Rapid Corsair HX850 
CaseMouseMouse Pad
HAF 932 Advanced Razer Deathadder QcK Mini 
CPUMotherboardGraphicsRAM
Q9550@ 3.5GHz w/ 1.191v Gigabyte GA-EP45-UD3P Sapphire Reference 5850 @ 775/1100 G.SKILL 4GB (2 x 2GB) DDR2 1066 
Hard DriveHard DriveHard DriveOptical Drive
Crucial C300 Spinpoint F3 Caviar Black ASUS DVD-RW 
CoolingOSMonitorKeyboard
Arctic Cooling Freezer 7 Pro Windows 7 Home Premium 64-bit Samsung 23" Saitek Eclipse III 
PowerCaseMouse
Corsair 430W NZXT Source 210 Wolf King 
  hide details  
Reply
Beast of Burden
(19 items)
 
Cool story, bro!
(16 items)
 
 
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77X-UD5H Gigabyte HD 7970 Reference Ripjaws Z 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Caviar Black ASUS DVD XSPC Raystorm 
CoolingCoolingCoolingOS
EK FC-7970 w/ Backplate Copper/Acetal RX360 w/ 3x Yate Loon High Speed in Push XSPC Dual Bay Res w/ D5 Pump Windows 7 
MonitorMonitorKeyboardPower
ASUS VE226H ASUS VE226H Cooler Master Quickfire Rapid Corsair HX850 
CaseMouseMouse Pad
HAF 932 Advanced Razer Deathadder QcK Mini 
CPUMotherboardGraphicsRAM
Q9550@ 3.5GHz w/ 1.191v Gigabyte GA-EP45-UD3P Sapphire Reference 5850 @ 775/1100 G.SKILL 4GB (2 x 2GB) DDR2 1066 
Hard DriveHard DriveHard DriveOptical Drive
Crucial C300 Spinpoint F3 Caviar Black ASUS DVD-RW 
CoolingOSMonitorKeyboard
Arctic Cooling Freezer 7 Pro Windows 7 Home Premium 64-bit Samsung 23" Saitek Eclipse III 
PowerCaseMouse
Corsair 430W NZXT Source 210 Wolf King 
  hide details  
Reply
post #10 of 27
Quote:
Originally Posted by DaClownie View Post

I'm still in the learning phase of C++ as well... what's up with the std::cin and all that? I put using namespace std or whatever at the top of my code and just type cin.get etc.
Am I doing it wrong? or am I going to have to eventually start putting std:: in front of everything in the future? Is that a class thing?

What you are doing is fine, but it is considered a bad practice because of the following:

http://stackoverflow.com/questions/1265039/using-std-namespace
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
Ziltoid
(14 items)
 
Exo x352
(6 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500k @ 4.1GHz ASUS P8Z68-v PRO XFX 9500GT 2x4GB GSkill Ripjaws x 1600MHz 
Hard DriveOSMonitorMonitor
OCZ Vertex 3 60GB Windows 8 LG FLATRON W1943SE LG 23EA53 
PowerCase
Sentey GSP750-SM Coolermaster 690 II 
CPUMotherboardGraphicsRAM
Intel Atom N455 ECS E11IS1 GMA 3150 1 GB 667 MHz 
Hard DriveOS
Fujitsu 160 GB 5200 rpm Lubuntu 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming