Review my code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 2
Reputation: Holy Roller is an unknown quantity at this point 
Solved Threads: 0
Holy Roller Holy Roller is offline Offline
Newbie Poster

Review my code

 
0
  #1
Dec 8th, 2008
Hey, I'm somewhat new to programming in c++ and I have had this code I wrote for a long time now. I wrote this about 9 months ago when I was really interested in learning the c++ language but lost interest during the summer. Well, now I'm getting back into it and I would like to know what you think of this code. Its a simple guessing game and its pretty novice. What I would like to know is what you think of it; is there room for improvement? Is it sloppy? Am I not fully utilizing what c++ has to offer? Just tell me what you think.

  1. /*
  2. Guessing Game
  3. By Jacob
  4. */
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <time.h>
  9. #include <string.h>
  10. #include <fstream>
  11. #include <windows.h>
  12. #include <STDLIB.H>
  13. #include <conio.h>
  14. #include "pause.h"
  15. #include "clrscr.h"
  16.  
  17. using namespace std;
  18.  
  19. int main(void)
  20. {
  21. SetConsoleTitle( "Guessing Game By Jacob" );
  22.  
  23. int number;
  24. int guess;
  25. int i=0;
  26. int x=8;
  27. int intro;
  28. double diff;
  29. char name[256];
  30. char choice[10];
  31.  
  32. srand(time(0));
  33. number=rand()%100+1;
  34. time_t start,end;
  35.  
  36. cout<<"MENU"<<endl;
  37. cout<<"------------------------------------------------------------"<<endl;
  38. cout<<"Type the number that corresponds with an available option."<<endl;
  39. cout<<"------------------------------------------------------------"<<endl;
  40. cout<<"1)Play Game"<<endl; cout<<"2)Restart Score"<<endl; cout<<"3)Exit"<<endl;
  41. cin>>intro;
  42.  
  43. if(intro==3)
  44. { EXIT_SUCCESS; }
  45.  
  46. else if(intro==2)
  47. {
  48. cls();
  49. ofstream score("score.txt", ios::trunc);
  50. cout<<"Score list reset."<<endl;
  51. cout<<"Press any key to continue. . ."<<endl;
  52. getch();
  53. cls();
  54. return main();
  55. }
  56.  
  57. else if(intro==1)
  58. {
  59. cls();
  60. time (&start);
  61. while (guess!=number)
  62. {
  63. cout<<""<<x<<" more tries"<<endl;
  64. cout<<"Pick a number:"<<endl<<endl;
  65. cin>>guess;
  66. cin.ignore();
  67.  
  68. if(x==0)
  69. {
  70. time(&end);
  71. cls();
  72. cout<<"You are out of turns. The correct number was "<<number<<"."<<endl;
  73. cout<<"Would you like to restart? (Y/N)"<<endl;
  74. cin.getline(choice,10);
  75.  
  76. for(int y=0; y!=1;)
  77. {
  78. if(strcmp(choice, "y")==0)
  79. {
  80. cls();
  81. return main();
  82. y++;
  83. }
  84.  
  85. if(strcmp(choice, "n")==0)
  86. { return 0; }
  87.  
  88. else
  89. { cin.getline(choice, 10); continue; }
  90. }
  91. }
  92.  
  93. if(guess<number)
  94. {
  95. cls();
  96. cout<<"Too low!"<<endl<<endl;
  97. x--;
  98. i++;
  99. }
  100.  
  101. else if(guess>number)
  102. {
  103. cls();
  104. cout<<"Too high!"<<endl<<endl;
  105. x--;
  106. i++;
  107. }
  108.  
  109. else
  110. {
  111. time(&end);
  112. diff=difftime(end,start);
  113. i++;
  114. float math=diff/i*1000;
  115. cout<<"Good job, you guessed the correct number!"<<endl<<endl;
  116. ofstream score("score.txt", ios::app);
  117. cout<<"You scored "<<math<<" points. Please enter your name so I can save your score:"<<endl;
  118. cin.getline(name,256);
  119. cls();
  120. score<<""<<name<<" scored "<<math<<" points."<<endl;
  121. cout<<"Check the score.txt file to see how you did."<<endl<<endl;
  122. cout<<"Would you like to restart? (Y/N)"<<endl;
  123. cin.getline(choice,10);
  124.  
  125. for(int z=0; z!=1;)
  126. {
  127. if(strcmp(choice, "y")==0)
  128. {
  129. cls();
  130. return main();
  131. z++;
  132. }
  133.  
  134. if(strcmp(choice, "n")==0)
  135. { return 0; }
  136.  
  137. else
  138. { cin.getline(choice, 10); continue; }
  139. }
  140. }
  141. }
  142. }
  143. else
  144. { cls(); return main(); }
  145. }

Sorry for the 145 lines, lol.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Review my code

 
0
  #2
Dec 9th, 2008
You are using some nonstandard #include files and you have both stdlib.h and cstdlib:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string.h>
#include <fstream>
#include <windows.h>
#include <STDLIB.H>
#include <conio.h>
#include "pause.h"
#include "clrscr.h"

I had to take all of the cls commands out because my compiler doesn't have that. Similarly, if the person isn't running Windows, I doubt they'll have windows.h. You have some .h files in there too. This is C++, so to be consistent, use the C++ headers. (i.e. string versus string.h, ctime versus time.h, etc.).

You can use strings instead of C-style strings for the name. I think getch () is from conio.h, which isn't standard, so you can use cin.get () if possible for portability.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Review my code

 
0
  #3
Dec 9th, 2008
Never Ever call main() as you are doing in your return statements. Use a loop. Ony the operating system should be calling main()
Use a lot more whitespace to make your code easier to read. In other words, change
  1. if(strcmp(choice, "y")==0)
  2. math=diff/i*1000;
  3. cout<<""<<x<<" more tries"<<endl;
to
  1. if (strcmp(choice, "y") == 0)
  2. math = (diff / i) * 1000;
  3. cout << "" << x << " more tries" << endl;
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Review my code

 
0
  #4
Dec 9th, 2008
Also try to keep your indentation a standard width - usually 3 or 4 spaces is good.

As for the non-standard includes as mentioned above, I'm guessing you are using something old like borland or turbo C... I would recommend you start using either VC++ or Code::Blocks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: Holy Roller is an unknown quantity at this point 
Solved Threads: 0
Holy Roller Holy Roller is offline Offline
Newbie Poster

Re: Review my code

 
0
  #5
Dec 9th, 2008
Thanks guys.

About the cls commands, I'm using a function from the clrscr.h file; is it not portable? The author said it was an alternative to using system("pause"). VernonDozier, the getch() should be from the pause.h file, also an alternative to using conio.h. I'm not really sure how that works since they are the same syntax but, like I said, the summer came around and I got lazy

Well, I appreciate all the comments. The reason I wanted to know is because I'm rewriting the entire program to get back into practice.

Thank you everyone.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Review my code

 
0
  #6
Dec 9th, 2008
Originally Posted by Holy Roller View Post
Thanks guys.

About the cls commands, I'm using a function from the clrscr.h file; is it not portable? The author said it was an alternative to using system("pause"). VernonDozier, the getch() should be from the pause.h file, also an alternative to using conio.h. I'm not really sure how that works since they are the same syntax but, like I said, the summer came around and I got lazy

Well, I appreciate all the comments. The reason I wanted to know is because I'm rewriting the entire program to get back into practice.

Thank you everyone.
Look at this thread, particularly WaltP's link in this thread, regarding "cls" and system("PAUSE") . cin.get () is a better approach for a simple pause.
http://www.daniweb.com/forums/thread74248.html

These links from Narue and Amadeus pinned to the top of Daniweb and dreamincode.net, respectively, are helpful discussions of portability of pausing, flushing input streams, etc., and may be of interest, mainly discussing portability issues.

http://www.daniweb.com/forums/thread90228.html
http://www.dreamincode.net/forums/showtopic30581.htm
Last edited by VernonDozier; Dec 9th, 2008 at 12:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC