Introduction
It is high time to write first program in Visual Studio. I am going to use C++ in next projects. I want to focus on this language.
In my opinion the best way to learn anything is … practice. Why practice ? There is few advantages:
+ the most important: programming requires practising.
+ possibility to check right level of knowledge,
+ preservation what you know,
+ you start to break boundaries,
+ practice rewards handsomely,
+ you keep your skills and knowledge for longer,
+ challenges which develop,
+ practice also helps in promote self-confidence,
+ chance to see what you have to repeat, focus.
Also you can check more advantages and disadvantages in Internet, but I highly recommend you, especially in programming- practicing. Without this component you can’t become a programmer- really.
Let’s start with first program in Visual Studio 2017
To be able code anything you must create a new project. Follow steps below:
1. Click ‘File’ -> ‘New’-> ‘Project’
2. Then ‘Visual C++’ ->’Windows Console Application’.
3. Call your project and leave in visible place on your PC. You can do it below the list.
4. Click ‘Next’ and wait a moment.
5. Currently you have an opened project.
The task:
In the file ‘liczby.txt’ there are 200 differents integers. Range is from 2 to 1 000 000. Every single number is in a new row.
1. Count how many numbers are less than 1000 and show 2 lasts numbers in the file.
2. Find in the file numbers, which has 18 dividers (including 1 and this number). For every number, show her and write the list of dividers sorted in ascending order.
First of all, we have to include a new library to be able do anything on the file. It is: #include <fstream>. Additionally write #include <iostream> and using namespace std;
You have to have accesibility to file and be able to download, do anything with numbers which there are. In this situation we do:
Then you should use a ‘for’ loop to download and do anything with a number.
Now you can write function to solve the task. Let’s return to first subtask:
1. Count how many numbers are less than 1000 and show 2 lasts numbers in the file.
In my opinion a code should be clear to read by programmer. So in this situation you go out from ‘int main()’ and write new function. There just write necessary lines of code:
Press CTRL +F5. Everything should works:
It is time for the next subtask:
2. Find in the file numbers, which has 18 dividers (including 1 and this number). For every number, show her and write the list of dividers sorted in ascending order.
You will use still this project. You should only add few new lines. I added only new function:
Let’s check results:
Trouble during coding
Obviously, I had a little problem. I couldn’t do second subtask because I had improperly wrote ‘void dividers(int x)’. Initially I wanted to load my dividers to array and display them in terminal. When I wrote, I couldn’t run the program. In my opinion I loaded dividers in wrong way into array. After pondering, I thought about this solution. As you can see-it works.
