|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|||
|
C++ noob question
I recently got into learning C++(by recently, I mean like ten, mabey fifteen minutes ago), I got a compiler, all works well, and I built the typical programmers first program, the hello world bit.
Code:
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
cout << "I'm a C++ program";
return 0;
}
EDIT: it seems that none of the example scripts that a tutorial is giving will run, is it my compiler? i am using one from http://www.bloodshed.net/, is there another one i should use? |
|
|||
|
Quote:
|
|
||||
|
MisterEd is right.. and kinda unhelpful.
Simplest answer ever: before the end of your program, include the following code: Code:
SYSTEM("PAUSE");
Since its running in the CMD window, once the code executes, the command window finishes and closes. putting the PAUSE at the end will cause the CMD window to wait for further user input before closing.. e.g., press any key to continue.. cin by the way is used to read input into a variable. e.g. Code:
cout << "What's your name"; cin >> name; cout << "Nice to meet you " << name << endl; this would prompt a user for their name, and then when they typed it and pressed enter (cin reads until an end line) it stores the input in a memory address, which is accessed through the variable "name". The 3rd line would then output "Nice to meet you John" - or whatever the name was, followed by an endline i.o.w. a carriage return. |
|
||||
|
BTW: For more programming help, may I advice DevH's smarter sister: DevArticles.
A bit quieter, but experts in every programming language you can dream of (and then some). Good luck!!
__________________
This is my sig. Is it not nifty? Itsacon's Sig v2. Click here =>
![]() |
|
|||
|
Quote:
My code is now this, Code:
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
SYSTEM("PAUSE");
}
and when i try to compile it, it highlights the system part, and gives these three errors Code:
C:\Documents and Settings\Bowkid\My Documents\codersanonymous\c++\firstprog.cpp In function `int main()': Code:
10 C:\Documents and Settings\Bowkid\My Documents\codersanonymous\c++\firstprog.cpp `SYSTEM' undeclared (first use this function) Code:
(Each undeclared identifier is reported only once for each function it appears in.) |
|
|||
|
could you give an example of one of them?
|
|
||||||||||
|
Quote:
You need to add #incluce <stdlib> as above and try system with lower case and insert it before return statement. ![]() also you can do following: Quote:
__________________
Click Here ->
![]()
|
|
|||
|
ok, that first one didn't work, it said something about unknown #include <stdlib> or something, and the secound one did AFTER, i added a ; to the end of cin >> enter gotta watch those statement enders :P
|
|
||||
|
Code:
#include <iostream>
main() {
std::cout << "Enter a friggin' number: ";
int x;
std::cin >> x;
//these print the same thing
std::cout << "You entered " << x << "." << std::endl;
printf("You entered %d.\n", x );
system("PAUSE");
}
when the compiler spits the "`SYSTEM' undeclared (first use this function)" error out, it should tell you that the compiler has no idea what you're talking about when you give it "SYSTEM." usually this compile error occurs when you spell something incorrectly (like in this example) or when you try to access a variable you haven't previously declared. remember, most programming languages (if not all...) are case-sensitive. ![]()
__________________
Desktop: Venice E3 0519 [@300x8=2.4GHz; 1.39v]; PDP XBL DDR400 (2x512) [blew em out...needs an RMA ]; DFI NF4 Ultra-D; ATI X1900XTX [@700/1700]; NeoHE 500160GB Barracuda 7200.9 SATA2, 250GB WDC 16MB SATA2; SB X-Fi; Samsung DVD+-R/RW/DL; TT VA8000BWS; Samsung 22" 225BW; Logitech Z5500; XP Pro, RC1 (5600) Laptop: HP dv5000t; Yonah @ 2GHz; DDR2 533 (2x512); Go7400 128MB; 100GB 5400RPM SATA; 802.11a/b/g; DVD+-R/RW/DL; 15.4" Brightview; RC1(5600), XP Pro, Ubuntu Last edited by PunkRocKEVin : June 26th, 2006 at 07:18 PM. |
|
|||
|
Quote:
Your code should look like this: Code:
#include<iostream>
int main ()
{
std::cout << "Hello World!";
std::cout << "I'm a C++ program";
std::cin.get();
return 0;
}
get() will pause the program until the user gives input, usually by pressing enter. |
|
|||
|
im sorry, but that was pretty much a spam post, the topic was old, and several solutions had already been given
|
|
||||
|
Quote:
![]()
__________________
“Greatness, combined with the hint of a 24-carat lifestyle, is within us striving to make it through, craving for attention, and in the end, recognition, leaving a record worthy of biography on forging success stories.” —ME Check out MadHyeNa's Article Index. I succeed, conquer, and achieve, therefore I am.™ |
| Viewing: Dev Hardware Forums > SOFTWARE > Programming > C++ noob question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|