C++ Hello World not working

Okay, I'm very new to using Visual Studio 2015 Community version, and I'm following a tutorial in CPP by another author. His code looks like this:

#include <cstdio>
using namespace std;

int main( int argc, char ** argv )
{
    puts("Hello, World!");
    return 0;
}

Open in new window


But Visual Studio creates me a default file that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Working
{
    class Program
    {
        static void Main(string[] args)
        {
            putc("Hello World"); // I put this in, not generated by VS
        }
    }
}

Open in new window


When I try to build the above in VS, I get this message: CS0103      The name 'putc' does not exist in the current context.

I thought maybe the missing #include <cstdio> was the problem because that's where putc() is defined, but when I put that in, I get a new error: CS1024      Preprocessor directive expected.

It has been a long time since I did any C++, and this is my very first attempt to using VS to learn CPP.

Can someone please tell me what is wrong here?
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
You have created a "managed" C++ project. You need to create an "unmanaged" project.
0
sarabandeCommented:
you created a .net project what is managed c++ (actually a different language with c++ syntax).

your sample code is unmanaged (normal) c++. you should create an empty win32 project (not using precompiled headers) to get your sample code running. in the general properties also change unicode to multi-byte character set before build.

note putc writes a single character to console while puts prints a zero-termianted string (like the "Hello World") to console.

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
To be more specific, managed C++ uses the .Net framework and is not ANSI Standard C++. It is C++ with Microsoft's own .Net extensions that uses the CLR (Common Language Runtime). If you want to learn C++ (real C++) you need to create a non .Net (unmanaged or sometimes referred to as "native") C++ project.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

evilrixSenior Software Engineer (Avast)Commented:
>> The name 'putc' does not exist in the current context.
I think you mean puts, no? The former puts a char, the latter puts a string.
http://www.cplusplus.com/reference/cstdio/puts/
0
sarabandeCommented:
actually stdio.h, puts, putc is c and not c++. you could use c in your c++ code but it is not recommended as far c++ provides better solutions.

so you may try

#include <string>
#include <iostream>

int main(int argc, char * szargs[])
{
      std::cout << "Hello World" << std::endl;
      return 0;
}

Open in new window


you could put a breakpoint at return statement (click left of line into grey colum to get a breakpoint).

then build the program by f7 and run it in the debugger by f5. it will open a console window and show the "Hello World". because of the breakpoint it will hold at last statement. otherwise the window would close immediately and you see nothing.

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
To create an unmanaged project in Visual Studio 2015:

1. From the menu: File >> New >> Project
2. From the left windows select: Installed >> Templates >> Visual C++
3. From the middle select: Win32 Console Application
4. Click OK
5. In the next windows click "Next"
6. Untick "Precompiled Header" and "Security Development Lifecycle checks"
7. Click "Finish"
8. Replace the "boilerplate" code with your own and built.

You should have a working program.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
evilrix, I followed your instructions. But when I got to:

2. From the left windows select: Installed >> Templates >> Visual C++

I am still getting errors. Here is the new code:

// Working.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int main()
{
	puts("Hello, World!");
	return 0;
}

Open in new window


I followed what you said by replacing the boilerplate code it had, and that's all I changed. The rest were generated by VS. But now I'm getting the following two errors:

Severity	Code	Description	Project	File	Line
Error	LNK2005	_main already defined in hello.obj	Working	D:\CPP\Working\Working\Working.obj	1
Error	LNK1169	one or more multiply defined symbols found	Working	D:\CPP\Working\Debug\Working.exe	1

Open in new window

0
sarabandeCommented:
8. Replace the "boilerplate" code with your own and built.

visual studio projects were using UTF16 character set (which is a double-byte character set called UNICODE by ms). you have to change that to ANSI character set (which is called Multi-byte character set by ms). you find the setting in menu project - properties - configuration properties - general - character set.

alternatively, change the "Hello World" to _T("Hello World") what makes the literal a wide character string (wchar_t *).

Sara
0
sarabandeCommented:
you can't have both working.cpp and main.cpp in the project since both implement a function main. instead right-click on main.cpp in the solution tree and choose properties. then 'exclude main.cpp' from build.

Sara
0
elepilAuthor Commented:
evilrix, my mistake, I had a hello.cpp (which was from the tutorial in there and accidentally compiled it. So VS ended up complaining about two main() functions. I deleted hello.cpp and recompiled, and it compiled fine.

Sara, thx for the tip on putting a breakpoint at the return statement, as that did leave a command console up. My beef with it though is that that command console did not have an icon in the taskbar. I had to do Alt-Tab or Window-Tab to see the command window.

I cannot believe how complex things have become just to run a Hello World program. Things were much simpler back then when I was using Borland's Turbo C++ and Microsoft's Visual C++ 2.0 :)

P.S. More points was given to evilrix because he went to the extent of enumerating the menu steps I had to do in VS. Thanks!
0
sarabandeCommented:
or rename function name 'main' in main.cpp by 'xmain'.

then the code should work (don't forget to set the breakpoint to return statement. you also can do it via menu debug - breakpoint).

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
>> visual studio projects were using UTF16 character set (which is a double-byte character set called UNICODE by ms).

That is kind of irrelevant since the actual example code is all narrow and doesn't use wide chars! My instructions specifically said, "Replace the "boilerplate" code with your own".

>> But now I'm getting the following two errors:

You seem to have two main functions. Remove the one that isn't yours! As I previously said, you should have replaced the existing code with your own. Don't add a new file, just delete the original code and copy/past your own in its place.
0
evilrixSenior Software Engineer (Avast)Commented:
>> I cannot believe how complex things have become just to run a Hello World program.
You can thank Microsoft for this and their stupid .Net approach to everything.

If you really want to learn proper C++ install Linux and use gcc :)
0
sarabandeCommented:
My beef with it though is that that command console did not have an icon in the taskbar.
it should. it is a white sheet icon.

did you try the code i posted using the std::cout ?

you also could create an mfc application project. make it dialog-based (page 'Application Type').

the rest could be made default.

after creation the resource editor pops up with a dialog that has a 'to do  ...' message in a static control and an ok and cancel button. click on the 'To do' control and make 'ALT+ENTER' to see the properties of the control. change text to "Hello World" and build.

Sara
0
sarabandeCommented:
That is kind of irrelevant since the actual example code is all narrow
that is irrelevant for the c sample code but important for all future code based on header files using the MS T convention. the stdafx.h which is included by the wizard, also contains myriads of T types which allow to switch from wide characters to narrow characters and reverse.

Sara
0
elepilAuthor Commented:
Sara, yes I did try our std::cout version, but the compiler underscores them in red saying:

Error: namespace "std" has no member "cout". Frankly, cout is one of the things I remember from back when I was dabbling with C++. Maybe I don't have the right #include?
0
evilrixSenior Software Engineer (Avast)Commented:
But not relevant if the asker is just learning standard ANSI C++, since all the standard ANSI C++ stuff works exactly as it should do. It only becomes relevant when working with Microsoft specific system functions. In other words, I already tested the instructions before I wrote them, so I knew it would work just fine as is, without the need to add confusion regarding Microsoft's insistence that wide is the same as UTF16 (it's not) and that UTF16 is a sensible encoding format for Unicode (it's not).

http://www.experts-exchange.com/articles/18363/When-is-Unicode-not-Unicode-When-Microsoft-gets-involved.html
0
evilrixSenior Software Engineer (Avast)Commented:
>>  Maybe I don't have the right #include?
You need to include <iostream>
0
elepilAuthor Commented:
Is there a good book I can read to learn all about this new stuff? I'm feeling a bit overwhelmed because I am hearing so many terms non-existent back then.

After doing my research, I realized that C++ is the "power language" by which all others are made. (It used to be C, but I understand how OOP reigns now).
0
elepilAuthor Commented:
evilrix, I added #include "iostream.h" or #include <iostream.h>, both failed.

What is the difference now again if I put it in quotes or angle brackets? I don't remember.
0
evilrixSenior Software Engineer (Avast)Commented:
You don't need the .h at the end - in fact, in C++ none of the standard headers have a .h at the end. You need...

#include <iostream>

not

#include <iostream.h>

>> What is the difference now again if I put it in quotes or angle brackets? I don't remember.
Quotes searches relative to the current files location, angled brackets search in standard compiler include paths.

As for books: C++ for dummies first and second volume are excellent.
0
sarabandeCommented:
Sara, yes I did try our std::cout version, but the compiler underscores them in red saying:
if you just copy the sample i posted you have the right include statements.

note, all include files of standard template library (stl) don't have a .h extension. for visual studio it is 1998 when they firstly added a pre-standard stl library with vc6.

Quotes searches relative to the current files location, angled brackets search in standard compiler include paths.

actually visual studio is rather insensible regarding kind of brackets. if you use quotes for your own headers and angled brackets for all others you never will get issues because of that.

But not relevant if the asker is just learning standard ANSI C++

i found some remarks to "ansi c++":


    The official names of the ISO and U.S. committees are:

    ISO/IEC JTC1/SC22/WG21 - C++ (ISO C++ committee)

    INCITS PL22.16 - C++ (the U.S. C++ committee, often called the "ANSI C++ committee" -- although more accurately INCITS is a separate organization that is not a part of ANSI but is accredited by ANSI to develop U.S. standards) (previously called X3J16 and J16)

    These two groups together are often referred to in the singular as "the C++ committee" because they meet and do their work together. The meetings are always in the same room at the same time

There isn't, and never has been a separate ANSI standard for C++.

i personally know ansi c but not ansi c++ . perhaps evilrix can explain what ansi c++ is.

generally you could use visual studio without being bothered of the UTF16/ANSI issue nor with ms specific headers around winapi, mfc, and atl.

however, that implies that you avoid wizard created sample projects, precompiled headers, and ms headers  generally. that could be very stressful and perhaps impossible for some projects. so, actually to defuse most barriers you would switch back to multi-byte character set, switch off precompiled headers and remove stdafx.h and stdafx.cpp from project. finally use stl classes and stl containers before MS classes wherever possible.

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
C++11 is the current C++ Standard as ratified by the Standards Committee. This is generally accepted as the current standard version of C++.

C89/90 is often referred to as ANSI C, but the current standard is C11.

Strict speaking these standards are published by the International Organization for Standardisation (ISO) but old-timer, like myself, often refer to them as ANSI standard because that was the original name of the Standards body who published the original standards documents.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.