Jump to content

Base de dados


meny
 Share

Recommended Posts

Boa tarde

Será que alguém me pode ajudar não estou a conseguir resolver o problema do código.

#include <string>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <string>

using namespace std;

struct stu_data
{
   int id;
   string fn;
   string ln;
   char gender;
   string major;
   float cgpa;
};

void load (stu_data array[], int & n, ifstream & extract);
void insertion (stu_data array[], int & n, ifstream & extract);
void deletion (stu_data array[], int & n);
void searching (stu_data array[], int & n);
void printing (stu_data array[], int & n);
void dump (stu_data array[], int & n, ofstream & write);

int main()
{
   string s;
   ifstream read;
   ifstream extract;
   ofstream write;

   cout<<"Enter the name of the data file: ";
   cin>>s;

   read.open(s.c_str());
   if (!read)
   {
       cout<<"Input file open error\n";
   }

   write.open("a.txt");
   if(!write)
   {
       cout<<"Output file open error\n";
   }


   cout<<"\n\t\tStudent Database.";
   cout<<"\n\nNB: Enter full name, full ID, entire CGPA, abreviated Gender \n"
   <<" (M for Male or F for Female) and abreviated Major (EEE, CSE, etc), \n"
   <<" for double major use comma in-between, followed by a space.\n\n";

   for (int x = 0; x < 70; x++)
   {
       cout<<"|";
   }

   cout<<endl<<endl;

   int stu_n = 0;
   stu_data array[20];

   for (char quit = 'a'; quit != 'q'; )
   {
       char choice = 'z';

       cout<<"\nEnter choices...............\n"
       <<"To insert data...... press i\n"
       <<"To delete data.......press d\n"
       <<"To search data.......press s\n"
       <<"To print data........press p\n"
       <<"To quit program......press q\n"
       <<"Enter choice now............: ";
       cin>>choice;

       cout<<endl;

       // insertion
       if (choice == 'i')
       {
           insertion (array, stu_n, extract);
       }

       // deletion
       else if (choice == 'd')
       {
           deletion (array, stu_n);
       }

       // searching
       else if (choice == 's')
       {
           searching (array, stu_n);
       }

       // printing
       else if (choice == 'p')
       {
           printing (array, stu_n);
       }

       // exit
       else if (choice == 'q')
       {
           dump (array, stu_n, write);
           quit = choice;
       }

       // invalid
       else
       {
           cout<<"Invalid Input\n";

       }
   }

   return 0;
}

void load (stu_data array[], int & n, ifstream & extract)
{
   extract.open("a.txt");
   int id_n;
   string fn_n;
   string ln_n;
   char gen;
   string maj;
   float gpa;

   while(extract>>id_n>>fn_n>>ln_n>>gen>>maj>>gpa)
   {
       array[n].id = id_n;
       array[n].fn = fn_n;
       array[n].ln = ln_n;
       array[n].gender = gen;
       array[n].major = maj;
       array[n].cgpa = gpa;

       n = n + 1;
   }
}

void insertion (stu_data array[], int & n, ifstream & extract)
{
   load (array, n, extract);
   const int z = 40;

   // ID
   cout<<" ID: ";
   cin>>array[n].id;

   // First Name
   cout<<" First Name: ";
   cin>>array[n].fn;

   // Last Name
   cout<<" Last Name: ";
   cin>>array[n].ln;

   // Gender
   cout<<" Sex: ";
   cin>>array[n].gender;

   // GPA
   cout<<"CGPA : ";
   cin>>array[n].cgpa;

   // Major
   cout<<"Major: ";
   cin>>array[n].major;

   int id_n;
   string f_n;
   string l_n;
   char gen;
   string maj;
   float gpa;

   for (int i = n - 1; i >= 0; i--)
   {
       if (array[i + 1].fn.compare(array[i].fn) < 0)
       {
           id_n = array[i].id;
           array[i].id = array[i + 1].id;
           array[i + 1].id = id_n;

           f_n = array[i].fn;
           array[i].fn = array[i + 1].fn;
           array[i + 1].fn = f_n;

           l_n = array[i].ln;
           array[i].ln = array[i + 1].ln;
           array[i + 1].ln = l_n;

           gen = array[i].gender;
           array[i].gender = array[i + 1].gender;
           array[i + 1].gender = gen;

           maj = array[i].major;
           array[i].major = array[i + 1].major;
           array[i + 1].major = maj;

           gpa = array[i].cgpa;
           array[i].cgpa = array[i+1].cgpa;
           array[i+1].cgpa = gpa;
       }
   }

   n = n + 1;
}

void deletion (stu_data array[], int & n)
{
   char delet;

   cout<<"To delete using ID .......press........i\n"
   <<"................Name......press .......n\n"
   <<"Enter now.......: ";
   cin>>delet;

   int i;
   if (delet == 'i') // id delete
   {
       int delet_i;
       int box;

       int bread_crum = -1;

       cout<<"Provide the id of the student you want to remove: ";
       cin>>delet_i;

       for(i = 0; i < n; i++)
       {
           if(array[i].id == delet_i)
           {
               box = i;
               bread_crum = 1;
               break;
           }
       }

       if (bread_crum > 0)
       {
           array[box].id = array[n - 1].id;
           array[box].fn = array[n - 1].fn;
           array[box].ln = array[n - 1].ln;
           array[box].gender = array[n - 1].gender;
           array[box].major = array[n - 1].major;
           array[box].cgpa = array[n - 1].cgpa;

           n--;

           cout<<"\nStudent information has been deleted!\n";
       }
       else
       {
           cout<<"\nStudent cant be deleted, there is no such record.\n";
       }
   }


}

void searching (stu_data array[], int & n)
{
   const int z = 40;
   char search = 'x';
   int s_id;

   int i,j;

   cout<<"To search using Name.....press n\n"
   <<"To search using ID.......press i\n"
   <<"Enter now......:";

   cin.clear();
   cin.ignore();
   cin>>search;


   // id search
   if (search == 'i')
   {
       cout<<"Enter the ID for searching: ";
       cin>>s_id;

       int x = 0;

       for (j = 0; j <= n; j++)
       {
           if (array[j].id == s_id)
           {
               x = j;
               break;
           }
       }

       if (array[x].id == s_id)
       {
           cout<<"\nID has been found\n";
       }

       else
       {
           cout<<"\nID is not in the database\n";
       }
   }
}
void printing (stu_data array[], int & n)
{
   int x, i;

   cout<<"ID"<<setw(15)<<"Names"<<setw(20)<<"Sex"<<setw(7)<<"CGPA"<<setw(9)<<"Major";
   cout<<endl;

   for(i = 0; i < n; i++)
   {
       cout<<array[i].id<<"";
       cout<<setw(5)<<"";

       cout<<array[i].fn<<"";
       cout<<" "<<array[i].ln<<"";
       x = array[i].ln.length() + array[i].fn.length();
       cout<<setw(22 - x)<<"";


       cout<<array[i].gender<<"";
       cout<<setw(4)<<"";

       cout<<showpoint<<setprecision(3)<<array[i].cgpa<<"";
       cout<<setw(5)<<"";

       cout<<array[i].major<<"";
       x = array[i].major.length();
       cout<<setw(10 - x)<<"";

       cout<<endl;
   }
}
void dump (stu_data array[], int & n, ofstream &write)
{
   int i;

   for(i = 0; i < n; i++)
   {
       write<<array[i].id;
       write<<array[i].fn;
       write<<array[i].ln;
       write<<array[i].gender;
       write<<array[i].cgpa;
       write<<array[i].major<<endl;
   }
}

EDIT: código adaptado de http://www.cplusplus.com/forum/beginner/41740/

Link to comment
Share on other sites

O programa compila sem erros.

A não ser que isso seja um trabalho da aula de inglês, parece não ter sido feito por ti...

Diz o que queres fazer e onde tens o problema.

Nota: Seria bom quando apresentarem código de terceiros referir a fonte. O autor do original ficaria agradecido.

por exemplo: código fornecido por Maestro em http://www.cplusplus.com/forum/beginner/41740/ onde indicou que tinha problema na função load.

Edited by FreiNando

O caminho mais curto para conseguir fazer muitas coisas é fazer uma de cada vez. Samuel Smiles

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.