|
-
February 16th, 2009, 08:09 AM
#16
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
how's this....
and can someone tell me why cin.get(); won't work where I have the two scanf("%"); to pause it at the end...
thanks
Code:
#include <iostream>
#include <windows.h>
#define maxrand 40
#define maxrand2 50
using namespace std;
int iq = 0;
int iq2 = 0;
int calc = 0;
int y = 1;
int counter = 0;
int main()
{
cout << " Welcome to the : Guess Mr. Hell's IQ Game !!\n\n";
cout << " To Win You Will Have to follow these simple rules:\n\n";
cout << " * guess Mr. Hell's IQ in less than 8 tries,\n";
cout << " * Complete the questions asked after you get it,\n";
cout << " * You can't say no to ANY questions.\n";
cout << "\n Once You guess Mr. Hell's IQ,\n";
cout << " you will have to PROVE you're smarter than Mr. Hell\n";
cout << "\n\n What's Your First Guess ?\n\n";
srand( (unsigned)time( NULL ) );
int iqanswer = rand() % maxrand;
while(iq != maxrand, ++counter)
{
cin>>iq;
if(iq == iqanswer)
{
cout << "\n Right On ! You guessed it. He Really is THAT stupid !!\n\n";
break;
}
if(iq > iqanswer)cout <<"\n Too High\n\n Try Again: ";
if(iq < iqanswer)cout << "\n Too Low\n\n Try Again: ";
if(counter == 8)
{
cout << "\n\n You're Not Too Bright Yourself Now Are You ?";
cout << "\n Are You Sure You're Not Mr. Hell ?\n\n It Was: ";
cout<< iqanswer; cout << " Dumb-*** !\n\n";
break;
}
}
cout << " Now You Will Have To Guess George Bush's IQ\n\n";
cout << " Go Ahead and take a Wild Guess: ";
counter = 0;
int iqanswer2 = rand() % maxrand2;
while( iq2 != maxrand2, ++counter){
cin>>iq2;
if(iq2 == iqanswer2)
{
cout << "\n Nice Work ! You guessed it. He is also pretty Dumb huh ?\n\n\n\n";
break;
}
if(iq2 > iqanswer2)cout <<"\n Too High\n\nTry Again: ";
if(iq2 < iqanswer2)cout << "\n Too Low\n\nTry Again: ";
if(counter == 8)
{
cout << "\n\n You're Not Too Bright Yourself Now Are You ?\n";
cout << " Are You Sure You're Not Mr. Hell ?\n\nIt Was: ";
cout << iqanswer2; cout << " Dumb-*** !\n\n";
break;
}
}
cout << " Now You Will Need To Add The Two IQ's Together....\n\n";
cout << " If You Are As Dumb As Mr. Hell You May Need A Calculator...\n\n";
cout << " Would You like to use a Calculator ?\n\n";
cout << " press 1 for the calculator,\n";
cout << " press any other key for no if you think you don't need it.\n\n";
{
cin >> calc;
if(calc == y)
{
::ShellExecute(NULL,
"open",
"calc.exe",
"",
"",
SW_SHOWNORMAL);
}
else
{
cout << "\n\n\n\n\n\n\n\n\n\n Ha Ha You're as Dumb As Mr. Hell !!\n";
cout << " You Loose Sucker....\n\n";
cout << " You could NOT answer NO to ANY question REMEMBER ???";
}
if(calc == y )
{
int total;
int totalanswer = (iqanswer + iqanswer2);
cout << " Now add Mr. Hell's IQ to George Bush's IQ and type your answer here: ";
cin >> total;
if(total == totalanswer)
{
cout << "\n\n\n YOU WIN !!!\n\n Very Good !\n\n";
cout << " You Clearly are a step ahead of Mr. Hell, and your IQ is higher than Both\n";
cout << " Mr. Hell's and George Bush's IQ COMBINED !!";
}
else
{
cout << "\n\n * GAME OVER * Ha Ha !\n\n Your lack of adding skills suggests\n You are as Dumb as Mr. Hell !\n\n";
cout << " The Correct Answer was: ";
cout << totalanswer;
}
}
cout << "\n\n Thanks for Playing.\n\n You Are Now Worthy Of A Special Treat From\n\n";
cout << " The Worlds Greatest New Alien Rock Band...\n\n\n T H E P E R I S H !! \n\n\n\n";
cout << " press any key, then ENTER to continue... \n\n\n\n\n";
scanf("%"); // if I use cin.get(); here instead it does NOT pause and wait for a key press. WHY ??????
{
::ShellExecute(NULL,
"open",
"http://www.youtube.com/watch?v=TwazkWbE4Pk&feature=channel_page",
"",
"",
SW_SHOWNORMAL);
scanf("%");// if I use cin.get(); here instead it does NOT pause and wait for a key press. WHY ??????
}
}
}
-
February 16th, 2009, 08:22 AM
#17
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by Jeff++
so should I use the iq = 0; or the iq(0); ????
You can use either, but some C++ coding standard manuals make a preference for, or even mandate direct initialisation:
Now to address a statement that I disagree with. Although in my opinion (and the opinion of others) it is bad practice not to explicitly initialise variables (and in many cases dangerous), I must disagree with those that said that iq was not initialised in your original code. Technically, it was inititialised, because according to the standard, variables of static storage duration are initialised to zero...
3.7.1 Static storage duration
1 All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these
objects shall last for the duration of the program (3.6.2, 3.6.3).
and
3.6.2 Initialization of non-local objects
1 Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.
In other words, since iq is declared at global scope it has static storage duration and therefore is initialised to zero. That said, regardless of this technicality, I agree with the advice you have been given to explicitly initialise iq and the other global-scope variables that you have declared.
-
February 16th, 2009, 08:26 AM
#18
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by Jeff++
how's this....
and can someone tell me why cin.get(); won't work where I have the two scanf("%"); to pause it at the end...
thanks
Code:
#include <iostream>
#include <windows.h>
#define maxrand 40
#define maxrand2 50
using namespace std;
int iq = 0;
int iq2 = 0;
int calc = 0;
int y = 1;
int counter = 0;
int main()
{
cout << " Welcome to the : Guess Mr. Hell's IQ Game !!\n\n";
cout << " To Win You Will Have to follow these simple rules:\n\n";
cout << " * guess Mr. Hell's IQ in less than 8 tries,\n";
cout << " * Complete the questions asked after you get it,\n";
cout << " * You can't say no to ANY questions.\n";
cout << "\n Once You guess Mr. Hell's IQ,\n";
cout << " you will have to PROVE you're smarter than Mr. Hell\n";
cout << "\n\n What's Your First Guess ?\n\n";
srand( (unsigned)time( NULL ) );
int iqanswer = rand() % maxrand;
while(iq != maxrand, ++counter)
{
cin>>iq;
if(iq == iqanswer)
{
cout << "\n Right On ! You guessed it. He Really is THAT stupid !!\n\n";
break;
}
if(iq > iqanswer)cout <<"\n Too High\n\n Try Again: ";
if(iq < iqanswer)cout << "\n Too Low\n\n Try Again: ";
if(counter == 8)
{
cout << "\n\n You're Not Too Bright Yourself Now Are You ?";
cout << "\n Are You Sure You're Not Mr. Hell ?\n\n It Was: ";
cout<< iqanswer; cout << " Dumb-*** !\n\n";
break;
}
}
cout << " Now You Will Have To Guess George Bush's IQ\n\n";
cout << " Go Ahead and take a Wild Guess: ";
counter = 0;
int iqanswer2 = rand() % maxrand2;
while( iq2 != maxrand2, ++counter){
cin>>iq2;
if(iq2 == iqanswer2)
{
cout << "\n Nice Work ! You guessed it. He is also pretty Dumb huh ?\n\n\n\n";
break;
}
if(iq2 > iqanswer2)cout <<"\n Too High\n\nTry Again: ";
if(iq2 < iqanswer2)cout << "\n Too Low\n\nTry Again: ";
if(counter == 8)
{
cout << "\n\n You're Not Too Bright Yourself Now Are You ?\n";
cout << " Are You Sure You're Not Mr. Hell ?\n\nIt Was: ";
cout << iqanswer2; cout << " Dumb-*** !\n\n";
break;
}
}
cout << " Now You Will Need To Add The Two IQ's Together....\n\n";
cout << " If You Are As Dumb As Mr. Hell You May Need A Calculator...\n\n";
cout << " Would You like to use a Calculator ?\n\n";
cout << " press 1 for the calculator,\n";
cout << " press any other key for no if you think you don't need it.\n\n";
{
cin >> calc;
if(calc == y)
{
::ShellExecute(NULL,
"open",
"calc.exe",
"",
"",
SW_SHOWNORMAL);
}
else
{
cout << "\n\n\n\n\n\n\n\n\n\n Ha Ha You're as Dumb As Mr. Hell !!\n";
cout << " You Loose Sucker....\n\n";
cout << " You could NOT answer NO to ANY question REMEMBER ???";
}
if(calc == y )
{
int total;
int totalanswer = (iqanswer + iqanswer2);
cout << " Now add Mr. Hell's IQ to George Bush's IQ and type your answer here: ";
cin >> total;
if(total == totalanswer)
{
cout << "\n\n\n YOU WIN !!!\n\n Very Good !\n\n";
cout << " You Clearly are a step ahead of Mr. Hell, and your IQ is higher than Both\n";
cout << " Mr. Hell's and George Bush's IQ COMBINED !!";
}
else
{
cout << "\n\n * GAME OVER * Ha Ha !\n\n Your lack of adding skills suggests\n You are as Dumb as Mr. Hell !\n\n";
cout << " The Correct Answer was: ";
cout << totalanswer;
}
}
cout << "\n\n Thanks for Playing.\n\n You Are Now Worthy Of A Special Treat From\n\n";
cout << " The Worlds Greatest New Alien Rock Band...\n\n\n T H E P E R I S H !! \n\n\n\n";
cout << " press any key, then ENTER to continue... \n\n\n\n\n";
scanf("%"); // if I use cin.get(); here instead it does NOT pause and wait for a key press. WHY ??????
{
::ShellExecute(NULL,
"open",
"http://www.youtube.com/watch?v=TwazkWbE4Pk&feature=channel_page",
"",
"",
SW_SHOWNORMAL);
scanf("%");// if I use cin.get(); here instead it does NOT pause and wait for a key press. WHY ??????
}
}
}
try
Code:
//clear error flags
std::cin.clear();
//ignore characters in current stream until '\n' is found
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
before your scanf statements. You'll need #include<limits>.
EDIT:
Since you have strapped yourself to Windows such that "PAUSE" is going to be a valid DOS shell command, why are you using scanf to pause? Why not use
instead?
Last edited by PredicateNormative; February 16th, 2009 at 08:53 AM.
-
February 16th, 2009, 01:20 PM
#19
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
Hello Jeff++
PredicateNormative is right.
I didn't see that you declared iq outside main(). My bad!
-
February 16th, 2009, 02:15 PM
#20
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
Whilst strictly speaking this is allowed.....
Code:
#define maxrand 40
#define maxrand2 50
It is never a good idea to use macro's to represent constants. The much more correct way would be....
Code:
const int maxrand = 40;
const int maxrand2 = 50;
Use the preprocessor as little as possible. There are some things you have to use it for, but for constants and inline functions it shouldn't be used.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 16th, 2009, 02:42 PM
#21
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by Russco
Whilst strictly speaking this is allowed.....
Code:
#define maxrand 40
#define maxrand2 50
It is never a good idea to use macro's to represent constants. The much more correct way would be....
Code:
const int maxrand = 40;
const int maxrand2 = 50;
Use the preprocessor as little as possible. There are some things you have to use it for, but for constants and inline functions it shouldn't be used.
What are you basing that on? The first line in MSDN's documentation for #define says "You can use the #define directive to give a meaningful name to a constant in your program", and they do it all over the place.
-
February 16th, 2009, 02:56 PM
#22
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by GCDEF
What are you basing that on?
I do not know about Russco, but Scott Meyers' Effective C++, Third Edition Item #2 is titled "Prefer consts, enums, and inlines to #defines." The main relevant point is that since the macro name "may be removed by the preprocessor before the source code ever gets to a compiler", it "may not get entered into the symbol table", which in turn "can be confusing if you get an error during compilation involving the use of the constant, because the error message may refer to (the literal) not (the symbolic name)".
-
February 16th, 2009, 03:42 PM
#23
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by GCDEF
What are you basing that on? The first line in MSDN's documentation for #define says "You can use the #define directive to give a meaningful name to a constant in your program", and they do it all over the place.
Laserlight explained perfectly well why.
Just because microsoft do something doesn't make it a good idea. They also frequently use gotos. I dont subscribe to the "Oh M$ do it that way so it must be fine club". There are times when a macro is unavoidable but whenever it is avoidable, you ought to avoid it.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 16th, 2009, 05:03 PM
#24
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
hey,
thanks everyone for this educational argument !
this is great.
so.... bottom line here is
I should use
Code:
const int maxrand = 40;
and NOT use
????
also... I'm only trying cin.get because I'm trying to use different things to learn, no other reason. I was wondering if someone could explain why the cin.get(); doesn't work without the cin.clear and cin.ignor ? I see it in other programs without those lines and it works fine. why why why ? : \
thanks again everyone.
-
February 16th, 2009, 05:10 PM
#25
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
Its not it doesn't work, it works fine, but the cin.get() will get the '\n' thats left in the stream by operator >> and so wont need to await further input. For cin.get to work as you expect it to, the input buffer must be empty. If the buffer holds input awaiting processing, theres no need to ask the user for more as cin.get() only returns the next character in the stream.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 16th, 2009, 07:53 PM
#26
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
ahhh....
thanks Russco ! so it's holding the last " \n " I assume then ??
thanks.
Last edited by Jeff++; February 18th, 2009 at 03:19 AM.
-
February 16th, 2009, 08:38 PM
#27
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
 Originally Posted by GCDEF
What are you basing that on? The first line in MSDN's documentation for #define says "You can use the #define directive to give a meaningful name to a constant in your program", and they do it all over the place.
Two more thinks:
* namespace scoping (#define's have no clue about it).
* debugging.
And I will repeat also, that does not make it correct and most of Microsoft API was written using C language so they are hamstrung with backward compatibility.
P.S. Jeff++, do not use #defines, specially for magic numbers.
-
February 20th, 2009, 04:20 PM
#28
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
*chuckles at the irony*
The game insults IQ, so you may want to fix to . :P
Seriously though, for real projects (work or school) don't forget to check spelling as well as the c++ coding
-
February 22nd, 2009, 03:12 AM
#29
Re: Newbie'z first real program. Could use some CONSTRUCTIVE critizism to learn...
ha ha ha, didn't even notice. lmao.
thanks Kryles... too funny.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|