C++ Struct

Published on: November 24, 2010

In this tutorial, I give you a very basic example and simple explanations to how structs work. This tutorial is to serve as a base for future tutorials dealing with structs and other data structures.

In future tutorials, I expect to show you how to use structs with Arrays as well as functions. If you have any questions about the program or functions in general, feel free to ask.

I hope you enjoy the video and if you have any requests feel free to let me know.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Programmer:  Nazmus
//Program:   Structs
//Website:   www.EasyProgramming.net

#include <iostream>
#include <string>

using namespace std;

struct student
{
 string name;
 int grade;
};

int main()
{
 
 student pupil1;
 student pupil2;

 cout << "Please enter student's name: ";
 cin >> pupil1.name;

 cout << "Please enter student's grade: " ;
 cin >> pupil1.grade;

 cout << "Please enter student's name: ";
 cin >> pupil2.name;

 cout << "Please enter student's grade: " ;
 cin >> pupil2.grade;
 
 cout << "Name" << "          " << "Grade" << endl;

 cout << pupil1.name << "           " << pupil1.grade << endl;
 cout << pupil2.name << "           " << pupil2.grade << endl;

 system("pause");

}

Comments: