| View previous topic :: View next topic |
| Author |
Message |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 4:55 pm Post subject: |
|
|
| noz3001 wrote: | | windows.h is bigger and is OS dependant. |
Headers, no matter how big, do not make an impact on the final size of the PE image because they are used for function and type definitions - something for the compiler. If they are used for holding functions that need compiled you are using them wrong.
Also, chances are that if you are coming to this section to learn how to program, you are doing it in a Windows environment.
If all you are trying to accomplish is attempting to contradict me, please stop, you are wasting your time.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Jul 03, 2008 5:08 pm Post subject: |
|
|
| Flyte wrote: | | noz3001 wrote: | | windows.h is bigger and is OS dependant. |
Headers, no matter how big, do not make an impact on the final size of the PE image because they are used for function and type definitions - something for the compiler. If they are used for holding functions that need compiled you are using them wrong.
Also, chances are that if you are coming to this section to learn how to program, you are doing it in a Windows environment.
If all you are trying to accomplish is attempting to contradict me, please stop, you are wasting your time. |
Wow, way to take offense at something not offensive. I was only saying that time() would be more suited with cross-platform programming in mind, I didn't say your code was wrong or anything.
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Thu Jul 03, 2008 5:14 pm Post subject: |
|
|
| noz3001 wrote: | | Flyte wrote: | | noz3001 wrote: | | windows.h is bigger and is OS dependant. |
Headers, no matter how big, do not make an impact on the final size of the PE image because they are used for function and type definitions - something for the compiler. If they are used for holding functions that need compiled you are using them wrong.
Also, chances are that if you are coming to this section to learn how to program, you are doing it in a Windows environment.
If all you are trying to accomplish is attempting to contradict me, please stop, you are wasting your time. |
Wow, way to take offense at something not offensive. I was only saying that time() would be more suited with cross-platform programming in mind, I didn't say your code was wrong or anything. |
I've adjusted my code and the only thing left I need is that part, having it select a random number each time. I'm willing to try your method using time() I honestly don't know how to go about using it and i tried the other method using Clock() and had no luck. Would you mind explaining it? XD
Also, do you know how I can make it so that if the person guesses it right before the 3rd try to negate the other tries and end it? Ty
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 5:16 pm Post subject: |
|
|
| MegaForum wrote: | I've adjusted my code and the only thing left I need is that part, having it select a random number each time. I'm willing to try your method using time() I honestly don't know how to go about using it and i tried the other method using Clock() and had no luck. Would you mind explaining it? XD
Also, do you know how I can make it so that if the person guesses it right before the 3rd try to negate the other tries and end it? Ty  |
Look at the example I posted, you can use break; to exit a loop at any time. It also shows you how to use the srand() and rand() functions.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Jul 03, 2008 5:20 pm Post subject: |
|
|
| Code: | #include <ctime>
#include <iostream>
int main( void )
{
srand( time(0) );
int RandomNumber = rand() % 100 + 1; // Random number between 100 & 1
std::cout << RandomNumber;
return 0;
} |
There we go, I initially wrote it in C . Messed up a bit there.
Last edited by Noz3001 on Fri Jul 04, 2008 4:03 am; edited 1 time in total |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 5:33 pm Post subject: |
|
|
| noz3001 wrote: | | Just re-arranged it. |
Just thought I should point out you should use ctime for C++.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Jul 03, 2008 5:41 pm Post subject: |
|
|
| Flyte wrote: | | noz3001 wrote: | | Just re-arranged it. |
Just thought I should point out you should use ctime for C++. |
Yeah, i fucked up a bit and did it half and half =D
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Thu Jul 03, 2008 9:16 pm Post subject: |
|
|
New problem this time D;. I get this trying to compiler.
fatal error C1083: Cannot open include file: 'ctime.h': No such file or directory
~Thanks for the help~
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 10:23 pm Post subject: |
|
|
| MegaForum wrote: | New problem this time D;. I get this trying to compiler.
fatal error C1083: Cannot open include file: 'ctime.h': No such file or directory
~Thanks for the help~ |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Jul 04, 2008 5:52 am Post subject: Re: [Help]C++ Loops |
|
|
| MegaForum wrote: | | fatal error C1083: Cannot open include file: 'ctime.h': No such file or directory | #include <ctime>
| slovach wrote: | | <stdlib.h> | <cstdlib>
Too lazy to read all the text. Here's my version. I'll answer your questions if you have any. | Code: | /* We shall use 32-bit time on x86 machines */
#if defined(_M_IX86) || defined(i386) || defined(__i386) || defined(__i386__)
#define _USE_32BIT_TIME_T
#endif
/* You can custom these O_o */
#define tries 5
#define nMin 5
#define nMax 10
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
::srand( time(NULL) );
int number[tries];
int mynumber = (::rand() % (nMax-nMin)) + nMin;
std::cout << mynumber << std::endl;
std::cout << "You get " << sizeof(number) / sizeof(int) << " tries to match my number! " << nMin << "-" << nMax << std::endl;
for(int i=0; i < (sizeof(number) / sizeof(int)); ++i) {
std::stringstream ss;
do {
std::cout << "Enter your number " << i+1 << ": ";
std::string s;
ss.clear();
ss.str("");
if( !std::getline(std::cin, s) )
return EXIT_FAILURE;
ss << s;
} while( !(ss >> number[i]) );
if( number[i] == mynumber ) {
std::cout << "You got my number correct!" << std::endl;
return EXIT_SUCCESS;
}
std::cout << "Your number is incorrect!" << std::endl;
}
std::cout << "You failed." << std::endl;
std::cin.sync();
std::cin.ignore();
return EXIT_SUCCESS;
} |
And few more comments about your code.
| MegaForum wrote: | | cin >> number; | What if I type "Jani"?
Well.. Works, but I personally I'd try to avoid breaks in if-statements.
| MegaForum wrote: | | else if(number != mynumber) | No need for this.
| MegaForum wrote: | | cout << "Your nubmer is incorrect!" << endl; | What to say..? :P
| MegaForum wrote: | | system("PAUSE"); | Eh.
And.. You could make your program a bit more adjustable (mins, maxs, tries)
EDIT: Uh oh.. I missed the second page again :p Whatever.
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Fri Jul 04, 2008 9:01 am Post subject: |
|
|
Thanks Jani . Although my code isn't as "pro" as you(neither am I) I'm going take your suggestions and fix a few things. I'm also working on making it say whether the person's input is higher or lower than the actual number ;P. Would you mind telling me how might make it so that It dissalows any character inputs and only numbers.
Thanks
|
|
| Back to top |
|
 |
|