First C++ program
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;
}
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..
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.
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.
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 :)
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 :(
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).