topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 9:28 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: My first program.  (Read 12655 times)

alivingspirit

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 167
    • View Profile
    • Donate to Member
My first program.
« on: July 01, 2008, 04:33 PM »
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

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: My first program.
« Reply #1 on: July 01, 2008, 06:36 PM »
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)
: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 gameplay
Interestingly, 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

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 467
    • View Profile
    • Circle Dock
    • Donate to Member
Re: My first program.
« Reply #2 on: July 01, 2008, 11:50 PM »
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)

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.microsof...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.
Author of Circle Dock: http://circledock.wikidot.com
Author of Video In Picture: http://videoinpicture.wikidot.com
Author of Webcam Signature: http://webcamsignature.wikidot.com
Author of Easy Unicode Paster: http://easyunicodepaster.wikidot.com

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: My first program.
« Reply #3 on: July 01, 2008, 11:53 PM »
btw if you are curious, this program is one of the challenges in our self-teaching programming school:
https://www.donation...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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 167
    • View Profile
    • Donate to Member
Re: My first program.
« Reply #4 on: July 02, 2008, 09:12 AM »
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.

fenixproductions

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,186
    • View Profile
    • Donate to Member
Re: My first program.
« Reply #5 on: July 02, 2008, 01:30 PM »
Just my 2 cents.
If you'd like to teach your younger brother / sister programming, here's the best choice:
http://www.ceebot.co.../colobot/index-e.php

Nice thing to play with, even for more advanced developers :)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: My first program.
« Reply #6 on: July 02, 2008, 01:44 PM »
If you feel like it fenix, that would be a GREAT thing to post a little "mini-review" about..  It's exactly the kind of program we should be bringing some attention to.