#include<stdarg.h>
#include<GL/glut.h>
#include<stdio.h>
GLfloat x=0.5;
GLint submenu1,submenu2;
int count1=0,count2=0;

void stroke_output(GLfloat x, GLfloat y, char *format,...)
{
	va_list args;
	char buffer[200], *p;
	va_start(args, format);
	vsprintf(buffer, format, args);
	va_end(args);
	glPushMatrix();
	glTranslatef(-3.2, y, 0);
	glScaled(0.003, 0.002, 0.000);
	for (p = buffer; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
	glPopMatrix();
}
void d4()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.2,0.4,0.3,1.0);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-13.0f);
	stroke_output(-2.0, 3.0, "     INSTRUCTIONS     ");
stroke_output(-2.0, 2.5, "       Keyboard interactions :       ");
	stroke_output(-2.0, 2.0, "       'q' or 'Q' :Quit       ");
	stroke_output(-2.0, 1.5, "       'b' :Fly ");
	stroke_output(-2.0,1.0, "       'B' :No fly");
stroke_output(-2.0, 0.5, "       Mouse Interactions:       ");
stroke_output(-2.0, 0.0, "       Right click-Exit : End      ");
stroke_output(-2.0,-0.5, "       Right click-Flying Ball : Flying Ball Control    ");
stroke_output(-2.0, -1.0, "       Right click-color : Change Background color      ");
	stroke_output(-2.0,-2.0, "     Press 'b' for next ");
	// Display instructions
	glFlush();
	glutSwapBuffers();
}

void d6()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0,0.0,1.0,1.0);
	glFlush();
	glutSwapBuffers();
}
void d7()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.0,1.0,1.0,1.0);
	glFlush();
	glutSwapBuffers();
}
void d8()
{

	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.3,1.5,0.5,1.0);
	glFlush();
	glutSwapBuffers();
}

void flying(double ang)
{
	glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glTranslatef(0.0,-0.5,-13.0);
        glRotatef(115,1.0,0.0,0.0);

	 
	glPushMatrix();
	glRotatef(ang,0.0,0.0,1.0);
	glScaled(2.9,0.2,0.1);
	glTranslatef(0.0,0.0,10.0);
        glColor3f(0.5,0.0,0.0);
	glRotatef(ang,0.0,0.0,1.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();
	
	
	glPushMatrix();
        glColor3f(0.0,0.0,1.0);
	glRotatef(ang,0.0,0.0,1.0);
	glScaled(0.2,2.9,0.1);
	glTranslatef(0.0,0.0,10.0);
	glRotatef(ang,0.0,0.0,1.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();
        
        glPushMatrix();
	glRotatef(ang+50,0.0,0.0,1.0);
	glScaled(2.9,0.2,0.1);
	glTranslatef(0.0,0.0,10.0);
	glRotatef(ang+50,0.0,0.0,1.0);
        glColor3f(1.0,0.0,0.0);
	glutSolidSphere(0.7,20,60);
	glPopMatrix();

        glPushMatrix();
	glRotated(ang,0.0,1.0,0.0);
	glTranslatef(0.05,-4.0,1.0);
	glRotated(ang,0.0,1.0,0.0);
	glutSolidSphere(0.3,20,60);
	glPopMatrix();
 
        glPushMatrix();
	glColor3f(1.5,0.5,1.5);
	glRotated(ang,0.0,1.0,0.0);
	glTranslatef(0.0,1.6,0.0);
	glRotated(ang,0.1,3.0,0.0);
	glutSolidSphere(0.15,20,60);
	glPopMatrix();
	glFlush();
	glutSwapBuffers();


      
 }
 void fly()
{
	x+=6;
   flying(x);
}
void slow()
{
  x+=0.03;
 flying(x);
}
void fast()
{
  x+=15;
 flying(x);
}
 void fly1()
{
	x+=8;
   flying(x);
}
 void fly2()
{
	x+=10;
   flying(x);
}
 void fly3()
{
	x+=12;
   flying(x);
}
 void fly4()
{
	x+=14;
   flying(x);
}

 void flyd()
{
	x+=6;
   flying(x);
}
 void flyd1()
{
	x+=7;
   flying(x);
}
 void flyd2()
{
	x+=8;
 
   flying(x);
}
 void flyd3()
{
	x+=9;
   flying(x);
}
 void flyd4()
{
	x+=11;
   flying(x);
}


 void nofly()
 {
	flying(0);
 }


 void Init() 
 {
   glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30.0,(GLfloat)500/(GLfloat)500,1.0,200.0);    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 void Display()
 {
	glClear(GL_COLOR_BUFFER_BIT );
	glColor3f(1.5,0.5,1.9);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-13.0f);
	stroke_output(-2.0, 3.0, "     * WEL COME *   ");
	stroke_output(-2.0, 2.5, "         To    ");
	stroke_output(-2.0, 2.0, "    CGOOP MINIPROJECT ");
	stroke_output(-2.0, 1.5, "Project Name: FLYING BALL ");
	stroke_output(-2.0, 1.0, "Created By:   ");
	stroke_output(-2.0, 0.5, "      Dipali Bhabad 5");
	stroke_output(-2.0, -0.1, "      Surabhi Kadam 24");
	stroke_output(-2.0, -0.6, "      Tejal Malode");
	stroke_output(-2.0, -1.2, "      Girija Shinde 61 ");
	stroke_output(-2.0, -1.7, "  Press '2' for next ");
	// Display information about the miniproject and its creators
	glFlush();
	glutSwapBuffers();
    
 }
  void Menu(int id)
  {
	switch(id)
	{
	case 1:exit(0);
		break;
	}
 }
 void flying_ball(int id)
{
 switch(id)
  {
    case 2:glutIdleFunc(fly);
            glutPostRedisplay();
		break;
	case 3:glutIdleFunc(nofly);
            glutPostRedisplay();
	        break;
	case 0:glutIdleFunc(slow);
            glutPostRedisplay();
	        break;
	case 9:glutIdleFunc(fast);
            glutPostRedisplay();
	        break;
  }
}

 void Color(int id)
{
 switch(id)
  {
        case 4:glutIdleFunc(d6);
                glutPostRedisplay();
	        break;
        case 5:glutIdleFunc(d7);
	        glutPostRedisplay();
                break;
		case 6: glutIdleFunc(d8);                
                glutPostRedisplay();
	        break;
       glutSwapBuffers();
 }

}
void mykey(unsigned char key,int x,int y)
// callback for the current window
{
	
	if(key=='q'||key=='Q')
	{
		exit(0);
		//terminates the program
	}
      
	if(key=='2')
	{
	      glutIdleFunc(d4);
	      // takes the user to instruction page
	}
	
	if(key=='b')
	{
		glutIdleFunc(fly);
// object fly
	}
	if(key=='B')
	{
		glutIdleFunc(nofly);
//object does not fly
	}
	 if(key=='i'||key=='I')
	{	
		count1=count1+1;
		if(count1==1)
		glutIdleFunc(fly1);
		if(count1==2)
		glutIdleFunc(fly2);
		if(count1==3)
		glutIdleFunc(fly3);
		if(count1==4)
		glutIdleFunc(fly4);
// increases the speed of fan
    }
    if(key=='d'||key=='D')
	{	
		count1=count1-1;
		if(count1==4)
		glutIdleFunc(flyd4);
		if(count1==3)
		glutIdleFunc(flyd3);
		if(count1==2)
		glutIdleFunc(flyd2);
		if(count1==1)
		glutIdleFunc(flyd1);
		// decreases the speed of fan
	}     
}

int main(int argc, char *argv[]) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Flying ball");
    glutDisplayFunc(Display);   
    glutKeyboardFunc(mykey);
   submenu1= glutCreateMenu(flying_ball);
                 glutAddMenuEntry("fly",2);
                 glutAddMenuEntry("nofly",3);
	          glutAddMenuEntry("slow",0);
                  glutAddMenuEntry("fast",9);
    submenu2=glutCreateMenu(Color);
               glutAddMenuEntry("pink",4);
               glutAddMenuEntry("blue",5);
               glutAddMenuEntry("green",6);
    glutCreateMenu(Menu);
    glutAddMenuEntry("Exit",1);
    glutAddSubMenu("flying_ball",submenu1);
    glutAddSubMenu("Color",submenu2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    Init();
    glutMainLoop();
}
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}