943,569 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 592
  • C++ RSS
Dec 8th, 2008
0

Review my code

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Holy Roller is offline Offline
2 posts
since Jun 2008
Dec 9th, 2008
0

Re: Review my code

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Dec 9th, 2008
0

Re: Review my code

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
cpp Syntax (Toggle Plain Text)
  1. if(strcmp(choice, "y")==0)
  2. math=diff/i*1000;
  3. cout<<""<<x<<" more tries"<<endl;
to
cpp Syntax (Toggle Plain Text)
  1. if (strcmp(choice, "y") == 0)
  2. math = (diff / i) * 1000;
  3. cout << "" << x << " more tries" << endl;
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,716 posts
since May 2006
Dec 9th, 2008
0

Re: Review my code

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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 9th, 2008
0

Re: Review my code

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Holy Roller is offline Offline
2 posts
since Jun 2008
Dec 9th, 2008
0

Re: Review my code

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Loop Switch Query?
Next Thread in C++ Forum Timeline: quick question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC