ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Coding Snacks

My first program.

(1/2) > >>

alivingspirit:
Hi everyone. So basically, I just started learning to program this Sunday in c++ (sorry Tinjaw that Python was not my 1st choice, but it is on my list to learn eventually).  :D So after messing around for a few days I came up with my first program. Its a game where you try and guess the number that the computer comes up with at random. I actually find it a little bit addicting. I'll keep adding to it as my skills progress. Ill post more updates if you guys like it. This might not belong in the coding snacks section and if not I'm sorry. I guess I'm just exited that I finally got around to learning this stuff.
OK, The program is attached. or get it here: http://www.mediafire.com/?e0nd9gemgcc
Here is the source code:

--- ---#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int num();
int game ();
int x;
int y;
int a;
int answer;
int tries;
int gp = 0;
int gw = 0;

int main() {

    tries = 6;
    a = 1;
    srand((unsigned)time(NULL));
    answer = num();
    cout<<"Games played: "<< gp << "       Games Won: "<< gw <<"\n\n";
    cout<<"Guess a number 1 through 100: ";
    game();

}

int game() {
        do {
        cin>> y;
        tries--;
        if ( y > 100  ||  y < 1 ) {
            cout<<"Sorry that is not a valid entry\n";
            cout<<"Guess another number: "; }

        else if ( y < answer ) {
            cout<<"That number is too LOW. Try again.\n";
            if ( tries != 0 ) {
            cout<<"You have " << tries <<" tries left.\n"; }
            else {
            cout<<"You are out of tries.\n"; } }

        else if ( y > answer ) {
            cout<<"That number is too HIGH. Try again.\n";
            if ( tries != 0 ) {
            cout<<"You have " << tries <<" tries left.\n"; }
            else {
            cout<<"You are out of tries.\n"; } }

        else {
            cout<<"You got it!";
            cout<<"\n\n\n";
            gp++;
            gw++;

            main(); }

        if (tries == 0) {
            cout<<"\nGame Over! Play again.\n\n\n";
            gp++;
            main();
            }
    }
while (1); }


int num() {
   return x = (rand() % 99) + 1 ; }


As you can see it is still a work in progress. There is one major glitch that I don't know how to correct yet and that is if you type in anything other than a number it flips out. I will fix it as soon as I learn how. For now have fun.
I appreciate any comments on how to improve or add to the game. I hope to be able to code more usefull programs in the future.
:Thmbsup:

jgpaiva:
This might not belong in the coding snacks section and if not I'm sorry. I guess I'm just exited that I finally got around to learning this stuff.
-alivingspirit (July 01, 2008, 04:33 PM)
--- End quote ---
:D No problem! Actually, if you'd like to get some more constructive criticism, the developer's section might be more suited, and the c++ experts might give you a hand. (if you'd like, i can move it there, just pm me).

I like the game!
great strategy for gameplayInterestingly, 5 atempts is exactly one atempt short of the required number to always win this game :P Using Binary Search, you can always end up with 2 numbers to choose (in the worst case scenario :) )
Keep it up, maybe we'll get a new coding snacks author ;)

VideoInPicture:
As you can see it is still a work in progress. There is one major glitch that I don't know how to correct yet and that is if you type in anything other than a number it flips out. I will fix it as soon as I learn how. For now have fun.
I appreciate any comments on how to improve or add to the game. I hope to be able to code more usefull programs in the future.
:Thmbsup:
-alivingspirit (July 01, 2008, 04:33 PM)
--- End quote ---

You have to use something like this to catch those input exceptions.

try
{
......
// execute regular commands
}
catch (Exception ee)
{
// do something if there is an error
}

More details here: http://msdn.microsoft.com/en-us/library/6dekhbbc(VS.80).aspx. I wish beginning programming classes would actually teach stuff like this so that you could make a program someone would actually use. I took two classes of University C++ programming and no one even mentioned try...catch clauses. I had to learn it myself.

mouser:
btw if you are curious, this program is one of the challenges in our self-teaching programming school:
https://www.donationcoder.com/forum/index.php?board=79.0

so in case you feel like you need some motivation to continue your learning, that might be a good place to start.

alivingspirit:
Thanks everyone for all your support.
By the way, after spending some time looking at the programming resource pages in programming school, I must say that I am truly proud and awed at how far along we've come in the information age. The amount of stuff that you can learn just at the click of a mouse is just amazing. The future holds some very exiting things for us. We should better get ready.

Navigation

[0] Message Index

[#] Next page

Go to full version