Q:

Unable to get trivial 2D shader to draw red triangle in C++ openGL (No compiler errors)

I used openGL, GLEW and GLFW3. In openGl 3.0 mesa, the default shader was used and a white triangle was drawn. In 4.2, the screen was left black. No error messages were generated.
The issue is not due to mislabeling of a fragment shader as a vertex shader or vice versa.
The program contains 3 functions:
(1) compileShader, which is supposed to take in shader type and source code as std::string and return a shader ID with compiled code.
(2) createProgram, takes in the source code of a vertex shader and a fragment shader and returns a program ID with the shaders compiled and attached.
(3) And main, where both shader source codes are defined as strings.
Sorry and Thank you.
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <string>

static unsigned int compileShader(unsigned int type, std::string const& sourceCode){
    unsigned int shaderId = glCreateShader(type);
    const char* source = sourceCode.c_str();
    glShaderSource(shaderId, 1, &source, nullptr);
    glCompileShader(shaderId);

    int result;
    glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE){

        if (type == GL_FRAGMENT_SHADER) std::cout<<"Fragment\n";
        else std::cout<<"Vertex";
        std::cout<<"SHADER COMPILATION FAILED!"<<std::endl;
        int logLength;
        glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
        char* infoLog = new char[logLength];
        glGetShaderInfoLog(shaderId, logLength, NULL, infoLog);
        std::cout<<infoLog<<std::endl;
        delete[] infoLog;

        glDeleteShader(shaderId);
        return 0;
    }

    return shaderId;
}

static unsigned int createProgram(std::string const& vertexCode, std::string const& fragmentCode){
    unsigned int vsId = compileShader(GL_VERTEX_SHADER, vertexCode);
    unsigned int fsId = compileShader(GL_FRAGMENT_SHADER, fragmentCode);

    unsigned int programId = glCreateProgram();
    glAttachShader(programId, vsId);
    glAttachShader(programId, fsId);
    glLinkProgram(programId);
    glValidateProgram(programId);

    glDeleteShader(vsId);
    glDeleteShader(fsId);
    return programId;
}

int main()
{

    GLFWwindow* window;

    if (!glfwInit())
    {
        std::cout<<"GLFW initialization failed";
        return -1;
    }

    ///switches to opengl 4.2!
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    glewInit();

    std::cout << "OpenGL " << glGetString(GL_VERSION) << "\n" << std::endl;
    float positions[6] = {
        0.5f, 0.5f,
        -0.5f, 0.5f,
        0.0f, 0.0f
    };

    unsigned int buffer = 1;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);                                        //enables the xy attrib of position
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); //specifies layout of xy attrib

    std::string vertexSource =   R"(
#version 330 core
layout(location=0) in vec4 position;

void main(){
    gl_Position = position;
}
)";
    std::string fragmentSource = R"(
#version 330 core

layout(location=0) out vec4 color;

void main(){
    color = vec4(1.0, 0.0, 0.0, 1.0);
}
)";

    unsigned int programId = createProgram(vertexSource, fragmentSource);
    glUseProgram(programId);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

A:

The issue is not the shader program, but you are using a core profile OpenGL Context (GLFW_OPENGL_CORE_PROFILE). Thus you have to create a named Vertex Array Object. The default VAO (0) is not valid in a core profile context.
Generate vertex array object name by glGenVertexArrays and create and bind the object by glBindVertexArray: 
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);                                        //enables the xy attrib of position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); //specifies layout of xy attrib

