Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstring>
- #include <fstream>
- using namespace std;
- const char FILE_NAME[] = "list.csv";
- struct Student {
- int num;
- char* name;
- Student* next;
- };
- void addItemToList(Student* &theList, Student* theNewStudent){
- //(*theNewStudent).next = theList;
- theNewStudent->next = theList;
- theList = theNewStudent;
- }
- Student* createStudent(int num, char name[]){
- Student* newItem = new Student;
- newItem->num = num;
- newItem->name = new char[256];
- strcpy(newItem->name, name);
- newItem->next = nullptr;
- return newItem;
- }
- void printList(Student* theList){
- Student* it = theList;
- while (it != nullptr){
- cout << it->num << ": " << it->name << endl;
- it = it->next;
- }
- }
- Student* inputItem(){
- int num;
- char name[256];
- cout << "Type the number: ";
- cin >> num;
- cout << "Type the name: ";
- cin >> name;
- return createStudent(num, name);
- }
- void saveList(Student* theList){
- ofstream out;
- out.open(FILE_NAME);
- Student* it = theList;
- while (it != nullptr){
- out << '"' << it->num << "\";\"" << it->name << '"' << endl;
- //out << it->num << " " << it->name << endl;
- it = it->next;
- }
- out.close();
- cout << "List saved as " << FILE_NAME << ".\n";
- }
- void loadList(Student* &ourList){
- ifstream in;
- in.open(FILE_NAME);
- char line[4096];
- while (in.getline(line, 4096)){
- char* a = &(line[1]);
- int num = 0;
- while (*a != '"'){
- num += *a - '0';
- a++;
- if (*a != '"'){
- num *= 10;
- }
- }
- cout << line << " ---- number is: " << num << endl;
- }
- in.close();
- }
- int main(){
- Student* ourList = nullptr;
- unsigned ans = 1;
- while (true){
- cout << " \n------------------------------------------\n\n";
- cout << "Select an option: \n";
- cout << " [1] - Display List\n";
- cout << " [2] - Insert Item\n";
- cout << " [3] - Save List\n";
- cout << " [4] - Load List\n";
- cout << " [0] - Exit\n\n";
- cout << "Choice: ";
- cin >> ans;
- cout << endl;
- switch (ans){
- case 1:
- printList(ourList);
- break;
- case 2:
- addItemToList(ourList, inputItem());
- break;
- case 3:
- saveList(ourList);
- break;
- case 4:
- loadList(ourList);
- break;
- case 0:
- return 0;
- break;
- default:
- cout << "Command not supported.\n\n\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement