Advertisement
Guest User

whats happening!!!

a guest
Mar 8th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX_CART 100
  6.  
  7. typedef char string15[16];
  8. typedef char string20[21];
  9. typedef char string50[51];
  10. typedef char string8[9];
  11.  
  12. typedef struct {
  13. string20 first,middle,last;
  14. }nameType;
  15.  
  16. typedef struct{
  17. nameType name;
  18. string50 address;
  19. }userInfoType; //for user information
  20.  
  21. typedef struct{
  22. string8 code;
  23. int qty;
  24. }prodBoughtType; //for the cart
  25.  
  26. typedef prodBoughtType arrBought[MAX_CART];
  27. struct userTag{
  28.  
  29. string15 username,
  30. password;
  31. userInfoType info;
  32. char type;
  33. float creditlimit,
  34. outstanding;
  35. arrBought cart;
  36. int nItems;
  37. struct userTag *pNext;
  38. };
  39. typedef struct userTag userType;
  40. typedef userType * ptrUser;
  41.  
  42.  
  43.  
  44. void signUp (userType *pUser)
  45. {
  46. char cDump, choice, cTempType = '\0';
  47. char arrInputCode[9];
  48. char arrCompareCode[9] = {"DLSU2017"};
  49.  
  50.  
  51.  
  52. do{
  53. printf("Enter username: ");
  54. scanf("%s%c", (*pUser).username,&cDump); //placing the information directly within the container. Username is inside pUser
  55. }while (strlen((*pUser).username)<3 || strlen((*pUser).username)>15);
  56.  
  57. do{
  58. printf("Enter password: ");
  59. scanf("%s%c", (*pUser).password,&cDump); //placing the information directly within the container. Username is inside pUser
  60. if (strlen((*pUser).password)<6){
  61.  
  62. printf("Password too short, please try again. \n");
  63. printf("Enter password: ");
  64. scanf("%s%c", (*pUser).password,&cDump);
  65. }
  66. if (strlen((*pUser).password)>15){
  67.  
  68. printf("Password too long, please try again. \n");
  69. printf("Enter password: ");
  70. scanf("%s%c", (*pUser).password,&cDump);
  71. }
  72.  
  73. if (ValidPass(pUser)==1)
  74. pUser->password;
  75. else{
  76. printf("Error, password must contain non-letter character.\n");
  77. printf("Enter password: ");
  78. scanf("%s%c", (*pUser).password,&cDump);
  79. }
  80.  
  81.  
  82. }while (strlen((*pUser).password)<6 || strlen((*pUser).password)>15);
  83.  
  84.  
  85.  
  86.  
  87.  
  88. printf("Create [S]hopper or [A]dministrator account? \n");
  89. printf("Input: ");
  90. scanf("%c%c", &cTempType, &cDump);
  91.  
  92. if(cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
  93. printf("Invalid input, please try again: \n");
  94. while (cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
  95. printf("Input: ");
  96. scanf("%c%c",&cTempType, &cDump);
  97. }
  98. }
  99.  
  100.  
  101. if (cTempType == 's' || cTempType == 'S'){
  102.  
  103. (*pUser).type = 'S';
  104. printf("Account Type: Shopper \n");
  105.  
  106. }
  107.  
  108. else if (cTempType == 'a' || cTempType == 'A'){
  109.  
  110. printf("Input authorization code: ");
  111. while (strcmp(arrInputCode,arrCompareCode)!=0){
  112. scanf("%s%c", arrInputCode,&cDump);
  113.  
  114. if (strcmp(arrInputCode,arrCompareCode)==0){
  115.  
  116. (*pUser).type = 'A';
  117. //AdminChoice();
  118. printf("Account Type: Admin \n");
  119.  
  120. }
  121. else {
  122.  
  123. printf("Invalid authorization code, try again. \n");
  124. printf("Input authorization code: ");
  125. scanf("%s%c", arrInputCode,&cDump);
  126. }
  127. }
  128. }
  129.  
  130.  
  131. }
  132.  
  133. void getUserInfo (userInfoType *pInfo)
  134. {
  135.  
  136. printf("Enter name: \n");
  137. printf("First Name: ");
  138. fgets((*pInfo).name.first,21,stdin);
  139. printf("Middle Name: ");
  140. fgets((*pInfo).name.middle,21,stdin);
  141. printf("Last Name: ");
  142. fgets((*pInfo).name.last,51,stdin);
  143.  
  144. printf("%s,%s,%s \n", (*pInfo).name.last,(*pInfo).name.first,(*pInfo).name.middle);
  145.  
  146.  
  147.  
  148.  
  149. printf("Enter address: ");
  150. fgets((*pInfo).address,51,stdin);
  151. (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  152. (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  153. (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  154. (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  155.  
  156. }
  157.  
  158.  
  159. int ValidPass (userType *pUser)
  160. {
  161. int i, valid=0;
  162. /*while (i<strlen(pUser->password)){
  163.  
  164. if ((pUser->password[i]) < 'a' || (pUser->password[i]) > 'z')
  165. if ((pUser->password[i]) < 'A' || (pUser->password)[i] > 'Z')
  166. valid = 1;
  167. i++;
  168. }
  169. */
  170.  
  171. for(i=0;i<(strlen(pUser->password));i++){
  172.  
  173. if (pUser->password[i] < 'a' || pUser->password[i] >'z')
  174. if (pUser->password[i] < 'A' || pUser->password[i] >'Z')
  175. valid=1;
  176. }
  177. return valid;
  178.  
  179. }
  180.  
  181. ptrUser search (ptrUser pFirst, string15 username)
  182. {
  183. ptrUser pRun;
  184.  
  185. pRun = pFirst;
  186.  
  187. while (pRun != NULL && strcmp(username, pRun->username)!=0)
  188. pRun = pRun->pNext;
  189.  
  190. return pRun;
  191. }
  192.  
  193. void displayAll (userType *pUsers)
  194. {
  195. while (pUsers!=NULL){ //stop traversing when there is no more new address
  196.  
  197. printf("%s\n", pUsers->username);
  198. pUsers = pUsers->pNext; //node traversal
  199.  
  200. }
  201.  
  202.  
  203. }
  204.  
  205. void signIn (userType *pUsers)
  206. {
  207. int loggedin=0;
  208. char arrTempUser[16], arrTempPW[16], arrInputCode[9], arrUserCode[16];
  209. char authcode[9]={"DLSU2017"};
  210. char cDump;
  211. ptrUser pLogin, pPassword;
  212.  
  213. while (loggedin == 0){
  214.  
  215.  
  216. printf("Enter username: \n");
  217. fgets(arrTempUser,16,stdin);
  218.  
  219. printf("Enter password: \n");
  220. fgets(arrTempPW,16,stdin);
  221.  
  222.  
  223. pLogin = search(pUsers, pUsers->username);
  224. pPassword = search(pUsers, pUsers->password);
  225.  
  226. if (pLogin == NULL)
  227. printf("Not found \n");
  228.  
  229.  
  230. if (strcmp(pLogin->username, arrTempUser)==0){
  231.  
  232. if (strcmp(pPassword->password, arrTempPW)==0){
  233. printf("Username is fine \n");
  234. printf("Password is fine");
  235. loggedin = 1;
  236. }
  237. else
  238. printf("Something is wrong with password");
  239. /*pPassword = search(pUsers, pUsers->password);
  240. if (pPassword == NULL)
  241. printf("Not found");*/
  242. }
  243. else printf("Something is wrong with username");
  244.  
  245. if (pUsers->type == 'A'){
  246.  
  247. printf("Input authorization code: ");
  248. scanf("%s%c", arrInputCode, &cDump);
  249.  
  250. while (strcmp(arrInputCode,authcode)!=0){
  251. printf("Incorrect code, please try again: ");
  252. scanf("%s%c", arrInputCode, &cDump);
  253. }
  254.  
  255. loggedin = 1;
  256. printf("Admin menu");
  257. }
  258.  
  259. }
  260.  
  261. if (pUsers->type == 'S'){
  262.  
  263. printf("Shopper Menu");
  264. loggedin = 1;
  265. }
  266.  
  267. }
  268.  
  269.  
  270. void freeAll (ptrUser *pFirst)
  271. {
  272. userType *pDel;
  273.  
  274. while(*pFirst != NULL){
  275. pDel = *pFirst;
  276. *pFirst = (*pFirst)->pNext;
  277. free(pDel);
  278. }
  279.  
  280. }
  281.  
  282. void menu (userType *pUsers, userType *pNew, userType *pLast)
  283. {
  284. int opt,
  285. choice=0;
  286. char cDump;
  287.  
  288. printf("MAIN MENU \n");
  289. printf("[1] Sign Up \n");
  290. printf("[2] Log In \n");
  291. printf("Choice: ");
  292. scanf("%d%c", &choice,&cDump);
  293.  
  294. switch (choice){
  295. case 1: do {
  296.  
  297. pNew = malloc(sizeof(userType));
  298. signUp(pNew);
  299.  
  300.  
  301. (*pNew).pNext = NULL;
  302.  
  303. if (pUsers == NULL) //if initially empty
  304. pUsers = pNew; //pNew is the first node
  305. else //connect nodes to the list
  306. pLast->pNext = pNew;
  307.  
  308. pLast = pNew;
  309. getUserInfo (&pUsers->info);
  310. printf("Press [1] to create a new user or [0] to return to main menu: ");
  311. scanf("%d%c", &opt,&cDump);
  312.  
  313. if (opt == 0)
  314. menu (pUsers, pNew, pLast);
  315. }while(opt == 0);
  316.  
  317. break;
  318.  
  319. case 2: signIn(pUsers);
  320. break;
  321. default:freeAll(&pUsers);
  322. exit (0);
  323. }
  324.  
  325. }
  326.  
  327. int main ()
  328. {
  329.  
  330. userType *pUsers = NULL,
  331. *pNew,
  332. *pLast;
  333. ptrUser pRun, pTrail;
  334. menu (pUsers, pNew, pLast);
  335.  
  336.  
  337.  
  338. //displayAll(pUsers);
  339. freeAll(&pUsers);
  340.  
  341. return 0;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement