Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. const char FILE_NAME[] = "list.csv";
  7.  
  8. struct Student {
  9.     int num;
  10.     char* name;
  11.     Student* next;
  12. };
  13.  
  14. void addItemToList(Student* &theList, Student* theNewStudent){
  15.     //(*theNewStudent).next = theList;
  16.     theNewStudent->next = theList;
  17.     theList = theNewStudent;
  18.  
  19. }
  20.  
  21. Student* createStudent(int num, char name[]){
  22.     Student* newItem = new Student;
  23.     newItem->num = num;
  24.     newItem->name = new char[256];
  25.     strcpy(newItem->name, name);
  26.     newItem->next = nullptr;
  27.     return newItem;
  28. }
  29.  
  30. void printList(Student* theList){
  31.     Student* it = theList;
  32.     while (it != nullptr){
  33.         cout << it->num << ": " << it->name << endl;
  34.         it = it->next;
  35.     }
  36. }
  37.  
  38. Student* inputItem(){
  39.     int num;
  40.     char name[256];
  41.    
  42.     cout << "Type the number: ";
  43.     cin >> num;
  44.  
  45.     cout << "Type the name: ";
  46.     cin >> name;
  47.  
  48.     return createStudent(num, name);
  49. }
  50.  
  51. void saveList(Student* theList){
  52.     ofstream out;
  53.     out.open(FILE_NAME);
  54.  
  55.     Student* it = theList;
  56.     while (it != nullptr){
  57.         out << '"' << it->num << "\";\"" << it->name << '"' << endl;
  58.         //out << it->num << " " << it->name << endl;
  59.         it = it->next;
  60.     }
  61.  
  62.     out.close();
  63.  
  64.     cout << "List saved as " << FILE_NAME << ".\n";
  65. }
  66.  
  67. void loadList(Student* &ourList){
  68.     ifstream in;
  69.     in.open(FILE_NAME);
  70.  
  71.     char line[4096];
  72.     while (in.getline(line, 4096)){
  73.         char* a = &(line[1]);
  74.         int num = 0;
  75.         while (*a != '"'){
  76.             num += *a - '0';
  77.             a++;
  78.  
  79.             if (*a != '"'){
  80.                 num *= 10;
  81.             }
  82.         }
  83.  
  84.         char name[256];
  85.         a += 3;
  86.         int pos = 0;
  87.         while (*a != '"'){
  88.             name[pos] = *a;
  89.             pos++;
  90.             *a++;
  91.         }
  92.         name[pos] = '\0';
  93.  
  94.         addItemToList(ourList, createStudent(num, name));
  95.  
  96.     }
  97.  
  98.  
  99.  
  100.     in.close();
  101.  
  102. }
  103.  
  104. int main(){
  105.     Student* ourList = nullptr;
  106.  
  107.     unsigned ans = 1;
  108.  
  109.     while (true){
  110.         cout << " \n------------------------------------------\n\n";
  111.         cout << "Select an option: \n";
  112.         cout << " [1] - Display List\n";
  113.         cout << " [2] - Insert Item\n";
  114.         cout << " [3] - Save List\n";
  115.         cout << " [4] - Load List\n";
  116.         cout << " [0] - Exit\n\n";
  117.         cout << "Choice: ";
  118.         cin >> ans;
  119.         cout << endl;
  120.  
  121.         switch (ans){
  122.         case 1:
  123.             printList(ourList);
  124.             break;
  125.  
  126.         case 2:
  127.             addItemToList(ourList, inputItem());
  128.             break;
  129.  
  130.         case 3:
  131.             saveList(ourList);
  132.             break;
  133.  
  134.         case 4:
  135.             loadList(ourList);
  136.             break;
  137.  
  138.         case 0:
  139.             return 0;
  140.             break;
  141.  
  142.         default:
  143.             cout << "Command not supported.\n\n\n";
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement