C++

Array of Structures


#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>

struct teacher{
char department[25];
char id[10];
char name[25];
char add[50];
}teachers[50];
char id[10];
int nrec=0;
int mrec=0;
int gotcha=0;
void insert()

{
    char x;
do{
    if (nrec<=49)
{
    clrscr();
    cout<<"Adding data...\t\tRecord #;"<<nrec+1<<endl<<endl<<endl;
    cout<<"ID number:";
    cin.getline(teachers[nrec].id,10);
    cout<<"\nFull name:";
    cin.getline(teachers[nrec].name,25);
    cout<<"\nMailing address:";
    cin.getline(teachers[nrec].add,50);
    cout<<"\nDepartment:";
    cin.getline(teachers[nrec].department,25);
    nrec++;
}
    else
{
    clrscr();
    cout<<"\n\nNo more space available..";
    getch();
    return;
}
}    while(x=='y');
}


    void display()
{
    clrscr();
    for(int y=0,w=1;y<nrec;y++,w++)
{
    cout<<"Record #:"<<w<<endl<<endl;
    cout<<"ID number:"<<teachers[y].id<<endl;
    cout<<"Full name:"<<teachers[y].name<<endl;
    cout<<"Mailing address:"<<teachers[y].add<<endl;
    cout<<"Department:"<<teachers[y].department<<endl<<endl;
    delay(1500);
}
    getch();
}
    void edit()
{do
{
       clrscr();
       cout<<"\nEnter the ID number you wanted to edit: ";
       cin.getline(id,10);
}while(strcmp(id,"")==0);
       for(int y=0;y<=49; y++){
    if(strcmp(teachers[y].id,id)==0)
{
    clrscr();

    cout<<"Info no. "<<y+1;
    cout<<"\n\nEnter the new ID number: ";
    cin.getline(teachers[y].id,10);
    cout<<"\n\nEnter the new Full Name: ";
    cin.getline(teachers[y].name,25);
    cout<<"\n\nEnter the new Mailing address: ";
    cin.getline(teachers[y].add,25);
    cout<<"\n\nEnter the new Department. . .";
    cin.getline(teachers[y].department,25);
    cout<<"\n\nSuccessfully edited. . .";
    getch();gotcha++;
    }

    }
    if(gotcha==0){cout<<"Did not found. . .Please try again";getch();}
    }


    void search(){
    do{
    textcolor(CYAN);
    textbackground(BLUE);
    clrscr();
    cout<<"\nEnter the ID number you wanted to search: ";
    cin.getline(id,10);
}while(strcmp(id,"")==0);
    for(int x=0; x<10;x++){
        if(strcmp(teachers[x].id,id)==0)
        {
        clrscr();
        cout<<"Info no. "<<x+1;
        cout<<"\n\nID number: ";
        cout<<teachers[x].id;
        cout<<"\n\nFull name: ";
        cout<<teachers[x].name;
        cout<<"\n\nMailing address: ";
        cout<<teachers[x].add;
        cout<<"\n\nDepartment: ";
        cout<<teachers[x].department;
        cout<<"\n\n\n\nSuccessfully search...";
        getch();gotcha++;
        }
}
    if(gotcha==0){cout<<"Did not found...Please try again";getch();}
}



    void deleter(){
do{
    textcolor(BLACK);
    textbackground(GREEN);
    clrscr();
    cout<<"\nEnter the ID number you wanted to delete: ";
    cin.getline(id,10);
}while(strcmp(id,"")==0);
    for(int x=0; x<10;x++){
    if(strcmp(teachers[x].id,id)==0)
{
    clrscr();
    cout<<"Info no. "<<x+1;

    strcpy(teachers[x].id,"");

    strcpy(teachers[x].name,"");

    strcpy(teachers[x].add,"");

    strcpy(teachers[x].department,"");
    cout<<"\n\n\nSuccessfully deleted...";
    getch();gotcha++;
}
}
    if(gotcha==0){cout<<"Did not found...Please try again";getch();}
}


    int main()
{
    char choice;
    textcolor(CYAN);
do{
    clrscr();
    cout<<"\n\n\t\tplease make a selection..-->"<<endl<<endl;
    cout<<"\n\n\t\t[1]Insert"<<endl;
    cout<<"\n\n\t\t[2]Display"<<endl;
    cout<<"\n\n\t\t[3]Delete"<<endl;
    cout<<"\n\n\t\t[4]Edit"<<endl;
    cout<<"\n\n\t\t[5]Search"<<endl;
    cout<<"\n\n\t\t[6]Exit"<<endl;
    choice=getche();
    switch(choice)

{
case '1':
    insert();
    break;

case '2':
    display();
    break;

case '3':
    deleter();
    break;

case  '4':
    edit();
    break;

case  '5':
    search();
    break;

case  '6':
    cout<<"\n\n\n\n\n\n\nGoodbye..";
    break;

default:
    cout<<"\n\n\n\n\n\n\nTry again..";
    getch();
    break;

}
}while    (choice!='6');
    getch();
    return 0;
}

Calculator

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int choice,input1,input2,a1,a2,a3,a4;
do
{
clrscr();
printf("  **MENU**  \n");
printf("[1] Addition\n");
printf("[2] Subtraction\n");
printf("[3] Multiplication\n");
printf("[4] Division\n");
printf("[5] Quit\n\n");
printf("Enter your choice:");
scanf("%d",&choice);
if(choice==1)
{
printf("\n Enter input 1:");
scanf("%d",&input1);
printf("\n Enter input 2:");
scanf("%d",&input2);
a1=input1+input2;
printf("the sum of %d and %d is %d",input1,input2,a1);
getch();
}
if(choice==2)
{
printf("\n Enter input 1:");
scanf("%d",&input1);
printf("\n Enter input 2:");
scanf("%d",&input2);
a2=input1-input2;
printf("the difference of %d and %d is %d",input1,input2,a2);
getch();
}
if(choice==3)
{
printf("\n Enter input 1:");
scanf("%d",&input1);
printf("\n Enter input 2:");
scanf("%d",&input2);
a3=input1*input2;
printf("the product of %d and %d is %d",input1,input2,a3);
getch();
}
if(choice==4)
{
printf("\n Enter input 1:");
scanf("%d",&input1);
printf("\n Enter input 2:");
scanf("%d",&input2);
a4=input1/input2;
printf("the quotient of %d and %d is %d",input1,input2,a4);
getch();
}
}while(choice!=5);
printf("\n\tHAVE A NICE DAY!!!");
getch();
}


Structures 

#include <iostream.h>
#include <conio.h>
#include <graphics.h>

struct teacher{
char id[10];
char name[25];
char add[15];
char department[25];
}jane,rick,Uno,Dos,Tres;

    void insert()

    {
    clrscr();
    cout<<"Adding data for Jane... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(jane.id,9);
    cout<<"\nFull Name:";
    cin.getline(jane.name,25);
    cout<<"\nMailing Address:";
    cin.getline(jane.add,50);
    cout<<"\nDepartment:";
    cin.getline(jane.department,25);

    clrscr();
    cout<<"Adding data for Rick... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(rick.id,9);
    cout<<"\nFull Name:";
    cin.getline(rick.name,25);
    cout<<"\nMailing Address:";
    cin.getline(rick.add,50);
    cout<<"\nDepartment:";
    cin.getline(rick.department,25);

    clrscr();
    cout<<"Adding data for Uno... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Uno.id,9);
    cout<<"\nFull Name:";
    cin.getline(Uno.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Uno.add,50);
    cout<<"\nDepartment:";
    cin.getline(Uno.department,25);

    clrscr();
    cout<<"Adding data for Dos... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Dos.id,9);
    cout<<"\nFull Name:";
    cin.getline(Dos.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Dos.add,50);
    cout<<"\nDepartment:";
    cin.getline(Dos.department,25);

    clrscr();
    cout<<"Adding data for Tres... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Tres.id,9);
    cout<<"\nFull Name:";
    cin.getline(Tres.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Tres.add,50);
    cout<<"\nDepartment:";
    cin.getline(Tres.department,25);

    }
    void display()
    {
    clrscr();
    cout<<"ID Number:"<<jane.id<<endl;
    cout<<"Full Name:"<<jane.name<<endl;
    cout<<"Mailing Address:"<<jane.add<<endl;
    cout<<"Department:"<<jane.department<<endl<<endl;

    cout<<"ID Number: "<<rick.id<<endl;
    cout<<"Full Name: "<<rick.name<<endl;
    cout<<"Mailing Address:"<<rick.add<<endl;
    cout<<"Department:"<<rick.department<<endl;

    cout<<"ID Number:"<<Uno.id<<endl;
    cout<<"Full Name:"<<Uno.name<<endl;
    cout<<"Mailing Address:"<<Uno.add<<endl;
    cout<<"Department:"<<Uno.department<<endl<<endl;

    cout<<"ID Number: "<<Dos.id<<endl;
    cout<<"Full Name: "<<Dos.name<<endl;
    cout<<"Mailing Address:"<<Dos.add<<endl;
    cout<<"Department:"<<Dos.department<<endl;

    cout<<"ID Number: "<<Tres.id<<endl;
    cout<<"Full Name: "<<Tres.name<<endl;
    cout<<"Mailing Address:"<<Tres.add<<endl;
    cout<<"Department:"<<Tres.department<<endl;

    getch();}

    void edit()
        {
    clrscr();
    cout<<"Adding data for Jane... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(jane.id,9);
    cout<<"\nFull Name:";
    cin.getline(jane.name,25);
    cout<<"\nMailing Address:";
    cin.getline(jane.add,50);
    cout<<"\nDepartment:";
    cin.getline(jane.department,25);

    clrscr();
    cout<<"Adding data for Rick... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(rick.id,9);
    cout<<"\nFull Name:";
    cin.getline(rick.name,25);
    cout<<"\nMailing Address:";
    cin.getline(rick.add,50);
    cout<<"\nDepartment:";
    cin.getline(rick.department,25);

    clrscr();
    cout<<"Adding data for Uno... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Uno.id,9);
    cout<<"\nFull Name:";
    cin.getline(Uno.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Uno.add,50);
    cout<<"\nDepartment:";
    cin.getline(Uno.department,25);

    clrscr();
    cout<<"Adding data for Dos... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Dos.id,9);
    cout<<"\nFull Name:";
    cin.getline(Dos.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Dos.add,50);
    cout<<"\nDepartment:";
    cin.getline(Dos.department,25);

    clrscr();
    cout<<"Adding data for Tres... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Tres.id,9);
    cout<<"\nFull Name:";
    cin.getline(Tres.name,25);
    cout<<"\nMailing Address:";
    cin.getline(Tres.add,50);
    cout<<"\nDepartment:";
    cin.getline(Tres.department,25);

    }


    void del()
        {
    clrscr();
    cout<<"Adding data for Jane... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(jane.id,0);
    cout<<"\nFull Name:";
    cin.getline(jane.name,0);
    cout<<"\nMailing Address:";
    cin.getline(jane.add,0);
    cout<<"\nDepartment:";
    cin.getline(jane.department,0);

    clrscr();
    cout<<"Adding data for Rick... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(rick.id,0);
    cout<<"\nFull Name:";
    cin.getline(rick.name,0);
    cout<<"\nMailing Address:";
    cin.getline(rick.add,0);
    cout<<"\nDepartment:";
    cin.getline(rick.department,0);

    clrscr();
    cout<<"Adding data for Uno... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Uno.id,0);
    cout<<"\nFull Name:";
    cin.getline(Uno.name,0);
    cout<<"\nMailing Address:";
    cin.getline(Uno.add,0);
    cout<<"\nDepartment:";
    cin.getline(Uno.department,0);

    clrscr();
    cout<<"Adding data for Dos... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Dos.id,0);
    cout<<"\nFull Name:";
    cin.getline(Dos.name,0);
    cout<<"\nMailing Address:";
    cin.getline(Dos.add,0);
    cout<<"\nDepartment:";
    cin.getline(Dos.department,0);

    clrscr();
    cout<<"Adding data for Tres... "<<endl<<endl;
    cout<<"ID Number:";
    cin.getline(Tres.id,0);
    cout<<"\nFull Name:";
    cin.getline(Tres.name,0);
    cout<<"\nMailing Address:";
    cin.getline(Tres.add,0);
    cout<<"\nDepartment:";
    cin.getline(Tres.department,0);

    }







       void search()

    {
    char f;
    int g;

    do{
    clrscr();
     cout<<"\na)jane";
     cout<<"\nb)rick";
     cout<<"\nc)Uno";
     cout<<"\nd)Dos";
     cout<<"\ne)Tres";
     cout<<"\nf>exit";
     cout<<"\nSearch:\n";
     cin>>f;
    {
    if(f=='a')
    cout<<"\nID Number:"<<jane.id<<endl;
    cout<<"\nFull Name:"<<jane.name<<endl;
    cout<<"\nMailing Address:"<<jane.add<<endl;
    cout<<"\nDepartment:"<<jane.department<<endl<<endl;
    }break;
    {
    break;
    if(f=='b')
    cout<<"\nID Number:"<<rick.id;
    cout<<"\nFull Name: "<<rick.name<<endl;
    cout<<"\nMailing Address:"<<rick.add<<endl;
    cout<<"\nDepartment:"<<rick.department<<endl;
    }
    {if(f=='c')
    cout<<"\nID Number:"<<Uno.id<<endl;
    cout<<"Full Name:"<<Uno.name<<endl;
    cout<<"Mailing Address:"<<Uno.add<<endl;
    cout<<"Department:"<<Uno.department<<endl<<endl;
    }      break;
    {if(f=='d')
    cout<<"ID Number: "<<Dos.id<<endl;
    cout<<"Full Name: "<<Dos.name<<endl;
    cout<<"Mailing Address:"<<Dos.add<<endl;
    cout<<"Department:"<<Dos.department<<endl;
    }       break;
    {if (f=='e')
    cout<<"ID Number: "<<Tres.id<<endl;
    cout<<"Full Name: "<<Tres.name<<endl;
    cout<<"Mailing Address:"<<Tres.add<<endl;
    cout<<"Department:"<<Tres.department<<endl;
    break;
    }


       } while(f!='g');
    getch();

       }

    void main()
    {
    char choice;
    textcolor(YELLOW);
    textbackground(BLACK);
    do{
    clrscr();
    cout<<"Please make a selection.. --> "<<endl<<endl;
    cout<<"\t[1] Insert" <<endl;
    cout<<"\t[2] Display"<<endl;
    cout<<"\t[3] Edit"<<endl;
    cout<<"\t[4] search"<<endl;
    cout<<"\t[5] delete"<<endl;
    cout<<"\t[6] exit"<<endl;
    gotoxy(32,1);choice=getche();

        switch(choice)
        {
        case '1':
            insert();
            break;
        case '2':
            display();
            break;
        case '6':
            cout<<"\n\n\n\n\n\n\n\n\n\nGoodbye..";
            break;
        case '3':
            edit();
            break;
        case '4':
            del();
            break;
        case '5':
            search();
            break;
        default:
            cout<<"\n\n\n\n\n\n\n\n\n\n\Try again..";
            getch();
            break;

        }
    }while(choice!='6');
    getch();
    }