| | |
Review my code
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
Sorry for the 145 lines, lol.
c++ Syntax (Toggle Plain Text)
/* Guessing Game By Jacob */ #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" using namespace std; int main(void) { SetConsoleTitle( "Guessing Game By Jacob" ); int number; int guess; int i=0; int x=8; int intro; double diff; char name[256]; char choice[10]; srand(time(0)); number=rand()%100+1; time_t start,end; cout<<"MENU"<<endl; cout<<"------------------------------------------------------------"<<endl; cout<<"Type the number that corresponds with an available option."<<endl; cout<<"------------------------------------------------------------"<<endl; cout<<"1)Play Game"<<endl; cout<<"2)Restart Score"<<endl; cout<<"3)Exit"<<endl; cin>>intro; if(intro==3) { EXIT_SUCCESS; } else if(intro==2) { cls(); ofstream score("score.txt", ios::trunc); cout<<"Score list reset."<<endl; cout<<"Press any key to continue. . ."<<endl; getch(); cls(); return main(); } else if(intro==1) { cls(); time (&start); while (guess!=number) { cout<<""<<x<<" more tries"<<endl; cout<<"Pick a number:"<<endl<<endl; cin>>guess; cin.ignore(); if(x==0) { time(&end); cls(); cout<<"You are out of turns. The correct number was "<<number<<"."<<endl; cout<<"Would you like to restart? (Y/N)"<<endl; cin.getline(choice,10); for(int y=0; y!=1;) { if(strcmp(choice, "y")==0) { cls(); return main(); y++; } if(strcmp(choice, "n")==0) { return 0; } else { cin.getline(choice, 10); continue; } } } if(guess<number) { cls(); cout<<"Too low!"<<endl<<endl; x--; i++; } else if(guess>number) { cls(); cout<<"Too high!"<<endl<<endl; x--; i++; } else { time(&end); diff=difftime(end,start); i++; float math=diff/i*1000; cout<<"Good job, you guessed the correct number!"<<endl<<endl; ofstream score("score.txt", ios::app); cout<<"You scored "<<math<<" points. Please enter your name so I can save your score:"<<endl; cin.getline(name,256); cls(); score<<""<<name<<" scored "<<math<<" points."<<endl; cout<<"Check the score.txt file to see how you did."<<endl<<endl; cout<<"Would you like to restart? (Y/N)"<<endl; cin.getline(choice,10); for(int z=0; z!=1;) { if(strcmp(choice, "y")==0) { cls(); return main(); z++; } if(strcmp(choice, "n")==0) { return 0; } else { cin.getline(choice, 10); continue; } } } } } else { cls(); return main(); } }
Sorry for the 145 lines, lol.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
You are using some nonstandard #include files and you have both stdlib.h and cstdlib:
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.
#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.
Never Ever call
Use a lot more whitespace to make your code easier to read. In other words, change
to
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)
if(strcmp(choice, "y")==0) math=diff/i*1000; cout<<""<<x<<" more tries"<<endl;
cpp Syntax (Toggle Plain Text)
if (strcmp(choice, "y") == 0) math = (diff / i) * 1000; 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jun 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
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.
"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.
![]() |
Similar Threads
- Java code to display a GUI (Java)
- whats wrong with the code......... (PHP)
- Just need someone to review the code for me please. (Java)
- c code snippets (DaniWeb Community Feedback)
- Please review Redesign old website (Website Reviews)
- Hi there id like you all to review my site (Website Reviews)
Other Threads in the C++ Forum
- Previous Thread: Loop Switch Query?
- Next Thread: quick question
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






