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.

/*
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.

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.

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

if(strcmp(choice, "y")==0)
math=diff/i*1000;
cout<<""<<x<<" more tries"<<endl;

to

if (strcmp(choice, "y") == 0)
math = (diff / i) * 1000;
cout << "" << x << " more tries" << endl;

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.

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 :P

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.

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 :P

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.