Advertisement
Guest User

Untitled

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