Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // Notepad.cpp
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. // Method definition
  10. void addText(char** iopszString);
  11.  
  12. void main()
  13. {
  14.  
  15.     // Const definition
  16.     int const ADD    = 1;
  17.     int const UPDATE = 2;
  18.     int const DELETE = 3;
  19.     int const SAVE   = 4;
  20.     int const EXIT   = 5;
  21.    
  22.     // Variable definition
  23.     int nUserCode;
  24.    
  25.     // Code section
  26.  
  27.     // Gets the user code
  28.     cout << "Enter the code: " << endl;
  29.     cin >> nUserCode;
  30.    
  31.     // + "\0" so 1 minimum!!!
  32.     char* iopszString = new char[1];
  33.     iopszString = "";
  34.  
  35.     // Runs until the exit code
  36.     while (nUserCode != EXIT)
  37.     {
  38.         // Checks the exit code
  39.         switch (nUserCode)
  40.         {
  41.             case ADD:
  42.             {
  43.                 addText(&iopszString);
  44.                 cout << iopszString << endl;
  45.                 break;
  46.             }
  47.             case UPDATE:
  48.             {
  49.  
  50.                 break;
  51.             }
  52.             case DELETE:
  53.             {
  54.  
  55.                 break;
  56.             }
  57.             case SAVE:
  58.             {
  59.  
  60.                 break;
  61.             }
  62.             default:
  63.             {
  64.                 cout << "Wrong code, try again" << endl;
  65.  
  66.                 break;
  67.             }
  68.         }
  69.  
  70.         // Gets the user code
  71.         cout << "Enter the code: " << endl;
  72.         cin >> nUserCode;
  73.     }
  74.    
  75.     // Delete the string cuz heap
  76.     delete[] iopszString;
  77. }
  78.  
  79. void addText(char** iopszString)
  80. {
  81.     // Variables definition
  82.     int  nAddLength;
  83.  
  84.     // Code section
  85.    
  86.     // Gets the new length
  87.     cout << "Enter the length of the added string: " << endl;
  88.     cin >> nAddLength;
  89.  
  90.     // Always remember - the length you want+1!!
  91.     char* szNewString = new char[nAddLength+1];
  92.  
  93.     // Gets the new string
  94.     cout << "Enter the new string which you want to add: " << endl;
  95.     cin >> szNewString;
  96.  
  97.     // Creating a new string (result)
  98.     char* szResult = new char[nAddLength+1+strlen(*iopszString)];
  99.  
  100.     // Copies the old string to the new
  101.     strcpy(szResult, *iopszString);
  102.     strcat(szResult, szNewString);
  103.  
  104.     // Deletes the new string cuz we already copied
  105.     delete[] szNewString;
  106.  
  107.     // Exchange pointers
  108.     //strcpy(*iopszString, szResult); <--- never
  109.  
  110.     // The problem!
  111.     delete[] *iopszString;
  112.  
  113.     // Exchange pointer
  114.     *iopszString = szResult;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement