Visual Studio 2017 not working

I just started programming C++. When I tried the shortcuts it didn't do anything. When I try to run or debug the code, the option to do so is grayed out. I don't know what to do. I've checked my code and I can't find any problems. Please help.

The code I wrote:
#include <iostream>
using namespace std;

int main()
{
int sum = 1 + 1;
cout<< sum;
return 0;


}
Hello MondayPenguin,

Welcome to the forum.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

I to use VS, the 2015 version, and will have to start with the question did you create a single ".cpp" or a project?

I am not sure how to create a single ".cpp" file and get it to compile. I always end up creating an empty project and adding the files I need to the project.

To create an empty project start with what you need to start the "new project wizard". That could be from the menu names "File -> new -> project" or "Ctrl + Shift + 'N'". I even put a button on the tool bar as a shortcut. Where it says "Name:" enter the name of the project. Most times I will end this name with " EP" to let me know it is an empty project. This part is not necessary. Next to "Solution Name:" you can either leave this the same as the project name or change it. For you can use what is there or change the location where the project will be stored. If changed the next time this will have the same path as the last time. The three text boxes are the only things that need changed for now, When done press OK or hit Enter.

On the next window that comes up press on "Next". It will likely be the active button.

For the last window you may need to click on the radio button for "Console Application". Under that it says "Additional options:". The first thing is a check box for "Empty project". Check this box and leave everything else alone for now. Click on "Finish" or press "Enter" when done.

When the window disappears you are left with an empty IDE. At this point you will need to add the files you need to the project. On the menu "Project -> Add New Item" or I usually use "Ctrl + Shift + 'A'" and again I put a button on the tool bar.
When the "Add New Item" window comes up yo will have the choice of ".cpp" or ".h". Click on what you need. The ".cpp" appears to be a default. Where it says "Name:" type in the file name that you want. The "location:" is already set for you and best not to change. Keep in mind where this path is and how to find it because someday you will create a text file for your program to read and that file will need to be saved to this same path for the program can find the file. When you click on "Add" or press "Enter" you will have a blank file to put your code in.

I do not know if you need all of this, but I hope some would be helpful.

When I created a project I was able to compile and run the program. I realize that you are new to all this, so in the following code I made some changes that will be helpful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>

//using namespace std;

int main()
{
	int sum = 1 + 1;

	std::cout << sum << std::endl;
	
	// The next line may not be needed. If you have to press "enter" to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

The last to and sometimes three line above return are there to keep the console window open before "return" ends the program and closes the window. I added the "std::endl" to your "cout". A habit you should get use to, but it is not always needed. Sometimes just "\n" will work and save the "endl" for the last line.

Also a couple of blank lines will break up the code and make it easier to read along with proper indenting.

Any other questions let me know.

Hope that helps,

Andy

Edit: spelling
Last edited on
closed account (E0p9LyTq)
@Handy Andy,

Visual Studio 2017 offers mode switches to use C++14, C++17 or C++ latest as the supported language versions. With C++14 as the default standard.

C++14 and later are inclusive of C++11, with several exceptions of language features being deprecated and/or removed (std::random_shuffle for example), VS simply doesn't have a switch to use C++11 (or earlier) language standard.
@FurryGuy,

Thank you for the tips. Some day when I actually get 2017 working this is nice to know.

Andy
closed account (E0p9LyTq)
When I tried the shortcuts it didn't do anything.

What shortcuts? The shortcuts associated with various menu items?

When I try to run or debug the code, the option to do so is grayed out.

You are likely editing a source code file without it being part of a solution/project. Visual Studio does a lot of the "house keeping" of managing the resources needed to compile source code.

A project is the framework for managing all the files and resources for a single program, a solution manages one or more projects.

Visual Studio is an integrated development environment (IDE) and learning how to use it can take some time and be frustrating in the beginning.

Here's a tutorial how to create a simple C++ console app using VS2017.
https://tutorials.visualstudio.com/cpp-console/intro

closed account (E0p9LyTq)
@MondayPenguin,

PLEASE learn to use code tags, it makes reading your source code much easier.
http://www.cplusplus.com/articles/jEywvCM9/

You can go back as edit your code to add the code tags.

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
   int sum = 1 + 1;

   std::cout << sum << '\n';
}

With the resulting output:
2

I use Visual Studio 2017 Community as one of the compilers I use.
Topic archived. No new replies allowed.