Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <limits>
  11. #include <cctype>
  12. #include <cmath>
  13. #include <Windows.h>
  14.  
  15. using namespace std;
  16. void rgs();
  17. void Menu();
  18. string Login();
  19. int monsters();
  20. void loading();
  21. void edit(string user,int obexp);
  22. void qns1();
  23. void qns2();
  24. void qns3();
  25. void qns4();
  26. void qns5();
  27. void qns6();
  28. void qns7();
  29. void qns8();
  30. void qns9();
  31. void qns10();
  32. int ErrorCheck();
  33.  
  34. int ans1, cLevel, cExp;
  35. string charname, pw, user, pass;
  36. bool access;
  37.  
  38.  
  39. int main()
  40. {
  41. loading();
  42. Sleep(3000);
  43. system("CLS");
  44. Menu();
  45. system("CLS");
  46. if (ans1==1)
  47. {
  48. Login();
  49. }
  50. else if (ans1==2)
  51. {
  52. rgs();
  53. system("CLS");
  54. main();
  55. }
  56. else
  57. {
  58. cout<<"You have entered an invalid option"<<endl;
  59. }
  60. system ("pause");
  61. return 0;
  62. }
  63. string Login()
  64. {
  65. cout<<"Please enter your charater name: ";
  66. cin>>user;
  67. cout<<"Please enter your password: ";
  68. cin>>pass;
  69.  
  70. int qexp;
  71. ifstream inFile;
  72. string username, password;
  73. inFile.open("users.txt");
  74. if (inFile.is_open())
  75. {
  76. while (!inFile.eof())
  77. {
  78. inFile >>username>>password>>cLevel>>cExp;
  79. if (user==username && pass==password)
  80. {
  81. access=true;
  82. break;
  83. }
  84. }
  85. if (access)
  86. {
  87. cout<<"You have login successfully!"<<endl;
  88. Sleep(2000);
  89. system("CLS");
  90. cout<<"You are in a fight now. Solve this question to defeat the monster!"<<endl;
  91. qexp=monsters();
  92. cout<<endl;
  93. if (cLevel=1)
  94. {qns1();
  95. edit(user,qexp);}
  96. else if (cLevel=2)
  97. {qns2();
  98. edit(user,qexp);}
  99. else if (cLevel=3)
  100. {qns3();
  101. edit(user,qexp);}
  102. else if (cLevel=4)
  103. {qns4();
  104. edit(user,qexp);}
  105. else if (cLevel=5)
  106. {qns5();
  107. edit(user,qexp);}
  108. else if (cLevel=6)
  109. {qns6();
  110. edit(user,qexp);}
  111. else if (cLevel=7)
  112. {qns7();
  113. edit(user,qexp);}
  114. else if (cLevel=8)
  115. {qns8();
  116. edit(user,qexp);}
  117. else if (cLevel=9)
  118. {qns9();
  119. edit(user,qexp);}
  120. else
  121. {qns10();
  122. edit(user,qexp);}
  123. }
  124. else
  125. {
  126. cout<<"You have enter the wrong username/password!"<<endl;
  127. cout<<"Please try again!"<<endl;
  128. Sleep(2000);
  129. system("CLS");
  130. Login();
  131. }
  132. }
  133. else
  134. {
  135. cout<<"Error!"<<endl;
  136. }
  137. inFile.close();
  138. return user;
  139.  
  140.  
  141. }
  142.  
  143. void Menu()
  144. {
  145. cout<<"Welcome to dungeon hunter!"<<endl;
  146. cout<<"Choose a choice below."<<endl;
  147. cout<<"1. Login"<<endl;
  148. cout<<"2. Register a new account"<<endl;
  149. cout<<"Please enter your option: ";
  150. cin>>ans1;
  151. }
  152.  
  153. void rgs()
  154. {
  155. ifstream inFile;
  156. ofstream newuser;
  157. string username, password, passwordconfirm;
  158. inFile.open("users.txt", ios::app);
  159. newuser.open("users.txt", ios::app);
  160. bool uservalid=false;
  161. while (!uservalid)
  162. {
  163. cout << "Username: ";
  164. cin >> username;
  165. cout << "Password: ";
  166. cin >> password;
  167. cout << "Confirm password: ";
  168. cin >> passwordconfirm;
  169. int m=0;
  170. int k=0;
  171. while (inFile>>user>>pass)
  172. {
  173. m++;
  174. if (username!=user)
  175. k++;
  176. }
  177. if (m==k && password==passwordconfirm)
  178. uservalid=true;
  179. else if (m!=k)
  180. cout << "There is already a user with this username." << endl;
  181. else
  182. cout << "The passwords given do not match." << endl;
  183. }
  184. newuser << username << " " << password << " 1 0" << endl;
  185. cout<<"You have successfully registered an account!"<<endl;
  186. Sleep(3000);
  187. inFile.close();
  188. newuser.close();
  189. }
  190.  
  191. int monsters()
  192. {
  193. ifstream monstersFile;
  194. int mLevel, mExpA, mExpB, mExp;
  195. srand((unsigned int)time(NULL));//initialize random number generator;
  196. string monsters[10]={"Sandton","Tentacuno","Porypix","Snorbell","Paradon","Muqueen","Maromander","Nidodash","Hitmonchop","Voltchamp"};
  197. int r = rand()%10+1; //generate a number between 1-1
  198.  
  199. monstersFile.open("monsters.txt");
  200. if(!monstersFile)
  201. cout<<"Not able to open text file\n";
  202. else
  203. {
  204. cout<<left<<setw(15)<<"Name"<<setw(15)<<"Level"<<"EXP"<<endl;
  205. while(!monstersFile.eof())
  206. {
  207. monstersFile>>mLevel>>mExpA>>mExpB;
  208. if(monstersFile.fail())
  209. {
  210. break;
  211. }
  212. else
  213. {
  214. if(cLevel==mLevel)
  215. {
  216. mExp=rand()%(mExpB-mExpA+1)+mExpA;
  217. cout<<left<<setw(15)<<monsters[r]<<setw(15)<<mLevel<<setw(15)<<mExp<<endl;
  218. }
  219.  
  220. }
  221.  
  222. }
  223.  
  224. }
  225. monstersFile.close();
  226. return mExp;
  227. }
  228.  
  229. void loading()
  230. {
  231. cout<<"Welcome to dungeon ma(th)ster!"<<endl;
  232. cout << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)255 << (char) 255<< (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << endl;
  233. cout << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << endl;
  234. cout << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << endl;
  235. cout << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << endl;
  236. cout << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << endl;
  237. cout << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << endl;
  238. cout << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << endl;
  239. cout << (char)255 << (char)255 << (char)255 << (char)255 << (char)255 << (char)255 << (char)178 << (char)178 << (char)255 << (char)255 << (char)255 << (char)255 << (char)255 << (char)255 << endl;
  240. cout << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << endl;
  241. cout << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << endl;
  242. cout << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << endl;
  243. cout << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)178 << (char)178 << endl;
  244. cout << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << endl;
  245. cout << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << (char)255 << (char)255 << (char)255 << (char)255 << (char)178 << (char)178 << (char)178 << (char)178 << (char)178 << endl;
  246.  
  247. }
  248.  
  249. void edit(string user,int obexp)
  250. {
  251. string username,pass,search;
  252. int lvl,nlvl, exp, nexp, mExp;
  253. ifstream ifile ("users.txt");
  254. ofstream ofile ("users2.txt");
  255. while (!ifile.eof())
  256. {
  257. ifile>>username>>pass>>lvl>>exp;
  258. nexp=exp+obexp;
  259. if(user==username)
  260. {
  261. if (nexp>0)
  262. {nlvl=1;}
  263. else if (nexp<1500)
  264. {nlvl=2;}
  265. else if (nexp<2200)
  266. {nlvl=3;}
  267. else if (nexp<3300)
  268. {nlvl=4;}
  269. else if (nexp<4500)
  270. {nlvl=5;}
  271. else if (nexp<6500)
  272. {nlvl=6;}
  273. else if (nexp<8700)
  274. {nlvl=7;}
  275. else if (nexp<8700)
  276. {nlvl=8;}
  277. else if (nexp<11200)
  278. {nlvl=9;}
  279. else if (nexp<14200)
  280. {nlvl=10;}
  281. else
  282. {nlvl=lvl;}
  283.  
  284. ofile<<endl<<user<<" "<<pass<<" "<<nlvl<<" "<<nexp;
  285. }
  286. else
  287. {
  288. ofile<<endl<<user<<" "<<pass<<" "<<lvl<<" "<<exp;
  289. }
  290. cout<<"\r";
  291. ifile.close();
  292. ofile.close();
  293. remove("users.txt");
  294. int result=rename("users2.txt","users.txt");
  295. if (result==0)
  296. cout<<"File successfully renamed\n";
  297. else
  298. {
  299. while (result==0)
  300. {
  301. cout<<"Error renaming file, Retrying\n";
  302. result=rename("users2.txt","users.txt");
  303. }
  304. }
  305. }
  306. }
  307.  
  308. void qns1() // plus
  309. {
  310. srand (time(NULL));
  311. int qn1 = rand() % 200 + 1;
  312. int qn2 = rand() % 100 + 1;
  313. int input;
  314. int qnsans;
  315. cout<<"Here is your question!"<<endl;
  316. cout<< qn1 <<"+"<< qn2<<endl;
  317. qnsans=qn1+qn2;
  318. bool Restart=true;
  319. while(Restart==true)
  320. {
  321. cout<<"Enter your answer : ";
  322. cin>>input;
  323. Restart = cin.fail();
  324. if(cin.fail()==true)
  325. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  326. else if(qnsans==input)// if no error is present
  327. {
  328. cout<<"You Won the round!"<<endl;
  329. Restart = false; // to allow do loop to end
  330. }
  331. else if(qnsans!=input)
  332. {
  333. cout<<"try again"<<endl;
  334. Restart = true;
  335. }
  336. else
  337. {
  338. cout<<"try again"<<endl;
  339. Restart = true;
  340. }
  341. cin.clear(); //Restart error flag
  342. }
  343.  
  344. }
  345. //plus
  346. void qns2() //minus
  347. {
  348. srand (time(NULL));
  349. int qn1 = rand() % 100 + 1;
  350. int qn2 = rand() % 100 + 1;
  351. int input;
  352. int qnsans;
  353. cout<<"Here is your question!"<<endl;
  354. cout<< qn1 <<"-"<< qn2<<endl;
  355. qnsans= qn1-qn2;
  356. bool Restart=true;
  357. while(Restart==true)
  358. {
  359. cout<<"Enter your answer : ";
  360. cin>>input;
  361. Restart = cin.fail();
  362. if(cin.fail()==true)
  363. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  364. else if(qnsans==input)// if no error is present
  365. {
  366. cout<<"You Won the round!"<<endl;
  367. Restart = false; // to allow do loop to end
  368. system("Pause");
  369. }
  370. else if(qnsans!=input)
  371. {
  372. cout<<"try again"<<endl;
  373. Restart = true;
  374. }
  375. else
  376. {
  377. cout<<"try again"<<endl;
  378. Restart = true;
  379. }
  380. cin.clear(); //Restart error flag
  381. }
  382.  
  383. } // minus
  384. //minus
  385. void qns3()//times
  386. {
  387. srand (time(NULL));
  388. int qn1 = rand() % 20 + 1;
  389. int qn2 = rand() % 10 + 1;
  390. int input;
  391. int qnsans;
  392. cout<<"Here is your question!"<<endl;
  393. cout<< qn1 <<"*"<< qn2<<endl;
  394. qnsans= qn1*qn2;
  395. bool Restart=true;
  396. while(Restart==true)
  397. {
  398. cout<<"Enter your answer : ";
  399. cin>>input;
  400. Restart = cin.fail();
  401. if(cin.fail()==true)
  402. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  403. else if(qnsans==input)// if no error is present
  404. {
  405. cout<<"You Won the round!"<<endl;
  406. Restart = false; // to allow do loop to end
  407. system("Pause");
  408. }
  409. else if(qnsans!=input)
  410. {
  411. cout<<"try again"<<endl;
  412. Restart = true;
  413. }
  414. else
  415. {
  416. cout<<"try again"<<endl;
  417. Restart = true;
  418. }
  419. cin.clear(); //Restart error flag
  420. }
  421.  
  422. }
  423. //times
  424. void qns4()//plusplus
  425. {
  426. srand (time(NULL));
  427. double qn1 = rand() % 200 + 1;
  428. double qn2 = rand() % 100 + 1;
  429. double QN3 = rand()%100+1;
  430. double input;
  431. double qnsans;
  432. cout<<"Here is your question!"<<endl;
  433. cout<< qn1 <<"+"<< qn2<<"+"<<QN3<<endl;
  434. qnsans= qn1+qn2+QN3;
  435. bool Restart=true;
  436. while(Restart==true)
  437. {
  438. cout<<"Enter your answer : ";
  439. cin>>input;
  440. Restart = cin.fail();
  441. if(cin.fail()==true)
  442. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  443. else if(qnsans==input)// if no error is present
  444. {
  445. cout<<"You Won the round!"<<endl;
  446. Restart = false; // to allow do loop to end
  447. system("Pause");
  448. }
  449. else if(qnsans!=input)
  450. {
  451. cout<<"try again"<<endl;
  452. Restart = true;
  453. }
  454. else
  455. {
  456. cout<<"try again"<<endl;
  457. Restart = true;
  458. }
  459. cin.clear(); //Restart error flag
  460. }
  461.  
  462. }
  463. //plusplus
  464. void qns5()
  465. {
  466. srand (time(NULL));
  467. double qn1 = rand() %200 + 1;
  468. double qn2 = rand() % 100 + 1;
  469. double QN3 = rand()%100+1;
  470. double input;
  471. double qnsans;
  472. cout<<"Here is your question!"<<endl;
  473. cout<< qn1 <<"-"<< qn2<<"+"<<QN3<<endl;
  474. qnsans= qn1-qn2+QN3;
  475. bool Restart=true;
  476. while(Restart==true)
  477. {
  478. cout<<"Enter your answer : ";
  479. cin>>input;
  480. Restart = cin.fail();
  481. if(cin.fail()==true)
  482. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  483. else if(qnsans==input)// if no error is present
  484. {
  485. cout<<"You Won the round!"<<endl;
  486. Restart = false; // to allow do loop to end
  487. system("Pause");
  488. }
  489. else if(qnsans!=input)
  490. {
  491. cout<<"try again"<<endl;
  492. Restart = true;
  493. }
  494. else
  495. {
  496. cout<<"try again"<<endl;
  497. Restart = true;
  498. }
  499. cin.clear(); //Restart error flag
  500. }
  501.  
  502. }
  503. //minusplus
  504. void qns6()
  505. {
  506. srand (time(NULL));
  507. double qn1 = rand() % 200 + 1;
  508. double qn2 = rand() % 20 + 1;
  509. double QN3 = rand() % 10+1;
  510. double input;
  511. double qnsans;
  512. cout<<"Here is your question!"<<endl;
  513. cout<< qn1 <<"+"<< qn2<<"*"<<QN3<<endl;
  514. qnsans= qn1+(qn2*QN3);
  515. bool Restart=true;
  516. while(Restart==true)
  517. {
  518. cout<<"Enter your answer : ";
  519. cin>>input;
  520. Restart = cin.fail();
  521. if(cin.fail()==true)
  522. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  523. else if(qnsans==input)// if no error is present
  524. {
  525. cout<<"You Won the round!"<<endl;
  526. Restart = false; // to allow do loop to end
  527. system("Pause");
  528. }
  529. else if(qnsans!=input)
  530. {
  531. cout<<"try again"<<endl;
  532. Restart = true;
  533. }
  534. else
  535. {
  536. cout<<"try again"<<endl;
  537. Restart = true;
  538. }
  539. cin.clear(); //Restart error flag
  540. }
  541.  
  542. }
  543. //plus times
  544. void qns7()
  545. {
  546. srand (time(NULL));
  547. double qn1 = rand() % 200 + 1;
  548. double qn2 = rand() % 20 + 1;
  549. double QN3 = rand() % 10+1;
  550. double input;
  551. double qnsans;
  552. cout<<"Here is your question!"<<endl;
  553. cout<< qn1 <<"-"<< qn2<<"*"<<QN3<<endl;
  554. qnsans= qn1-(qn2*QN3);
  555. bool Restart=true;
  556. while(Restart==true)
  557. {
  558. cout<<"Enter your answer : ";
  559. cin>>input;
  560. Restart = cin.fail();
  561. if(cin.fail()==true)
  562. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  563. else if(qnsans==input)// if no error is present
  564. {
  565. cout<<"You Won the round!"<<endl;
  566. Restart = false; // to allow do loop to end
  567. system("Pause");
  568. }
  569. else if(qnsans!=input)
  570. {
  571. cout<<"try again"<<endl;
  572. Restart = true;
  573. }
  574. else
  575. {
  576. cout<<"try again"<<endl;
  577. Restart = true;
  578. }
  579. cin.clear(); //Restart error flag
  580. }
  581.  
  582. }
  583. //minus times
  584. void qns8()
  585. {
  586. srand (time(NULL));
  587. double qn1 = rand() % 200 + 1;
  588. double qn2 = rand() % 30 + 1;
  589. double QN3 = rand() % 10+1;
  590. double QN4 = rand() % 10+1;
  591. double input;
  592. double qnsans;
  593. cout<<"Here is your question!"<<endl;
  594. cout<< qn1 <<"+"<< qn2<<"*"<<QN3<<"-"<<QN4<<endl;
  595. qnsans= qn1+qn2*QN3-QN4;
  596. bool Restart=true;
  597. while(Restart==true)
  598. {
  599. cout<<"Enter your answer : ";
  600. cin>>input;
  601. Restart = cin.fail();
  602. if(cin.fail()==true)
  603. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  604. else if(qnsans==input)// if no error is present
  605. {
  606. cout<<"You Won the round!"<<endl;
  607. Restart = false; // to allow do loop to end
  608. system("Pause");
  609. }
  610. else if(qnsans!=input)
  611. {
  612. cout<<"try again"<<endl;
  613. Restart = true;
  614. }
  615. else
  616. {
  617. cout<<"try again"<<endl;
  618. Restart = true;
  619. }
  620. cin.clear(); //Restart error flag
  621. }
  622.  
  623. }
  624. //plus times minus
  625. void qns9()
  626. {
  627. srand (time(NULL));
  628. double qn1 = rand() % 50 + 1;
  629. double qn2 = rand() % 20 + 1;
  630. double QN3 = rand() % 10+1;
  631. double QN4 = rand() % 10+1;
  632. double input;
  633. double qnsans;
  634. cout<<"Here is your question!"<<endl;
  635. cout<< qn1 <<"+"<< qn2<<"*"<<QN3<<"*"<<QN4<<endl;
  636. qnsans= qn1+qn2*QN3*QN4;
  637. bool Restart=true;
  638. while(Restart==true)
  639. {
  640. cout<<"Enter your answer : ";
  641. cin>>input;
  642. Restart = cin.fail();
  643. if(cin.fail()==true)
  644. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  645. else if(qnsans==input)// if no error is present
  646. {
  647. cout<<"You Won the round!"<<endl;
  648. Restart = false; // to allow do loop to end
  649. system("Pause");
  650. }
  651. else if(qnsans!=input)
  652. {
  653. cout<<"try again"<<endl;
  654. Restart = true;
  655. }
  656. else
  657. {
  658. cout<<"try again"<<endl;
  659. Restart = true;
  660. }
  661. cin.clear(); //Restart error flag
  662. }
  663.  
  664. }
  665. //plus timestimes
  666. void qns10()
  667. {
  668. srand (time(NULL));
  669. double qn1 = rand() % 200 + 1;
  670. double qn2 = rand() % 20 + 1;
  671. double QN3 = rand() % 10+1;
  672. double QN4 = rand() % 20+1;
  673. double QN5 = rand() % 20+1;
  674. double input;
  675. double qnsans;
  676. cout<<"Here is your question!"<<endl;
  677. cout<< "("<<qn1 <<"-"<< qn2<<")"<<"*"<<QN3<<"*"<<QN4<<"+"<<QN5<<endl;
  678. qnsans= (qn1-qn2)*QN3*QN4+QN5;
  679. bool Restart=true;
  680. while(Restart==true)
  681. {
  682. cout<<"Enter your answer : ";
  683. cin>>input;
  684. Restart = cin.fail();
  685. if(cin.fail()==true)
  686. cout<<"\nError, Invalid Choice, Please Key in a Numerical Value\n";
  687. else if(qnsans==input)// if no error is present
  688. {
  689. cout<<"You Won the round!"<<endl;
  690. Restart = false; // to allow do loop to end
  691. system("Pause");
  692. }
  693. else if(qnsans!=input)
  694. {
  695. cout<<"try again"<<endl;
  696. Restart = true;
  697. }
  698. else
  699. {
  700. cout<<"try again"<<endl;
  701. Restart = true;
  702. }
  703. cin.clear(); //Restart error flag
  704. }
  705.  
  706. }
  707. //(minus times) times plus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement