اذهب إلى المحتوى
  • 0

سؤال c++

Anwar Shweiki

السؤال

يعطيكو العافية بتمنى حدا يحكيلي كيف الحل :)

Create C++ program that manage a student system that will store data about 100 student, this
includes the student’s ID, name, grade, collage. All the added data are stored in file.
The available operations in a menu:
1. Add New Student
2. Save data in file
3. View all students names with their grades.
4. View all students names whose grades more than the average.
5. List all student names in collage (x) that entered by user.
6. List the name of the student who has the maximum grade.
7. Search for student with a given ID entered by user
8. Modify the grade for a given student.
9. Exit
Hints: you should use parallel arrays, suitable function for each operation, files.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0
بتاريخ On 4/10/2020 at 19:38 قال Anwar Shweiki:

يعطيكو العافية بتمنى حدا يحكيلي كيف الحل :)

Create C++ program that manage a student system that will store data about 100 student, this
includes the student’s ID, name, grade, collage. All the added data are stored in file.
The available operations in a menu:
1. Add New Student
2. Save data in file
3. View all students names with their grades.
4. View all students names whose grades more than the average.
5. List all student names in collage (x) that entered by user.
6. List the name of the student who has the maximum grade.
7. Search for student with a given ID entered by user
8. Modify the grade for a given student.
9. Exit
Hints: you should use parallel arrays, suitable function for each operation, files.

اهلا بك @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;


}

 

اتمنى ان اكون قد افدتك :) 

تحياتي

شكرا لك 

تم التعديل في بواسطة Mohamd Imran
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...