My Real First Code of My Own in C++

Of course, I had already looked into C++ several times, just like plenty of other languages, but most of my scripts are probably written in these:

  • C#
  • JavaScript / TypeScript
  • Dart

I only tried the others, but I had neither the mental energy nor the motivation to learn them – I had everything in C# anyway, which can do the same as, say, C++ (at least on Windows OS), so no stress . About 2 months ago I programmed something tiny in C++ for the first time – for a friend who studies at VSB, majoring in Information and Communication Technologies. He is in his first year, where they do C++; Java should come in the second year, and networking in the third, which is the reason he went this way.

He sent it to me sometime during the week, and on Sunday I searched Google for tutorial c++ , which are of course available in Czech too. Personally, I went through the course C++ Basics on itnetwork.cz . Then at home I wrote this according to his assignment:

C++
//-For using cout
#include < iostream >
//-For using the string function
#include < locale >
//-For using ofstream
#include < fstream >
//-For dereferencing a string
#include < string >
//-For using ostringstream
#include < sstream >
//-For writing without the std:: prefix
using namespace std;
string intToString(int i) {
ostringstream outputStringStream; //output string stream
outputStringStream <<
i;
return outputStringStream.str();

}
int main(int argc, char * argv[]) {
if (argc != 3) {
cout << "Wrong number of parameters. If you enter a path with spaces, do not forget to use quotes. " <<endl;
} else {
unsigned int start = clock();
string outputFile = argv[2];
ofstream outputFileStream(outputFile.c_str());
if (outputFileStream.is_open()) {
string inputFile = argv[1];
ifstream input(inputFile.c_str());
int number;
while (input >> number) {
//-1 and itself
int countDivisors = 2;
for (int i = 2; i < number; i++) {
if (number % i == 0) {
countDivisors++;

}

}
string line = intToString(number) + * (new string( " ")) + intToString(countDivisors);
cout <<
line <<
endl;
outputFileStream <<
line <<
endl;

}

}
outputFileStream.flush();
outputFileStream.close();
int elapsedMilliseconds = clock() - start;
cin.ignore();
cout << "Apparently it took only " << elapsedMilliseconds << " ms ";
cin.ignore();

}
return 0;

}

It is, of course, a program that takes an input and an output file - the input file has 1 number on each line, the program calculates how many numbers it is divisible by without a remainder and writes this information to the output file and to the console. This was basically just 1 .cpp and .h file. Then he sent me another assignment, but I never sent that one back, because I had decided I was quitting programming of any kind for good. Now I have come back to it; I cannot live without it.

It is a big part of my life. He sent it to me again yesterday, but unfortunately I did almost nothing on it, because in a few minutes I was heading out to my choir for a concert. I wrote to him on Messenger and went out. At home I did nothing either, because I quickly fell asleep, woke up at almost 10 p.m. and then went back to sleep so I would not stay up late.

But since I want to learn this as well, and not send him a project with just one .cpp file, I used my most frequently used method, trial and error, to learn static methods, classes, including my own header files, the Visual Studio "stdafx.h" or ones from the global C++ library, constructors and a few other small things.

With this foundation I managed to compile a project made of four of my own *.cpp files, of course with the same number of *.h files. I have to say that if I did not know C#, I am not sure I would have managed it =D. This is the first one, and I hope hundreds more will follow on the topic of C++. If you are also starting out with "C plus plus" and know C# best, I will try to describe how some things work and how they differ from .NET - so not just the language syntax, which with a bit of imagination could be swapped; here it is primarily about the philosophy behind it. It will help my friend too.

I write code in Visual Studio 2015 Community. My friend uses Code::Blocks, which is good too, but compared to VS it falls short, especially in the languages supported by MS itself. For me, Code::Blocks runs just as fast as VS, which is miles ahead of other IDEs. I will try to post complete solutions here, primarily for VS, but when possible also for other IDEs, mainly the mentioned Code:: :).

Categories

Leave a Reply