Question

Hi, I want to creat a file by the name osa_list that i can type 3...

Hi,

I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200.

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

using namespace std;

class Student

{

private:

string fname;

string lname;

string email;

  

  

public:

Student();

Student(string fname, string lname, string email);

string getfname() const

{

return fname;

}

string getlname() const

{

return lname;

}

  

string getemail() const

{

return email;

}

  

void setfname(string fname)

{

this->fname = fname;

}

void setlname(string lname)

{

this->lname= lname;

}

void setemail(string email)

{

this->email = email;

}

friend void Sort(Student studentlist[], int size);

friend ostream& operator<<(ostream &, Student slist[]);

};

Student::Student()

{

fname = "";

lname = "";

email = "";

}

Student::Student( string fname, string lname, string email)

{

this->fname = fname;

this->lname = lname;

this->email = email;

}

void Sort(Student studentlist[], int size)

{

for (int i = 0; i < size - 1; i++)

{

for (int j = 0; j < size - i - 1; j++)

{

  

Student temp = studentlist[j];

studentlist[j] = studentlist[j + 1];

studentlist[j + 1] = temp;

  

}

}

}

ostream& operator<<(ostream &output, Student slist)

{

output << slist.getfname() << " ";

output << slist.getlname() << " ";

output << slist.getemail();

output << endl;

return output;

}

int main()

{

Student studentlist[200];

string fname, lname, email;

int size = 0;

  

ifstream input;

input.open("input.dat");

  

if (input.fail())

{

cout << "Error: file not found!" << endl;

exit (1);

}

  

else

{

while (input >> fname >> lname >> email)

{

  

studentlist[size]= Student(fname, lname, email);

size++;

}

}

  

Sort(studentlist, size);

  

ofstream output;

output.open("output.dat");

  

if (output.fail())

{

cout << "Error: file not found!" << endl;

exit(1);

}

  

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

{

output << studentlist[i];

}

  

input.close();

output.close();

  

cout << "Your file is ready and has been transferred...." << endl;

  

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

using namespace std;

class Student

{

private:

string fname;

string lname;

string email;

public:

Student();

Student(string fname, string lname, string email);

string getfname() const

{

return fname;

}

string getlname() const

{

return lname;

}

string getemail() const

{

return email;

}

void setfname(string fname)

{

this->fname = fname;

}

void setlname(string lname)

{

this->lname = lname;

}

void setemail(string email)

{

this->email = email;

}

friend void Sort(Student studentlist[], int size);

friend ostream &operator<<(ostream &, Student slist[]);

};

Student::Student()

{

fname = "";

lname = "";

email = "";

}

Student::Student(string fname, string lname, string email)

{

this->fname = fname;

this->lname = lname;

this->email = email;

}

void Sort(Student studentlist[], int size)

{

for (int i = 0; i < size - 1; i++)

{

for (int j = 0; j < size - i - 1; j++)

{

Student temp = studentlist[j];

studentlist[j] = studentlist[j + 1];

studentlist[j + 1] = temp;

}

}

}

ostream &operator<<(ostream &output, Student slist)

{

output << slist.getfname() << " ";

output << slist.getlname() << " ";

output << slist.getemail();

output << endl;

return output;

}

int main()

{

Student studentlist[200];

string fname, lname, email;

int size = 0;

ifstream input;

input.open("input.dat");

if (input.fail())

{

cout << "Error: file not found!" << endl;

exit(1);

}

else

{

while (input >> fname >> lname >> email)

{

studentlist[size] = Student(fname, lname, email);

size++;

}

}

Sort(studentlist, size);

ofstream output;

output.open("output.dat");

if (output.fail())

{

cout << "Error: file not found!" << endl;

exit(1);

}

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

{

output << studentlist[i].getfname()<<" "<<studentlist[i].getlname()<<" "<<studentlist[i].getemail();

}

input.close();

output.close();

cout << "Your file is ready and has been transferred...." << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Hi, I want to creat a file by the name osa_list that i can type 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT