Understanding Structs in C++ (USC)

Substance:

  1. Understanding Structure
  2. Structure Declaration
  3. Accessing Struct Elements
  4. Structure Within Structure
  5. Array Structure


Structures In C++

1. Understanding Structure

Structure is a data type that can store a number of data that have different data types. The variables that make up the Structure are called structure elements.

2. Declaration of Struct (Structure)

The structure can be declared as follows:

struct nama_struktur {
elemen1;
elemen2;
};

Or it could be like this:

typedef struct {
elemen1;
elemen2;
} nama_struct;

Example of structure declaration:

struct Buku {
char kode_buku[10];
char judul[50];
char isbn[20];
char pengarang[50];
int berat;
};

3. Accessing Struct Elements

Accessing struct elements is done individually by specifying the name of the structure variable followed by a dot ".". Consider the following example:

buku = Buku;
cout << buku.kode_buku;
cout << buku.judul;

Example:

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

float get_nilai(float a, float b) {
return (a + b) / 2;
}

void main() {
// definisi struktur
struct Mahasiswa {
char nim[10];
char nama[30];
float uts;
float uas;
};
Mahasiswa mhs;

// entri data
cout << "Masukkan NIM : ";
cin.getline(mhs.nim, 10);
cout << "Masukkan Nama : ";
cin.getline(mhs.nama, 30);
cout << "UTS : ";
cin >> mhs.uts;
cout << "UAS : ";
cin >> mhs.uas;

// tampil data
cout << "\nNIM : " << mhs.nim;
cout << "\nNama : " << mhs.nama;
cout << "\nNilai : " << get_nilai(mhs.uts, mhs.uas);
getch();
}

Output:

Enter NIM: 12345676
Enter Name: Edomaru
UTS: 89
UAS: 93

NIM: 12345676
Name: Edomaru
Score: 91

4. Structure Within Structure

Structure within a Structure means an element that is inside a structure of another structure data type. Consider the following example:

struct Mahasiswa {
char nim[10];
char nama[30];
};

struct Nilai {
float uts; float uas;
};

struct NilaiMahasiswa {
Mahasiswa mhs;
Nilai nilai;
};

Example:

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

float get_nilai(float a, float b) {
return (a + b) / 2;
}

void main() {
// definisi struktur
struct Mahasiswa {
char nim[10];
char nama[30];
};

struct Nilai {
float uts;
float uas;
};

struct NilaiMahasiswa {
Mahasiswa mhs;
Nilai nilai;
};

NilaiMahasiswa nm;

// entri data
cout << "Masukkan NIM : ";
cin.getline(nm.mhs.nim, 10);
cout << "Masukkan Nama : ";
cin.getline(nm.mhs.nama, 30);
cout << "UTS : ";
cin >> nm.nilai.uts;
cout << "UAS : ";
cin >> nm.nilai.uas;

// tampil data
cout << "\nNIM : " << nm.mhs.nim;
cout << "\nNama : " << nm.mhs.nama;
cout << "\nNilai : " << get_nilai(nm.nilai.uts, nm.nilai.uas);
getch();
}

Output:

Enter NIM: 12345786
Enter Name: Dee Chozo
UTS: 90
UAS: 70

NIM: 12345786
Name: Dee Chozo
Score: 80

5. Array Structure

Structures can also be combined with arrays, consider the following example:

struct Mahasiswa {
char nim[10];
char nama[30];
float uts;
float uas;
}; Mahasiswa mhs[10];

Example:

#include <iostream.h>
#include <conio.h>
#define MAX 5

float get_nilai(float a, float b) {
return (a + b) / 2;
}

char get_huruf(float nilai) {
char huruf;
if (nilai > 85) huruf = 'A';
else if (nilai > 70) huruf = 'B';
else if (nilai > 50) huruf = 'C';
else if (nilai > 40) huruf = 'D';
else huruf = 'E';
return huruf;
}

void main() {
// definisi struktur
struct Mahasiswa {
char nim[10];
char nama[30];
float uts;
float uas; };
Mahasiswa mhs[MAX];

// entri data
for (int i=0; i < MAX; i++) {
clrscr();
cout << "Mahasiswa ke " << i + 1 << endl;
cout << "Masukkan NIM : ";
cin.getline(mhs[i].nim, 10);
cout << "Masukkan Nama : ";
cin.getline(mhs[i].nama, 30);
cout << "UTS : ";
cin >> mhs[i].uts;
cout << "UAS : ";
cin >> mhs[i].uas;
}

// tampil data
for (int i=0; i < MAX; i++) {
float n_akhir = get_nilai(mhs[i].uts, mhs[i].uas);
cout << "\nNIM : " << mhs[i].nim;
cout << "\nNama : " << mhs[i].nama;
cout << "\nNilai : " << n_akhir;
cout << "\nGrade : " << get_huruf(n_akhir);
}

getch();
}

Source:

System Programming Practical Module. STMIK El Rahma Yogyakarta. By  Eding Muh. Saprudin, S.Kom

C++ Student Data Structure Example

Struct data type is a data type that can also be called a group, which consists of several different data types grouped into one struct/group name. The data elements are known as members/elements, with different types and/or lengths.

Tricks for calling struct programs:

  1. The first way to call a structure program so that it can be recognized is to put it as a global variable, namely by placing it outside void main().
  2. The second way is to define a struct variable into a function either when declaring it or when calling it, for example:
input(myBook);
clrscr();
output(myBook);

To learn more about structs in C++, the basic points you need to understand are:

  1. Understanding Structure
  2. Structure Declaration
  3. Accessing Struct Elements
  4. Structure Within Structure
  5. Array Structure

Meanwhile, in this post I just share my experience using structs for student assessment, here is the source code:

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

/**
*bundet.com
*Wawan Beneran
*C++ Struct Mahasiswa
*/

float get_nilai(float a, float b) {
return (a + b) / 2;
}

void main()
{
// definisi struktur
struct Mahasiswa
{
char nim[10]; char nama[30]; float uts; float uas;
};

Mahasiswa mhs;
// entri data
cout << "Masukkan NIM : "; cin.getline(mhs.nim, 10);
cout << "Masukkan Nama : "; cin.getline(mhs.nama, 30);
cout << "UTS : "; cin >> mhs.uts; cout << "UAS : "; cin >> mhs.uas;
// tampil data
cout << "\nNIM : " << mhs.nim;
cout << "\nNama : " << mhs.nama;
cout << "\nNilai : " << get_nilai(mhs.uts, mhs.uas);

getch();

}

Output:


Understanding / Definition of C++ Structs

Example of C++ Struct on Motor Dealer Receipt


C++ Struct Implementation 

Source Code:

#include<stdio>
#include<conio>
#include<iostream>
#include<iomanip>

/*
*bundet.com
*Wawan Beneran
*Implementasi Struct C++ pada Struk Dealer Motor
*/

struct
{
char kd[5],*merk;
int hrg,jml,total;
}motor[30];

main()
{
char nmsales[20],nm[20],lagi;
float tb=0;
int i,j;
awal:
clrscr();
gotoxy(17,1);
cout<<"S&D MOTOR"<<endl;
cout<<"Jl. Sunan Wawan Beneran No. 38 Telp. 021-192.168.8.1"<<endl;
cout<<"=============================================="<<endl;
cout<<" Nama Sales : ";cin>>nmsales;
cout<<" Nama Pembeli : ";cin>>nm;
cout<<" Jumlah data : ";cin>>j;
for(i=1;i<=j;i++)
{
cout<<" \n Data ke- "<<i<<endl;
cout<<" Kode [SPR/VRO/TGR] : ";cin>>motor[i].kd;
if(strcmp(motor[i].kd,"SPR")==0|| strcmp(motor[i].kd,"spr")==0)
{
motor[i].merk="Supra X";
motor[i].hrg=16540000;
}
else if(strcmp(motor[i].kd,"VRO")==0|| strcmp(motor[i].kd,"vro")==0)
{
motor[i].merk="Vario Techno";
motor[i].hrg=15790000;
}
else if(strcmp(motor[i].kd,"TGR")==0|| strcmp(motor[i].kd,"tgr")==0)
{
motor[i].merk="Tiger";
motor[i].hrg=24940000;
}
else
{
cout<<" Anda Salah Memasukkan Kode . . !!!, input Kode[SPR/VRO/TGR] . ."<<endl;
cout<<" Ingin Input Lagi : ";cin>>lagi;
if(lagi=='Y'||lagi=='y')
goto awal;
else
goto akhir;
}
cout<<" Masukan Jumlah Beli : ";cin>>motor[i].jml;
motor[i].total=motor[i].hrg*motor[i].jml;
tb=motor[i].total+tb;
}
clrscr();
gotoxy(23,1);
cout<<" S&D MOTOR"<<endl;
gotoxy(8,2);
cout<<"Jl. Sunan Wawan Beneran No. 38 Telp. 021-192.168.8.1"<<endl;
gotoxy(8,3);
cout<<"======================================================"<<endl;
cout<<"Nama Sales :"<<nmsales<<endl;
cout<<"Nama Pembeli :"<<nm<<endl;
cout<<"======================================================"<<endl;
cout<<"No. Kode Merk Harga Jumlah Total "<<endl;
cout<<" Motor Motor Beli "<<endl;
cout<<"======================================================"<<endl;
for(i=1;i<=j;i++)
{
cout<<setiosflags(ios::left)<<setw(5)<<i;
cout<<setiosflags(ios::left)<<setw(6)<<motor[i].kd;
cout<<setiosflags(ios::left)<<setw(12)<<motor[i].merk;
cout<<setiosflags(ios::left)<<setw(14)<<motor[i].hrg;
cout<<setiosflags(ios::left)<<setw(10)<<motor[i].jml;
cout<<setiosflags(ios::left)<<setw(2)<<motor[i].total<<endl;
}
cout<<"======================================================="<<endl;
printf(" Total bayar Rp. %4.0f",tb);
cout<<endl;
cout<<"Mau Input data lagi[Y/T]:";cin>>lagi;
if (lagi=='Y'||lagi=='y')
goto awal;
else
goto akhir;
akhir:
getch();
}

Hope this is useful & happy learning!


Post a Comment

Previous Next

نموذج الاتصال