More learning the differences…

Decided to start doing small projects to see the differences in how C, C++, C# and Python handle doing the same thing. Started off with a simple enter your name, and then print Hello and the name, since that is one of the more basic things that everyone should know how to do.

// C
#include <stdio.h>
int main()
{
    char name[40];
    printf("Enter your name please: ");
    scanf("%s",name);
    printf("Hello %s.\n", name);
    return 0;
}
Code language: C++ (cpp)
// C++
#include <iostream>
using namespace std;

int main() 
{
    string name;
    cout << "Enter your name please: ";
    cin >> name;
    cout << "Hello " << name << ".\n";
    return 0;
}
Code language: C++ (cpp)
// C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace print_enter
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            Console.Write("Enter your name please: ");
            name = Console.ReadLine();
            Console.WriteLine("Hello {0}.\n", name);
        }
    }
}
Code language: C# (cs)
# Python
username = input("Enter your name please: ")
print("Hello " + username + ".")
Code language: Python (python)

The differences between languages…

I’ve been learning how a few different languages can do the same task, so decided to put up the examples below.

#!/usr/bin/python

number = 5
counter = 1
while (counter <=10):
    if counter < number:
        print (counter, ' is less than ', number)
    elif counter == number:
        print (counter, ' equals ', number)
    else:
        print (counter, ' is greater than ', number)
    counter = counter + 1
Code language: Python (python)
#powershell

$numValue = @(1..10)
$counter = 0

while ($counter -lt $numValue.length){
if ($numValue[$counter] -lt 5){
    write-host "Less than`n"
}
elseif ($numValue[$counter] -eq 5) {
    write-host "Equal`n"
}
else {
    write-host "Greater than`n"
}
$counter += 1
}Code language: PowerShell (powershell)
//C++
#include <iostream>
using namespace std;

int main()
{
    int counter = 1;
    while (counter <= 10) {
        if (counter > 5) { cout << "Greater than\n"; }
        else if (counter == 5) { cout << "Equal\n"; }
        else { cout << "Less than\n"; }
        counter++;
    }
    system("pause>0");
}

Code language: C++ (cpp)

Getting past the basics…

I’ve been watching a C++ tutorial series on Youtube lately, and decided to take some of the basic knowledge that I had of functions, and go into more advanced functions. I was so happy to finally be able to set up a function in an external file, and then pass a variable to that function, and have it be used. Next step will be to manipulate that variable, and pass it back to the main program.

// FunctionExample.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include <iostream>
#include "functions.h"

using std::string;
using std::cout;
using std::cin;
using std::endl;

string name;

int main()
{
    cout << "Please enter your name: ";
    cin >> name;
    function1(name);

    system("pause>0");
}

Code language: C++ (cpp)
//functions.h - This file contains functions declarations for the related cpp file.
#pragma once
int function1(const std::string& name);
Code language: C++ (cpp)
//functions.cpp - User defined functions to be called in the main file.

#include <iostream>
#include "functions.h"

using std::string;
using std::cout;
using std::cin;
using std::endl;

int function1(const string& name)
{
    cout << "Hello " << name << ".";

    return 0;
}
Code language: C++ (cpp)