fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. //Two global variables for the "fixed" administrator name and password to be easily changed.
  8. char adminname[] = "admin";
  9. char adminpass[] = "pass";
  10.  
  11. class userscloud;
  12.  
  13. //Every object of the class User is one user of the network.
  14. class User {
  15.  
  16. char name[31],loginname[31],password[31];
  17. int id,friends[10],friendsnum;
  18.  
  19. public:
  20. char const* getname() const {return name;};
  21. char const* getloginname() const {return loginname;};
  22. char const* getpassword() const {return password;};
  23. int getid() const {return id;};
  24. void addfriend(int totusers);
  25. void delfriend();
  26. void printfriends() const;
  27. User(int ids);//Constructor for User-type objects.
  28. };
  29.  
  30. //A class that contains an array of pointers to each User object.
  31. class userscloud{
  32. User *usersarray;
  33. public:
  34. userscloud(int num);//Constructor for the userscloud object.
  35. User getuser(int temp) const;
  36. };
  37.  
  38. User userscloud::getuser(int temp) const{
  39. return usersarray[temp];
  40. }
  41.  
  42. userscloud::userscloud(int num){
  43.  
  44. if (num==1){ //If it's the first time here, allocate enough memory.
  45. if ( (usersarray=(User *)malloc (sizeof(User))) == NULL){
  46. cout<<"Not enough memory! \n";
  47. exit(1);
  48. }
  49. }
  50. else{ //Expand allocated memory.
  51. if ( (usersarray = (User *) realloc(usersarray,num*sizeof(User))) == NULL){
  52. cout<<"Not enough memory! \n";
  53. exit(2);
  54. }
  55. }
  56. usersarray[num-1]=User(num);
  57. }
  58.  
  59. User::User(int ids){ //User class constructor gets user's name, password and login name as inputs.
  60. cout << "Give name, login name and password for the new user ( <=30 characters eatch ) \n";
  61. cin >> name >> loginname >> password;
  62. id=ids;
  63. friendsnum=0;
  64. for (int l=0;l<9;l++){
  65. friends[l]=0;
  66. }
  67. }
  68.  
  69. int loginpross(userscloud Obj,int totusers){ //Gets a login name as input, finds if this login name exists, if yes then also checks the password.
  70. char temploginname[31], temppassword[31];
  71.  
  72. cout << "Give your login name. \n";
  73. cin >> temploginname;
  74.  
  75. if (!strcmp(temploginname,adminname)){
  76. cout << "Admin name was given. Give password to terminate program. \n";
  77. cin >> temppassword;
  78. if (!strcmp(temppassword,adminpass)){
  79. cout << "Admin password right! \n" << "Terminating program.\n";
  80. return 0;
  81. }
  82. cout << "Wrong password!";
  83. return -1;
  84. }
  85. for (int k=0;k<=(totusers-2);k++){
  86. if (!(strcmp(Obj.getuser(k).getname(),temploginname))){
  87. cout << "Give your password. \n";
  88. cin >> temppassword;
  89. if (!strcmp((Obj.getuser(k)).getpassword(),temppassword)){
  90. cout << "Login successful! \n" << "Welcome user: " << k << endl;
  91. return k;
  92. }
  93. cout << "Wrong password!";
  94. return -1;
  95. }
  96. }
  97. cout << "No such user found!";
  98. return -2;
  99. }
  100.  
  101. void User::addfriend(int totusers){ //In addfriend function the argument totusers is the number of total users + 1 (last user is actually the administrator).
  102. if (friendsnum > 9){
  103. cout << "Friends list is full! \n";
  104. return;
  105. }
  106. int tempid;
  107. cout << "Please enter friend's id number \n";
  108. cin >> tempid;
  109. if (tempid <= 0 || tempid > totusers){ //The id given from input is out of range.
  110. cout << "Invalid id \n";
  111. return;
  112. }
  113. friends[friendsnum]=tempid;
  114. friendsnum++;
  115. }
  116.  
  117. void User::delfriend(){
  118. if (friendsnum == 0){
  119. cout << "Friend's list is empty! \n";
  120. return;
  121. }
  122. int tempid;
  123. cout << "Which friend do you want to delete? \n";
  124. cin >> tempid;
  125. int counter=-1;
  126. for(int k=0;k<friendsnum;k++){
  127. if (friends[k]==tempid){
  128. friends[k]=friends[friendsnum-1];
  129. k--;
  130. friendsnum--;
  131. counter++;
  132. }
  133. }
  134. if (counter==-1){
  135. cout << "You have no such friend! \n";
  136. }
  137. else{
  138. cout << counter+1 << " friends deleted. \n";
  139. }
  140. }
  141.  
  142. void User::printfriends() const{
  143. if (friendsnum==0){
  144. cout << "Friend's list is empty! \n";
  145. return;
  146. }
  147. for (int k=0;k<friendsnum;k++){
  148. cout << k+1 << ": " << getname() << endl;
  149. }
  150. }
  151.  
  152. void searchuser(userscloud Obj,int totusers){
  153. char tempname[31];
  154.  
  155. cout << "Give the name of the user. \n";
  156. cin >> tempname;
  157. for (int k=0;k<=(totusers-1);k++){
  158. if (!(strcmp((Obj.getuser(k)).getname(),tempname))){
  159. cout << "Requested user found with id: " << (Obj.getuser(k)).getid() << endl;
  160. }
  161. }
  162. }
  163.  
  164. int main()
  165. {
  166. int newuser = 1;
  167. userscloud Obj(1); //Create a userscloud object.
  168.  
  169. while (1){
  170.  
  171. if( !(strcmp(Obj.getuser(newuser-1).getname(),adminname)) && !(strcmp(Obj.getuser(newuser-1).getpassword(),adminpass))){
  172. break;
  173. }
  174. newuser++;
  175. userscloud Obj(newuser);
  176. }
  177. while (1){
  178. int curruser = loginpross(Obj,newuser);
  179. if(curruser == 0){
  180. break;
  181. }
  182. if (curruser>0){
  183.  
  184. char inp;
  185. cout << "Add friend, Delete friend, Print friends, Search someone \n";
  186. cin >> inp;
  187. switch (inp){
  188. case 'A': case 'a': Obj.getuser(curruser).addfriend(newuser);
  189. break;
  190. case 'D': case 'd': Obj.getuser(curruser).delfriend();
  191. break;
  192. case 'P': case 'p': Obj.getuser(curruser).printfriends();
  193. break;
  194. case 'S': case 's': searchuser(Obj,newuser);
  195. break;
  196. default: cout << "Invalid option";
  197. break;
  198. }
  199. }
  200. }
  201. }
  202.  
Runtime error #stdin #stdout #stderr 0s 3440KB
stdin
Standard input is empty
stdout
Give name, login name and password for the new user ( <=30 characters eatch ) 
stderr
*** Error in `./prog': realloc(): invalid pointer: 0xb77dfd84 ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x75e72)[0xb755ee72]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(realloc+0x275)[0xb7562ad5]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(realloc+0x28b)[0xb7562aeb]
./prog[0x8048d92]
./prog[0x80489eb]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xf5)[0xb75028f5]
./prog[0x8048bd1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:03 1295711    /home/FMujdJ/prog
0804a000-0804b000 rw-p 00001000 08:03 1295711    /home/FMujdJ/prog
095a8000-095c9000 rw-p 00000000 00:00 0          [heap]
b74e7000-b74e9000 rw-p 00000000 00:00 0 
b74e9000-b7692000 r-xp 00000000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7692000-b7693000 ---p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7693000-b7695000 r--p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7695000-b7696000 rw-p 001ab000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7696000-b7699000 rw-p 00000000 00:00 0 
b7699000-b76b4000 r-xp 00000000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b76b4000-b76b5000 rw-p 0001a000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b76b5000-b76b6000 rw-p 00000000 00:00 0 
b76b6000-b76f7000 r-xp 00000000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76f7000-b76f8000 r--p 00040000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76f8000-b76f9000 rw-p 00041000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76f9000-b77d5000 r-xp 00000000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77d5000-b77d6000 ---p 000dc000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77d6000-b77da000 r--p 000dc000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77da000-b77db000 rw-p 000e0000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77db000-b77e2000 rw-p 00000000 00:00 0 
b77e3000-b77e8000 rw-p 00000000 00:00 0 
b77e8000-b77e9000 r-xp 00000000 00:00 0          [vdso]
b77e9000-b7808000 r-xp 00000000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b7808000-b7809000 r--p 0001f000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b7809000-b780a000 rw-p 00020000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
bf98e000-bf9a3000 rw-p 00000000 00:00 0          [stack]