Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and run simple C++ programs in Visual Studio 2012?

I'm currently a C.Sc student in India. We have lots of C++ to study but still is limited. Even if it goes the farthest, the toughest programs would be to make a library management system or a bank management system. The problem with the current syllabus is that it relies on the old C++ standards and we work on the old Turbo C++ compiler.

Sine I have a Windows 8 system, I can't make Turbo C++ to work seamlessly in my system. I would have to use DOSBox to emulate the executables. Moreover, I would like to switch to a newer compiler/IDE that's based on the latest C++ standards and fits for future education.

I tried Visual Studio and NetBeans and both are powerful and excellent. But I can't cope with these IDEs as I don't find ways to practice the silly codes I learn from school. We don't learn C++ to an extended level as I said before. The options in Visual Studio is to create a new project and I did that but it seems these 'Projects' are for app developers for developing programs based on C++. For a beginner like me, it's not helping.

I see a lot of people recommending Visual Studio Express for learning C++ but I can't seem to understand the working principles yet. Whenever I create multiple files in Visual Studio and debug them, all those files debug at once and I can't use main function for each and every C++ file as it reports an error.

I need to know how I can seamlessly create, edit and organize my C++ files in Visual Studio 2012. Or, if these are for app developers and enthusiasts, can you suggest me some other IDEs for practicing my codes without going into the complex parts?

like image 690
sangeeth96 Avatar asked Jun 22 '13 14:06

sangeeth96


3 Answers

What I think you are trying to achieve is that you can have multiple small programs in a project. Now while visual studio doesn't allow this with these exact words it is possible to achieve this.

Using this setup:

Solution
    Project
    Project 
    etc.

This is done by going to: File -> new -> Project.. where you initially select the first project you want, for example: an empty project as this is probably what you are looking for. Now in the Solution Explorer you will see your project which allows you to add a new source file: "main.cpp" for example.

When you want to add another "main" you simply right click the solution and add a new project (which can be an empty project or any project).

Now when you want to run the other project you can simply rightclick the project file and select: "Set as startup project".


Possible project layout

like image 117
Floris Velleman Avatar answered Oct 10 '22 17:10

Floris Velleman


To have multiple self compilable single files in a project, you can exclude all other files except one which you want to compile.

Eg. Lets assume this hierarchy:


-Solution1
    -Project1
        -file1.c
        -file2.c

Now if one wants to compile only 'file2.c', thus excluding 'file1.c' from compilation, one can go properties of 'file1.c' and change 'Exclude from Build' field to 'Yes'.

like image 21
sbhal Avatar answered Oct 10 '22 16:10

sbhal


This guy does great videos, hh did a video on getting familiar with Visual Studio: http://thenewboston.org/watch.php?cat=39&number=3 The rest of the videos are for a different programming language but the IDE layout is all the same.

To start a new project: File -> Select C++ on the left hand side - > Console Application Give it a name and click ok. on the right hand side there will be the folder layout, right click src, add new file, select .cpp and give it a name.

This is your main

I hope this is what you mean and that video explains what you need to know!

like image 43
Eduardo Avatar answered Oct 10 '22 17:10

Eduardo