Visual C++ Menu Driven Student Mark List Preparation with persistant data
Here is Visual C++ source code Menu Driven Student Mark List Preparation with persistant data. You can add student name and marks for 5 subjects. Then you will have an option to view, edit and delete the student and marks information.
All the data you entered would be saved into a file. so that if you are re running the program, then you do not have add any old data.
Related Links
Turbo C++ Menu Driven Student Mark List Preparation with persistant data
-
Visual C++ Menu Driven Student Mark List Preparation with persistant data
-
C# Menu Driven Student Mark List Preparation with persistant data
To get the simple version of student marklist with out menu and streaming
Source Code
#include <windows.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
void gotoxy(int x, int y)
{
COORD ord;
ord.X = x;
ord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
}
#pragma pack(2)
struct CStudent
{
public:
char name[64];
int marks[5];
int total;
};
struct CStudents
{
#define MaxSz 100
char m_fileName[1024];
int m_nMaxStudents;
CStudent m_studList[MaxSz];
CStudents(const char *filename);
void AddRecord(const char *name, int *marks);
void EditRecord(int pos, const char *name, int *marks);
void DeleteRecord(int pos);
int ReadRecords();
int WriteRecords();
};
CStudents::CStudents(const char *filename)
{
m_nMaxStudents = 0;
strcpy(m_fileName, filename);
}
void CStudents::AddRecord(const char *name, int *marks)
{
int pos = m_nMaxStudents;
strcpy(m_studList[pos].name,name);
m_studList[pos].total = 0;
for(int i = 0; i < 5; i++)
{
m_studList[pos].marks[i] = marks[i];
m_studList[pos].total += marks[i];;
}
m_nMaxStudents++;
WriteRecords();
}
void CStudents::EditRecord(int pos, const char *name, int *marks)
{
strcpy(m_studList[pos].name,name);
m_studList[pos].total = 0;
for(int i = 0; i < 5; i++)
{
m_studList[pos].marks[i] = marks[i];
m_studList[pos].total += marks[i];
}
WriteRecords();
}
void CStudents::DeleteRecord(int pos)
{
m_nMaxStudents--;
for(int i = pos; i < m_nMaxStudents; i++)
{
m_studList[i] = m_studList[i + 1];
}
WriteRecords();
}
int CStudents::ReadRecords()
{
FILE *istream = fopen(m_fileName, "rb");
if (istream == 0)
return 0;
char buf[4096];
int nTotalRecordsRead = 0;
for(int i = 0; i < MaxSz; i++)
{
if(feof(istream))
break;
int nBytesRead = fread(buf, 1, sizeof(CStudent), istream);
if(nBytesRead < sizeof(CStudent))
break;
char *p = (char*)(&m_studList[i]);
memcpy(p, buf, sizeof(CStudent));
nTotalRecordsRead++;
}
fclose(istream);
m_nMaxStudents = nTotalRecordsRead;
return nTotalRecordsRead;
}
int CStudents::WriteRecords()
{
FILE *ostream = fopen(m_fileName, "wb");
if (ostream == 0)
return 0;
int nTotalRecordsWritten = 0;
char buf[4096];
for(int i = 0; i < m_nMaxStudents; i++)
{
fwrite((char*)&m_studList[i], 1, sizeof(CStudent), ostream);
nTotalRecordsWritten++;
}
fclose(ostream);
return nTotalRecordsWritten;
}
CStudents theStudents("c:\\marklist.bin");
int DisplayMainMenu()
{
::system("cls");
gotoxy(10,4);
std::cout << "Welcome to Student MarkList Application";
gotoxy(10,5);
std::cout << "___________________________________________";
gotoxy(10,6);
std::cout << "1. Add Student Mark List";
gotoxy(10,7);
std::cout << "2. Edit Student Mark List";
gotoxy(10,8);
std::cout << "3. View Student Mark List";
gotoxy(10,9);
std::cout << "4. Delete Student Mark List";
gotoxy(10,10);
std::cout << "5. Exit";
gotoxy(10,11);
std::cout << "___________________________________________";
gotoxy(10,13);
std::cout << "Enter your Selection: ";
int m = -1;
std::cin >> m;
return m;
}
void ViewRecords()
{
theStudents.ReadRecords();
::system("cls");
gotoxy(10,4);
std::cout << "Welcome to Student Mark List Application";
gotoxy(10,5);
std::cout << "_______________________________________________________________";
gotoxy(10,6);
std::cout << "SNo Student Name Sub1 Sub2 Sub3 Sub4 Sub5 Total";
gotoxy(10,7);
std::cout << "_______________________________________________________________";
int pos = 8;
// Enable Paging
for(int i = 0; i < theStudents.m_nMaxStudents; i++)
{
gotoxy(10,pos);
std::cout << i + 1;
gotoxy(14,pos);
std::cout << theStudents.m_studList[i].name;
gotoxy(33,pos);
std::cout << theStudents.m_studList[i].marks[0];
gotoxy(40,pos);
std::cout << theStudents.m_studList[i].marks[1];
gotoxy(47,pos);
std::cout << theStudents.m_studList[i].marks[2];
gotoxy(54,pos);
std::cout << theStudents.m_studList[i].marks[3];
gotoxy(61,pos);
std::cout << theStudents.m_studList[i].marks[4];
gotoxy(68,pos);
std::cout << theStudents.m_studList[i].total;
pos++;
}
gotoxy(10,pos++);
std::cout << "_______________________________________________________________";
pos++;
gotoxy(10,pos++);
}
void InputRecords()
{
while(1)
{
::system("cls");
gotoxy(10,4);
std::cout << "Welcome to Student Mark List Application";
gotoxy(10,5);
std::cout << "__________________________________________________________";
gotoxy(10,6);
std::cout << "Student Name: ";
gotoxy(10,7);
std::cout << "Sub1 Mark: ";
gotoxy(10,8);
std::cout << "Sub2 Mark: ";
gotoxy(10,9);
std::cout << "Sub3 Mark: ";
gotoxy(10,10);
std::cout << "Sub4 Mark: ";
gotoxy(10,11);
std::cout << "Sub5 Mark: ";
gotoxy(10,12);
std::cout << "__________________________________________________________";
gotoxy(23,6);
char name[64];
std::cin >> name;
int marks[5];
for(int i = 0; i < 5; i++)
{
gotoxy(23,7 + i);
std::cin >> marks[i];
}
theStudents.AddRecord(name, marks);
gotoxy(10,14);
std::cout << "Do you want to add another record (Y/N)? ";
char ch = getch();
if(ch == 'Y' || ch == 'y')
continue;
else
break;
}
}
void EditRecords()
{
ViewRecords();
std::cout << "Enter the serial number you want to edit: ";
int m;
std::cin >> m;
if(m >= 1 && m <= theStudents.m_nMaxStudents)
{
::system("cls");
gotoxy(10,4);
std::cout << "Welcome to Student Mark List Application";
gotoxy(10,5);
std::cout << "__________________________________________________________";
gotoxy(10,6);
std::cout << "Student Name: ";
gotoxy(10,7);
std::cout << "Sub1 Mark: ";
gotoxy(10,8);
std::cout << "Sub2 Mark: ";
gotoxy(10,9);
std::cout << "Sub3 Mark: ";
gotoxy(10,10);
std::cout << "Sub4 Mark: ";
gotoxy(10,11);
std::cout << "Sub5 Mark: ";
gotoxy(10,12);
std::cout << "__________________________________________________________";
gotoxy(23,6);
char name[64];
std::cin >> name;
int marks[5];
for(int i = 0; i < 5; i++)
{
gotoxy(23,7 + i);
std::cin >> marks[i];
}
theStudents.EditRecord(m - 1, name, marks);
gotoxy(10,12);
std::cout << "Record updated. Press any key to return to Main Menu";
getch();
}
else
{
gotoxy(10,12);
std::cout << "Invalid Entry. Press any key to return to Main Menu";
getch();
}
}
void DeleteRecords()
{
ViewRecords();
std::cout << "Enter the serial number you want to delete: ";
int m;
std::cin >> m;
if(m >= 1 && m <= theStudents.m_nMaxStudents)
{
theStudents.DeleteRecord(m - 1);
std::cout << "Record deleted. Press any key to return to Main Menu";
getch();
}
else
{
std::cout << "Invalid Entry. Press any key to return to Main Menu";
getch();
}
}
int main()
{
theStudents.ReadRecords();
while(1)
{
int selection = DisplayMainMenu();
switch(selection)
{
case 1:
InputRecords();
break;
case 2:
EditRecords();
break;
case 3:
{
ViewRecords();
std::cout << "Press any key to return to Main Manu: ";
getch();
}
break;
case 4:
DeleteRecords();
break;
case 5:
default:
return 0;
};
}
}Output
|