"hello world" help

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 715 Views - Last Post: 09 November 2011 - 11:29 AM Rate Topic: -----

#1 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

"hello world" help

Posted 06 November 2011 - 10:20 PM

My question: Does VS not support terms such as
cout<<"Hello World";
because everywhere I look program examples have code such as cout<<"Hello World!"; and cin>>; but I find myself having to use
Console::WriteLine("Hello world!");

Is this the only way now? Or have I overlooked something/done something incorrect.

Is This A Good Question/Topic? 0
  • +

Replies To: "hello world" help

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: "hello world" help

Posted 06 November 2011 - 10:56 PM

Can you post your entire code?

VS most certainly does support cout, but you must declare namespace first.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: "hello world" help

Posted 06 November 2011 - 11:01 PM

> Console::WriteLine("Hello world!");

Are you sure you are working with the right language??
Was This Post Helpful? 1
  • +
  • -

#4 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: "hello world" help

Posted 06 November 2011 - 11:19 PM

This is the code that works, notice I do include the iostream.
// test.cpp : main project file.

#include "stdafx.h"
#include <iostream>;
#include <string>;

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine("hello");
	Console::ReadLine();
    return 0;
}



and here is code that does not work; I will even post the error messages.
// test.cpp : main project file.

#include "stdafx.h"
#include <iostream>;
#include <string>;

using namespace System;

int main(array<System::String ^> ^args)
{
    cout<<"hello";
	cin>>;
    return 0;
}


and the errors:Error 3 error C2065: 'cout' : undeclared identifier and Error 4 error C2065: 'cin' : undeclared identifier

Also yes this is c++
Was This Post Helpful? 0
  • +
  • -

#5 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: "hello world" help

Posted 06 November 2011 - 11:40 PM

You don't need the semicolons appended to the include statements.

The namespace isn't System. It's std.
(using namespace std;)
Was This Post Helpful? 0
  • +
  • -

#6 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: "hello world" help

Posted 07 November 2011 - 12:03 AM

// test.cpp : main project file.

#include "stdafx.h"
#include <iostream>;
#include <string>;

using namespace std;

int main(array<System::String ^> ^args)
{
    cout<< "hello"
	;cin>> 
    return 0;
}


return gets a syntax error
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: "hello world" help

Posted 07 November 2011 - 12:10 AM

View PostTylermon, on 07 November 2011 - 02:03 AM, said:

    cout<< "hello"
	;cin>> 
    return 0;
}


return gets a syntax error

That's because the above is invalid syntax. Semicolons indicate the end of the instruction line. Therefor the semicolon goes at the end of the line & not the beginning of it.
Was This Post Helpful? 0
  • +
  • -

#8 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: "hello world" help

Posted 07 November 2011 - 12:12 AM

Also what is the difference between win32 app and CLR app? Their descriptions in the ide are practically the same and I'm at a loss as to what to use to make a basic console application.

p.s thanks for correcting the namespace! I would never have solved that due to it being the default namespace the new project created :P but it makes me wonder...what is the difference of the two?
Was This Post Helpful? 0
  • +
  • -

#9 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: "hello world" help

Posted 07 November 2011 - 12:19 AM

View Postno2pencil, on 07 November 2011 - 12:10 AM, said:

View PostTylermon, on 07 November 2011 - 02:03 AM, said:

    cout<< "hello"
	;cin>> 
    return 0;
}


return gets a syntax error

That's because the above is invalid syntax. Semicolons indicate the end of the instruction line. Therefor the semicolon goes at the end of the line & not the beginning of it.

sorry, that was bad formating on my part but pretending i had written it as
cout<< "hello";
	cin>> ;
    return 0;

I recieve an error to remove the ";" after cin>> which then leads to my code looking like:
cout<< "hello";
	cin>> 
    return 0;

and this being the same as what i had before, i still have a syntax error for return.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: "hello world" help

Posted 07 November 2011 - 12:26 AM

That's because you are not using cin correctly. cin is expecting an argument, & you have not provided it one.

You have to think logically, what is the cin command? What does it do? What do you expect it to do? & how are you using it in your program?
Was This Post Helpful? 1
  • +
  • -

#11 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: "hello world" help

Posted 07 November 2011 - 12:38 AM

Why do you have the cin there in the first place? If you want to hold open the console window then use cin.get() so you have to press enter.

You're getting errors because cin is used to get input from the user.

Something like this.

// . . .
int number; // Tell the compiler to create a variable called "number"
cin >> number; // Get the value of number from the user's input.
// The user will probably enter a number, such as 8.
// . . .


This is a full example of the above code I just wrote.

#include <iostream>

using namespace std;

int main()
{
    int number;
    cin >> number; // This is a valid cin statement because you provided the variable there. (It is normal if you are currently confused.)
    return 0;
}




Oh by the way, an argument is data you pass so the argument in this case is "number" but you don't have any arguments in your cin line so how will the compiler know what to do? Like no2pencil said, think logically. :)
Was This Post Helpful? 0
  • +
  • -

#12 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: "hello world" help

Posted 07 November 2011 - 12:44 AM

Wait, what? When you made the main.cpp in your project, using namespace System; was already there?

What compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "hello world" help

Posted 07 November 2011 - 01:31 AM

Visual Studio has two "flavors" of C++ -- the regular one and then the "managed" .Net version (C++/CLI).

Generally speaking most of the people on this particular forum will not be much help if you are working with the CLI version (see the link above for the proper forum).

If you choose to create a new native project you should get something that looks a little more like a C++ program and less like C#.
Was This Post Helpful? 4
  • +
  • -

#14 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: "hello world" help

Posted 07 November 2011 - 09:32 PM

Thank you for all the help, my IDE is Microsoft Visual Studio 2010, how do I check what type of C++ it is using? Or change it to the "regular" version of C++? Because although I'm comfortable with java and C# I'm trying to learn C++ due to how it is unmanaged and different from the two. Hence my confusion when I was having to use a WriteLine method for C++.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: "hello world" help

Posted 08 November 2011 - 04:10 AM

MSDN is always a good starting point
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2