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 "C
onsole 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