اهلا بك @Anwar Shweiki
تفضل قمت بعمل الاجزاء الرئيسية للمطلوب من البرنامج وتبقى اجزاء قليلة اتركها لك هذا لمصلحتك
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
int choice;
//load the text file and put it into a single string:
std::ifstream in("student.txt");
std::stringstream buffer;
buffer << in.rdbuf();
std::string test = buffer.str();
int max = 0;
int index = 0;
// std::cout << test << std::endl << std::endl;
//create variables that will act as "cursors". we'll take everything between them.
size_t name = 2;
size_t grade = 13;
size_t collage = 17;
size_t pos2;
// std::string line = test.find("2|",name);
//create the array to store the strings.
std::string str[11];
for (int x=0; x<=10; x++){
//Student Name from student file
pos2 = test.find("|", name); //search for the bar "|". pos2 will be where the bar was found.
str[x] = test.substr(name, (pos2-name)); //make a substring, wich is nothing more
//than a copy of a fragment of the big string.
std::cout << "Name"<< test << ""<< str[x] << '\t' ;
// std::cout << "Student Name :" << str[x] << std::endl;
name = pos2; // sets pos1 to the next character after pos2.
//so, it can start searching the next bar |.
//Ends here
//Student Grade from student file
pos2 = test.find("|", grade); //search for the bar "|". pos2 will be where the bar was found.
str[x] = test.substr(grade, (pos2-grade)); //make a substring, wich is nothing more
//than a copy of a fragment of the big string.
std::cout << "Grade" << "|" << ""<< str[x] << '\t';
// std::cout << "Grade :" << str[x] << std::endl;
// std::cout << "grade pos:" << grade << ", pos2:" << pos2 << std::endl;
grade = pos2; // sets pos1 to the next character after pos2.
//so, it can start searching the next bar |.
//Ends here
//Student Collage from student file
pos2 = test.find("|", collage); //search for the bar "|". pos2 will be where the bar was found.
str[x] = test.substr(collage, (pos2-collage)); //make a substring, wich is nothing more
//than a copy of a fragment of the big string.
std::cout << "Collage" << "|" << ""<< str[x] << '\n';
// std::cout << "Student Collage :" << str[x] << std::endl;
// std::cout << "collage pos:" << collage << ", pos2:" << pos2 << std::endl;
collage = pos2; // sets pos1 to the next character after pos2.
//so, it can start searching the next bar |.
//Ends here
std::cout << "\n" << '\n';
}
ofstream myfile;
ifstream infile;
bool gameOn = true;
//parallel arrays
char students_id[100];
string students_name[100];
double grades[100];
string collages[100];
int n = sizeof(students_id)/sizeof(students_id[0]);
for(int i = 0; i < n; i++) {
if (students_id[i] > max) {
max = students_id[i];
index = i;
}
}
cout << "*******************************\n";
cout << " 1 - Add Student.\n";
cout << " 2 - Student info.\n";
cout << " 3 - View all students names with their grades.\n";
cout << " 4 - Exit.\n";
cout << " Enter your choice and press return: ";
do {
/* code */
cin >> choice;
switch (choice)
{
case 1:
cout << "Adding Students data\n";
myfile.open("student.txt",ios::app);
cout << "Student Id :" <<endl;
cin >> students_id[index];
myfile << students_id[index] << "|";
cout << "Student Name :" <<endl;
cin >> students_name[index];
myfile << students_name[index] << "|";
cout << "Student Grade :" <<endl;
cin >> grades[index];
myfile << grades[index] << "|";
cout << "Student Collage :" <<endl;
cin >> collages[index];
myfile << collages[index] << "|";
myfile <<"\n"<<endl;
myfile.close();
cout << "Data Saved Successfully "<<"\n";
cout << " 2 - Show Student info."<<endl;
break;
case 2:
cout << students_id[index]<<"\t";
cout << students_name[index]<<"\t";
cout << grades[index] << "\t" ;
cout << collages[index] << "\t" ;
// cout << students_id[index] << "\t" ;
break;
case 3:
cout << "Students Grades :\n";
infile.open("student.txt");
infile.close();
break;
case 4:
cout << "End of Program.\n";
studentStar = false;
break;
default:
studentStar = false;
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}
}while(studentStar != false);
return 0;
}
اتمنى ان اكون قد افدتك
تحياتي
شكرا لك