Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

First C++ program


ghost's Avatar
0 0

Just learned c++ today and made this program with what I learned. It sucks I know but its my first program. If you see anyway i can improve it feel free to share.

#include <iostream>

using namespace std;
int value ( int firstnum, int secondnum );
int numbergame();

int main()
{
  int firstnum, secondnum;
  int final = 12;
  int goagain;
  cout << "Enter password!\n";
  cin >> firstnum >> secondnum;
  cin.ignore();
  if ( value ( firstnum, secondnum ) == final ) 
  {
       cout << "Access granted\n";   
       cout << "Now lets play a number game\nPress enter to start...\n";
       cin.get();
       return numbergame();    
  }
  else 
  {
       cout << "Access denied!\n";
       cout << "Try again 1=yes 2=no\n";
       cin >> goagain;
       cin.ignore();
       if ( goagain == 1 )
       {
            return main();
       }
       else 
       {
            cout << "Exiting!\n";
            cin.get();
       }
  }
  
}

int value ( int firstnum, int secondnum )
{
      return firstnum + secondnum;
}

int numbergame()
{
    int number;
    cout << "Guess a number between 1 and 100.\n";
    cin >> number;
    cin.ignore();
    if ( number == 77 )
    {
         cout << "You have guessed the correct number\n";
         cout << "You win a free command prompt!\npress enter to claim your prise\n";
         cin.get();
         system("start");
         return 0;
    }
    else if ( (number <= 20) && (number >= 0) )
    {
         cout << "You are way off, try again!\n";
         return numbergame();
    } 
    else if ( (number <= 40) && (number >= 21) )
    {
            cout << "Your getting warmer but your still not there.\n";
            return numbergame();
    }
    else if ( (number <= 60) && (number >= 41) )
    {
            cout << "Your getting very warm try again!\n";
            return numbergame();
    }
    else if ( (number <= 76) && (number >= 61) )
    {
            cout << "Your on fire guess again!\n";
            return numbergame();
    }
    else if ( (number >= 77) && (number <=90) )
    {
            cout << "Your very close guess a lower number!\n";
            return numbergame();
    }
    else if ( (number >= 91) && (number <= 100) )
    {
            cout << "Your getting warmer but your still not there.\n";
            return numbergame();
    }
    else
    {
         cout << "You are not in the number range, try again!\n";
         return numbergame();
    }
    return 0;
}
    

Uber0n's Avatar
Member
0 0

Looks like a good start, but you could use a randomized number instead of always letting 77 be the correct answer in the nyumber game ;)


eXXon's Avatar
Member
0 0

i also thought the random number would be a good idea. and i would change this:

  else 
  {
       cout << "Access denied!\n";
       cout << "Try again 1=yes 2=no\n";
       cin >> goagain;
       cin.ignore();
       if ( goagain == 1 )
       {
            return main();
       }
       else 
       {
            cout << "Exiting!\n";
            cin.get();
       }
  }
  
}

for this:

int wronganswer(){
    int goagain;
    cout << "Try again 1=yes 2=no\n";
    cin >> goagain;
    if(goagain == 1){
        main();
    }
    else if(goagain == 2){
        cout << "Exiting!\n";
        cin.get();
    }
    else{
        cout << "Not a valid choice\n\n";
        wronganswer();
    }
}

because i hate when programs take whatever answer and do something they're not supposed to.

but overall it is a good first program..


ghost's Avatar
0 0

eXXon wrote: i also thought the random number would be a good idea. and i would change this:

  else 
  {
       cout << "Access denied!\n";
       cout << "Try again 1=yes 2=no\n";
       cin >> goagain;
       cin.ignore();
       if ( goagain == 1 )
       {
            return main();
       }
       else 
       {
            cout << "Exiting!\n";
            cin.get();
       }
  }
  
}

for this:

int wronganswer(){
    int goagain;
    cout << "Try again 1=yes 2=no\n";
    cin >> goagain;
    if(goagain == 1){
        main();
    }
    else if(goagain == 2){
        cout << "Exiting!\n";
        cin.get();
    }
    else{
        cout << "Not a valid choice\n\n";
        wronganswer();
    }
}

because i hate when programs take whatever answer and do something they're not supposed to.

but overall it is a good first program.. Good idea ill change that, and uberon I couldnt figure out how I could randomize the number but I like that idea. Thanks for the input.


ghost's Avatar
0 0

you all are blind or great programmers, huh?

if ( number == 77 )

and

else if ( (number >= 77) && (number <=90) )

if number is equal to 77 OR is equal or higher to 77, nice way how to confuse some "programmers"

friend, number > 77 is better in this case, and don't worry, I am not laughing at you, it's really amazing app for first time.


ghost's Avatar
0 0

It is cool, just wondering do you know any other programming languages? The first language I learned I did something simaler. How every at the end I had say how many trys it took them to get it, the number was random, you had the ability to guess numbers between 1-10, 1-100, 1-1000, But nontheless it is very nice :)


ghost's Avatar
0 0

rand() is the random function I think…not completely sure though…


ghost's Avatar
0 0

deathalive wrote: you all are blind or great programmers, huh?

if ( number == 77 )

and

else if ( (number >= 77) && (number <=90) )

if number is equal to 77 OR is equal or higher to 77, nice way how to confuse some "programmers"

friend, number > 77 is better in this case, and don't worry, I am not laughing at you, it's really amazing app for first time. lol, thanks for finding that im surprised I didn't catch that mistake :(


ynori7's Avatar
Future Emperor of Earth
0 0

for random numbers, use this:

#include <time.h> //put this at the start of the program

srand(time(0)); //put this every time you you want to generate a new seed

number=rand()%max; //this will generate a number from 0 to (max-1)


ghost's Avatar
0 0

I got the random numbers working thanks. I am having trouble understanding pointers, If anyone can explain to me about how pointers work or have a good article for a basic understanding of how the pointer work, that be great. :ninja:


ghost's Avatar
0 0

a pointer is a variable containing a memory address.


ynori7's Avatar
Future Emperor of Earth
0 0

basically the difference between a pointer and a variable is that a variable stores data in a certain place in memory, and a pointer 'points' to a certain place in memory. so if you pass a pointer into a method, you can change the original value of the variable (in its own environment) being pointing to, not just the temorary one(in the new environment).