0% found this document useful (0 votes)
24 views38 pages

Sun Shin

The document is a C++ program that manages student data, including adding student information, calculating GPA, saving data to a file, and updating student records. It includes functions for input validation, checking for duplicate courses, and displaying student information. The program also implements user registration and login functionalities, allowing up to 25 users.

Uploaded by

amanuelbr92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views38 pages

Sun Shin

The document is a C++ program that manages student data, including adding student information, calculating GPA, saving data to a file, and updating student records. It includes functions for input validation, checking for duplicate courses, and displaying student information. The program also implements user registration and login functionalities, allowing up to 25 users.

Uploaded by

amanuelbr92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

#include <iostream>

#include <fstream>

#include <string>

#include <conio.h>

#include <cctype> // Include the cctype library for isalpha function

#include <windows.h>

using namespace std;

void login();

void registere();

void forget();

const int MAX_USERS = 25;

int userCount = 0;

void studentMenu( );

void teacherMenu();

void readStudentRecord();

void forgotPassword();

void forgotEmail();

void showStudentInfo();

struct student

{int k;

string name;

int mid[10], assignment[10], final[10], credit_hours[10];

int point[10];
char grade[10];

string course_name[10];

string id;

int total_credit_hours;

int get_credit_hours(int i) const

return credit_hours[i];

char get_grade(int j) const

return grade[j];

};

void add_student_data(student stu[], int& n);

bool is_alphabetical(const string& str);

bool is_duplicate_course(const student stu[], int n, const string& courseName);


bool is_valid_name(string name)

for (char c : name)

if (!isalpha(c))

return false;

return true;

void add_student_data(student stu[], int& n)

while (true)

cout << "Enter the ID of the student: ";

cin >> stu[n].id;

while (true)

{
cout << "Enter the name of the student: ";

cin >> stu[n].name;

system("cls");

if (stu[n].name.length() > 3)

break;

cout << "Invalid name. Please try again." << endl;

while (!is_valid_name(stu[n].name))

cout << "Incorrect input. Enter the student's first name: ";

cin >> stu[n].name;

stu[n].total_credit_hours = 0; // Initialize total_credit_hours

for (int j = 0; j < 4; j++)

system("cls");
cout << "Enter the name of course " << j + 1 << ": ";

cin >> stu[n].course_name[j];

// Validate course name (alphabetical and unique)

while (!is_alphabetical(stu[n].course_name[j]) || is_duplicate_course(stu, n,


stu[n].course_name[j]))

if

(!is_alphabetical(stu[n].course_name[j]))

cout << "Invalid course name. Only alphabetical characters are allowed. Please try again." <<
endl;

else if (is_duplicate_course(stu, n, stu[n].course_name[j]))

cout << "Course name already exists. Please enter a unique course name." << endl;

cout << "Enter the name of course " << j + 1 << ": ";

cin >> stu[n].course_name[j];

cout << "Enter the number of credit hours for course " << stu[n].course_name[j] << " (1-6): ";

cin >> stu[n].credit_hours[j];

// Validate credit hours (between 1 and 6)

while (stu[n].credit_hours[j] < 1 || stu[n].credit_hours[j] > 6)

cout << "Invalid credit hours. Please enter a value between 1 and 6." << endl;

cout << "Enter the number of credit hours for course " << stu[n].course_name[j] << " (1-6): ";
cin >> stu[n].credit_hours[j];

}/*

int stu[n].total_credit_hours += stu[n].credit_hours[j];

int total_credit_hours += stu[n].credit_hours[j];

if (total_credit_hours > 40)

cout << "Total credit hours cannot exceed 40. Please adjust the credit hours." << endl;

stu[n].total_credit_hours -= stu[n].credit_hours[j];

total_credit_hours -= stu[n].credit_hours[j];

j--; // Re-ask for the current course

continue;

*/

int midterm_score;

while (true)

cout << "Enter the midterm result for course " << j + 1 << " " << stu[n].course_name[j] << " (0-
20): ";

cin >> midterm_score;

if (midterm_score >= 0 && midterm_score <= 20)

{
stu[n].mid[j] = midterm_score;

break;

cout << "Invalid score! The score must be between 0 and 20." << endl;

int assignment_score;

while (true)

cout << "Enter the assignment result for course " << stu[n].course_name[j] << " (0-30): ";

cin >> assignment_score;

if (assignment_score >= 0 && assignment_score <= 30)

stu[n].assignment[j] = assignment_score;

break;

cout << "Invalid score! The score must be between 0 and 30." << endl;

int final_score;

while (true)
{

cout << "Enter the final result for course " << stu[n].course_name[j] << " (0-50): ";

cin >> final_score;

if (final_score >= 0 && final_score <= 50)

stu[n].final[j] = final_score;

break;

cout << "Invalid score! The score must be between 0 and 50." << endl;

stu[n].point[j] = stu[n].mid[j] + stu[n].assignment[j] + stu[n].final[j];

if (stu[n].point[j] >= 90)

stu[n].grade[j] = 'A';

else if (stu[n].point[j] >= 80 && stu[n].point[j] < 90)

stu[n].grade[j] = 'B';

else if (stu[n].point[j] >= 70 && stu[n].point[j] < 80)

stu[n].grade[j] = 'C';

else if (stu[n].point[j] >= 60 && stu[n].point[j] < 70)

stu[n].grade[j] = 'D';

else

stu[n].grade[j] = 'F';
}

cout << "Do you want to add another student? (y/n): ";

char choice;

cin >> choice;

system("cls");

if (choice == 'n' || choice == 'N')

break;

n++;

// Function to check if a string contains only alphabetical characters

bool is_alphabetical(const string& str)

for (char ch : str)

if (!isalpha(ch))

return false;

return true;

}
// Function to check if a course name already exists in the student array

bool is_duplicate_course(const student stu[], int n, const string& courseName)

{int k;

for (int i = 0; i <= n; i++)

for (int j = 0; j < k; j++)

cin>>k;

if (stu[i].course_name[j] == courseName)

return true;

return false;

void display_student_data(const student stu[], int n)

for (int i = 0; i <= n; i++)

cout << "ID: " << stu[i].id << endl;

cout << "Name: " << stu[i].name << endl;

for (int j = 0; j < 4; j++)


{

cout << "Course " << j + 1 << ": " << stu[i].course_name[j] << endl;

cout << "Credit hours: " << stu[i].credit_hours[j] << endl;

cout << "Grade: " << stu[i].get_grade(j) << endl;

cout << endl;

cout << "Total Credit Hours: " << stu[i].total_credit_hours << endl;

cout << "------------------------------------" << endl;

float calculate_gpa(const student stu[], int n)

int total_credit_hours = 0;

float total_points = 0.0;

float gpa;

for (int i = 0; i <= n; i++)

{
for (int j = 0; j < 4; j++)

total_credit_hours += stu[i].credit_hours[j];

float grade_point = 0.0;

switch (stu[i].grade[j])

case 'A':

grade_point = 4.0;

break;

case 'B':

grade_point = 3.0;

break;

case 'C':

grade_point = 2.0;

break;

case 'D':

grade_point = 1.0;

break;

case 'F':

grade_point = 0.0;

break;

// Add additional cases for other grades if needed

}
total_points += (stu[i].credit_hours[j] * grade_point);

if (total_credit_hours == 0)

return 0.0;

gpa =total_points / total_credit_hours;

return gpa ;

void save_data(const student stu[], int n)

ofstream outFile("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::app);

if (!outFile)

cout << "Error opening file." << endl;

return;

for (int i = 0; i < n; i++)

{
outFile << "ID: " << stu[i].id << endl;

outFile << "Name: " << stu[i].name << endl;

for (int j = 0; j < 4; j++)

outFile << "Course " << j + 1 << " Name: " << stu[i].course_name[j] << endl;

outFile << "Score for MID " << j + 1 << ": " << stu[i].mid[j] << endl;

outFile << "Score for Assignment " << j + 1 << ": " << stu[i].assignment[j] << endl;

outFile << "Score for Final " << j + 1 << ": " << stu[i].final[j] << endl;

outFile << "Credit hours for this course: " << stu[i].credit_hours[j] << endl;

outFile << "Point for this course: " << stu[i].point[j] << endl;

outFile << "Grade for this course: " << stu[i].grade[j] << endl;

outFile << "Total credit hours: " << stu[i].total_credit_hours << endl;

outFile << endl;

outFile.close();

cout << "Data saved to file successfully." << endl;

}
void update_student_info(student stu[], int n)

string student_id;

cout << "Enter the ID of the student whose information you want to update: ";

cin >> student_id;

int student_index = -1;

for (int i = 0; i < n; i++)

if (stu[i].id == student_id)

student_index = i;

break;

if (student_index == -1)

cout << "Student not found." << endl;

return;

cin.ignore(); // Ignore any remaining newline character in the input buffer


cout << "Enter the new name of the student: ";

getline(cin, stu[student_index].name);

cout << "Enter the new credit hours for each course: " << endl;

for (int j = 0; j < 4; j++)

cout << "Course " << j + 1 << ": ";

cin >> stu[student_index].credit_hours[j];

stu[student_index].total_credit_hours += stu[student_index].credit_hours[j];

cout << "Student information updated successfully." << endl;

void search_by_course_name(const student stu[], int n)

string search_course;

cout << "Enter the course name to search for: ";

cin >> search_course;

bool found = false;

for (int i = 0; i < n; i++)

{
for (int j = 0; j < 4; j++)

if (stu[i].course_name[j] == search_course)

cout << "Student ID: " << stu[i].id << endl;

cout << "Student Name: " << stu[i].name << endl;

cout << "Course " << j + 1 << " Name: " << stu[i].course_name[j] << endl;

cout << "Score for MID " << j + 1 << ": " << stu[i].mid[j] << endl;

cout << "Score for Assignment " << j + 1 << ": " << stu[i].assignment[j] << endl;

cout << "Score for Final " << j + 1 << ": " << stu[i].final[j] << endl;

cout << "Credit hours for this course: " << stu[i].credit_hours[j] << endl;

cout << "Point for this course: " << stu[i].point[j] << endl;

cout << "Grade for this course: " << stu[i].grade[j] << endl;

cout << endl;

found = true;

if (!found)

cout << "No student found with the given course name." << endl;

}
void cool()

student stu[10];

int n = 0;

int c;

do

cout << "1. show Student Info" << endl;

cout << "2. Exit" << endl;

cout << "Enter your choice: ";

cin >> c;

switch (c)

case 1:

system("cls");

showStudentInfo();

break;
case 2:

cout << "Goodbye." << endl;

break;

default:

cout << "Invalid choice." << endl;

system("pause");

} while (c!= 2);

void menu(){

student stu[10];

int n = 0;

bool logged_in = false;


int choice;

float gpa;

do

cout << "1. Add student data" << endl;

cout << "2. Calculate GPA" << endl;

cout << "3. Save data to file" << endl;

cout << "4. Display student data" << endl;

cout << "5. Update student information" << endl;

cout << "6. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice)

case 1:

system("cls");

add_student_data(stu, n );
break;

case 2:

system("cls");

gpa = calculate_gpa(stu, n);

cout << "GPA: " << gpa << endl;

break;

case 3:

system("cls");

save_data(stu, n);

break;

case 4:

system("cls");

display_student_data(stu, n);

break;

case 5:

system("cls");

update_student_info(stu, n);
break;

case 6:

cout << "Goodbye." << endl;

break;

default:

cout << "Invalid choice." << endl;

menu();

system("pause");

} while (choice != 6);

}
struct User {

int id;

string email;

string name;

string password;

string role;

};

User users[MAX_USERS];

void registerUser()

system("cls");

int ascii = 178;

char ch = ascii, d;

for (int i = 1; i <= 23; i++)

cout << ch;

cout << " C++ Project for Our Aim ";


for (int i = 1; i <= 23; i++)

cout << ch;

cout << "\n\n\n" << ch << ch << " Enter user id: ";

cin >> users[userCount].id;

// Check if the user ID is already used

for (int i = 0; i < userCount; i++) {

if (users[i].id == users[userCount].id) {

cout << "\n\n\n" << ch << ch << " User ID already exists! Please choose a different one.\n";

getch();

return;

cout << "\n\n\n" << ch << ch << " Enter user name: ";

cin >> users[userCount].name;

cout << "\n\n\n" << ch << ch << " Enter user email: ";

cin >> users[userCount].email;

// Check if the email is already used

for (int i = 0; i < userCount; i++) {

if (users[i].email == users[userCount].email) {

cout << "\n\n\n" << ch << ch << " Email already exists! Please choose a different one.\n";
getch();

return;

cout << "Role of user (1 for student, 2 for teacher): ";

cin >> users[userCount].role;

cout << "\n\n\n" << ch << ch << " Enter user password: ";

do {

d = getch();

if (isdigit(d) || isalpha(d) || ispunct(d)) {

users[userCount].password += d;

cout << "*";

} while (isdigit(d) || isalpha(d) || ispunct(d));

ofstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::app);

if (file.is_open()) {

file << users[userCount].id << " " << users[userCount].name << " " << users[userCount].email << " "
<< users[userCount].password << " " << users[userCount].role << endl;

file.close();

userCount++;

cout << "\n\n\n " << ch << " Registered successfully! " << ch << ch;

login();

}
void showStudentInfo()

system("cls");

int ascii = 178;

char ch = 178;

for (int i = 1; i <= 23; i++)

cout << ch;

cout << " C++ Project for Our Aim ";

for (int i = 1; i <= 23; i++)

cout << ch;

int studentID;

cout << "\n\n\n" << ch << ch << " Enter student ID: ";

cin >> studentID;

bool found = false;

ifstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::in);

if (file.is_open()) {

string line;

while (getline(file, line)) {

if (line.find("ID: " + to_string(studentID)) != string::npos) {

cout << "\n\n\n " << ch << " Student Information " << ch << ch << endl;
cout << line << endl;

while (getline(file, line) && !line.empty()) {

cout << line << endl;

found = true;

break;

file.close();

if (!found) {

cout << "\n\n\n " << ch << " Student not found! " << ch << ch;

void login()

system("cls");

int ascii = 178;

char ch = 178, d;

for (int i = 1; i <= 23; i++)

cout << ch;


cout << " C++ Project for Our Aim ";

for (int i = 1; i <= 23; i++)

cout << ch;

cout << "\n\tWelcome to the login page\n";

cout << "\tMenu:\n";

cout << "| Press 1 for Student Login |\n";

cout << "| Press 2 for Teacher Login |\n";

cout << "| Press 3 for Forget Password |\n";

cout << "| Press 4 for forget email \n";

cout << "| Press 5 to exist |\n";

cout << "| Please enter your choice: ";

if (userCount == 0) {

cout << "\n\n\n " << ch << " No records found! " << ch << ch;

else {

int choice;

cin >> choice;

string role;

switch (choice) {

case 1:
studentMenu();

break;

case 2:

menu();

break;

case 3:

forgotEmail();

break;

case 4:

forgotPassword();

break;

case 5:

login();

default:

cout << "\n\n\n " << ch << " Invalid choice! " << ch << ch;

getch();

return;

string email, password;

cout << "\n\n\n" << ch << ch << " Enter user email: ";

cin >> email;

cout << "\n\n\n" << ch << ch << " Enter user password: ";

do {
d = getch();

if (isdigit(d) || isalpha(d) || ispunct(d)) {

password += d;

cout << "*";

} while (isdigit(d) || isalpha(d) || ispunct(d));

bool found = false;

ifstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::in);

if (file.is_open()) {

while (!file.eof()) {

file >> users[userCount].id >> users[userCount].name >> users[userCount].email >>


users[userCount].password >> users[userCount].role;

if (email == users[userCount].email && password == users[userCount].password) {

if (users[userCount].role == role) {

cout << "\n\n\n " << ch << " Login successful! Welcome, " << users[userCount].name << " ("
<< role << ")" << ch << ch;

found = true;

// Additional functionality based on role

if (role == "student")

studentMenu();

else if (role == "teacher")


{

teacherMenu();

break;

else {

cout << "\n\n\n " << ch << " Incorrect role for login! " << ch << ch;

found = true;

break;

file.close();

if (!found) {

cout << "\n\n\n " << ch << " Invalid login credentials! " << ch << ch;

void studentMenu()

cool();

}
void teacherMenu()

menu();

void forgotEmail()

system("cls");

int ascii = 178;

char ch = 178, d;

for (int i = 1; i <= 23; i++)

cout << ch;

cout << " C++ Project for Our Aim ";

for (int i = 1; i <= 23; i++)

cout << ch;

if (userCount == 0) {

cout << "\n\n\n " << ch << " No records found! " << ch << ch;

else {

string password;

cout << "\n\n\n" << ch << ch << " Enter user password: ";

do {

d = getch();
if (isdigit(d) || isalpha(d) || ispunct(d)) {

password += d;

cout << "*";

} while (isdigit(d) || isalpha(d) || ispunct(d));

bool found = false;

ifstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::in);

if (file.is_open()) {

while (!file.eof()) {

file >> users[userCount].id>> users[userCount].name >> users[userCount].email >>


users[userCount].password >> users[userCount].role;

if (password == users[userCount].password) {

cout << "\n\n\n" << ch << ch << " User ID: " << users[userCount].email<< ch << ch;

found = true;

break;

file.close();

if (!found) {

cout << "\n\n\n " << ch << " Invalid password! " << ch << ch;

}
}

void forgotPassword()

system("cls");

int ascii = 178;

char ch = 178;

for (int i = 1; i <= 23; i++)

cout << ch;

cout << " C++ Project for Our Aim ";

for (int i = 1; i <= 23; i++)

cout << ch;

if (userCount == 0) {

cout << "\n\n\n " << ch << " No records found! " << ch << ch;

else {

string email;

cout << "\n\n\n" << ch << ch << " Enter user email: ";

cin >> email;

bool found = false;


ifstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::in);

if (file.is_open()) {

while (!file.eof()) {

file >> users[userCount].id >> users[userCount].name >> users[userCount].email >>


users[userCount].password >> users[userCount].role;

if (email == users[userCount].email) {

cout << "\n\n\n" << ch << ch << " User ID: " << users[userCount].id << ch << ch;

found = true;

break;

file.close();

if (!found) {

cout << "\n\n\n " << ch << " Invalid email! " << ch << ch;

void readStudentRecord(int userID)

ifstream file("C:\\Users\\Pc\\Desktop\\New folder (3)\\text.txt",ios::in);


if (file.is_open()) {

while (!file.eof()) {

int id;

string name, email, password, role;

file >> id >> name >> email >> password >> role;

if (id == userID && role == "student") {

cout << "ID: " << id << endl;

cout << "Name: " << name << endl;

cout << "Email: " << email << endl;

// Additional fields can be printed here

break;

file.close();

int main()

system("cls");

int ascii = 178;

char ch = ascii;
while (true) {

cout << "\n";

for (int i = 1; i <= 23; i++)

cout << ch;

cout << " C++ Project for Our Aim ";

for (int i = 1; i <= 23; i++)

cout << ch;

cout << "\n\n" << ch << ch << " 1. Register ";

cout << "\n\n" << ch << ch << " 2. Login";

cout<<"\n\n";

for (int i = 1; i <= 23; i++){

cout << ch;

cout << "\n\n\n " << ch << " Enter your choice: " << ch << ch << " > ";

int choice;

cin >> choice;

switch (choice) {

case 1:

registerUser();

break;
case 2:

login();

break;

default:

cout << "\n\n\n " << ch << " Invalid choice! " << ch << ch;

break;

getch;

system("cls");

}}

You might also like