C++ Newbies Question

is prob something Simple

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1903 Views - Last Post: 07 June 2010 - 05:07 PM Rate Topic: -----

#1 aaron1178  Icon User is online

  • Dovakiin, Dragonborn
  • member icon

Reputation: 97
  • View blog
  • Posts: 1,176
  • Joined: 22-October 08

C++ Newbies Question

Posted 04 June 2010 - 11:26 PM

Hey all,
I am new as in just stated just then and im following a tutorial,
im using c++ builder and im making a console app.
I have this code in it:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World";
return 0;
}

i then click run and i compiles it and then opens a console for about 5 seconds but then goes away,
i am new and have no exp with it,
Could you please tell me whats wrong
Thanks Aaron1178

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Newbies Question

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ Newbies Question

Posted 04 June 2010 - 11:34 PM

Read reply 2 from this topic.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: C++ Newbies Question

Posted 05 June 2010 - 01:26 AM

when main returns 0; it returns control back to the operating system and closes your program. int main() is the entry point of your program there for the end of your program is at the end of main. You need to pause the execution before your program returns control back to the OS, return 0; to do this place this before return 0;

cin.ignore(); //ignore input buffer, the buffer can sometimes have garbage characters in it
//you can put your own message if you would like
cout<<"Please press enter to quit...."<<endl;
cin.get(); //get a character from the user


Was This Post Helpful? 1
  • +
  • -

#4 aaron1178  Icon User is online

  • Dovakiin, Dragonborn
  • member icon

Reputation: 97
  • View blog
  • Posts: 1,176
  • Joined: 22-October 08

Re: C++ Newbies Question

Posted 05 June 2010 - 01:55 AM

Ok thanks for your help but im looking to do someting like CMD.exe where it never ends with userinput only when they type /quit or click the X simbol,

thanks aaron1178
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ Newbies Question

Posted 05 June 2010 - 01:59 AM

Then create a loop which loops as long as "/quit" string is not inputted.
Was This Post Helpful? 0
  • +
  • -

#6 aaron1178  Icon User is online

  • Dovakiin, Dragonborn
  • member icon

Reputation: 97
  • View blog
  • Posts: 1,176
  • Joined: 22-October 08

Re: C++ Newbies Question

Posted 05 June 2010 - 02:26 AM

Ok thanks, i do get your point but
it just says welcome to I-Studio - Client
//This is my first C++ Program
//---------Includes---------//
#include <iostream>
#include <string>
//----------Using----------//
using namespace std;
//---------Defines---------//
//----------Main-----------//
int main ()
{
while(cin != "/quit")
{
cout << "Welcome to I-studio - Client\n";
}
}


Now it just wont stop saying it lol. so what do i do here

Anyone

Thanks aaron1178

This post has been edited by aaron1178: 05 June 2010 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

#7 aaron1178  Icon User is online

  • Dovakiin, Dragonborn
  • member icon

Reputation: 97
  • View blog
  • Posts: 1,176
  • Joined: 22-October 08

Re: C++ Newbies Question

Posted 05 June 2010 - 02:36 AM

Please Delete, internet done this

This post has been edited by aaron1178: 05 June 2010 - 02:38 AM

Was This Post Helpful? 0
  • +
  • -

#8 runnerrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-May 10

Re: C++ Newbies Question

Posted 05 June 2010 - 10:24 AM

Would it be of any use to insert this line

system ("pause");

before you end you're program with

return 0;

The program will ask to press any key to continue.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C++ Newbies Question

Posted 05 June 2010 - 10:49 AM

runnerrob while that will work it is a Windows specific command, and using it will remove the portability of C++. So if you were to use this in an application it would only be able to run on a Windows OS, and one of the major perks of C++ (and C) is that it's portable and can run on any OS.
Was This Post Helpful? 0
  • +
  • -

#10 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 545
  • Joined: 12-June 08

Re: C++ Newbies Question

Posted 05 June 2010 - 11:23 AM

Why would you use a Windows specific command that disables the portability of C++ if there is a faster and better substitute already built into C++? I just don't get it :D
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: C++ Newbies Question

Posted 05 June 2010 - 11:51 AM

take a look
//This is my first C++ Program
//---------Includes---------//
#include <iostream>
//----------Using----------//
using namespace std;
//---------Defines---------//
//----------Main-----------//

int main ()
{
	//variable that holds one character
	char command;

	// this loop will always execute atleast once because it checks the conditions at the bottom
  do{
		cout << "Welcome to I-studio - Client\n";
		cout<<"Enter 'q' to quit : ";
	        cin>>command;
   }while(command != 'q'); //if this is true then loop again
   // do 'this' while command DOES NOT (!=) equal 'q'

  //pause window
  cin.ignore();
  cin.get();

  //end
  return 0;
}


This post has been edited by ImaSexy: 05 June 2010 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#12 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ Newbies Question

Posted 05 June 2010 - 01:49 PM

You may want to check this tutorial about loops too.
Was This Post Helpful? 0
  • +
  • -

#13 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 545
  • Joined: 12-June 08

Re: C++ Newbies Question

Posted 05 June 2010 - 02:00 PM

@ImaSexy,

That's how I usually do it too. Simple, fast and easy :D
Was This Post Helpful? 0
  • +
  • -

#14 undertaker17  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 12-March 08

Re: C++ Newbies Question

Posted 05 June 2010 - 04:40 PM

Jus do a system("PAUSE") b4 return 0.

system("PAUSE");
return 0;


Was This Post Helpful? -1
  • +
  • -

#15 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 545
  • Joined: 12-June 08

Re: C++ Newbies Question

Posted 05 June 2010 - 11:57 PM

We have already replied in the posts above that using system functions is a bad way to hold the command window. Using that will make C++ not portable, which is one of it's up sides.

Use
cin.ignore();
cin.get();
instead.

This post has been edited by C++ Programmer: 05 June 2010 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3