What if amount is zero?
If you wanted to be picky about English, too, you're using lots of single 't's where there should be two.
[edit]Variable Length Arrays.Good now? What are VLAs?
No, you still have a VLA. [/edit]Good now?
What if amount is zero?
If you wanted to be picky about English, too, you're using lots of single 't's where there should be two.
[edit]Variable Length Arrays.Good now? What are VLAs?
No, you still have a VLA. [/edit]Good now?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>> Good now? What are VLAs?
No, use vectors. A VLA is a variable length array, which is what you used first. It's not valid in C++ but is supported as an extension by gcc.
Not valid in C++!!!
I never use vectors, always and only VLAs!
But I guess so long as it works it should be okay...
Riiight. Well, I guess we know which compiler you have.
Besides, that's the point. It wouldn't work if your compiler didn't have that extension.
And it will still break if you enter 0 for amount. Or "cat" or something. But it's just a demonstration, after all.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>> But I guess so long as it works it should be okay...
How do you know if it works?
I'm guessing you'll spend a lot more time debugging and fixing bugs in your game with that attitude than you would if you tried to use better tools in the first place.
If the compiler supports them as an extension, they should work - in a limited way. If you have very large amounts of data, you may not be able to store it all in a VLA (it has rather limited memory capacity), whereas a vector would handle that amount of data just fine.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Just noticed you declared this:
But you did not delete the allocated memory... thus you have a dangling pointer.Code:int *num = new int[amount];
And I dont see the reason for allocating memory in such a simple program, you can accomplish
the task without using a pointer at all.
Double Helix STL
Fourth-last line.Just noticed you declared this:
But you did not delete the allocated memory... thus you have a dangling pointer.Code:int *num = new int[amount];
Code:delete [] num;Unless you using a standard container, you need to allocate memory dynamically if you want any number of numbers to be supported. (And of course, the standard containers like std::vector just allocate memory behind the scenes.)And I dont see the reason for allocating memory in such a simple program, you can accomplish
the task without using a pointer at all.
Assuming that you have to store the values at all, of course. I'd use something like this:
(Disclaimer: I have not tested it, only proved it correct. [Donald E. Knuth] Actually, I didn't even prove it correct. )Code:#include <iostream> #include <climits> int main() { int num, max = INT_MIN, min = INT_MAX; bool did = false; while(std::cin >> num) { if(num < min) min = num; if(num > max) max = num; did = true; } if(!did) std::cout << "No numbers entered.\n"; else { std::cout << "Minimum: " << min << std::endl << "Maximum: " << max << std::endl; } return 0; }
[edit] The exact quote is "I have only proved it correct, not tried it." [/edit]
Last edited by dwks; 09-26-2007 at 02:07 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ah sorry dwks I missed that line and yes of course you are correct
Double Helix STL
Yet another reason why RAII rocksAh sorry dwks I missed that line
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
where does it say only enter 5 numbers? and if your doing a loop i would do it like this
Code:int input,least, greastest; do { cout<<"Please enter a number."; cin>>input; least = input; greatest = input; if(input < least) { least = input; } if(input > greatest) { greatest = input; } }while(input != -99);
Cool, then both the least and the greatest will be -99 when the loop exits.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
IF the loop exits - which won't happen if the user presses Ctrl+Z/Ctrl+D or enters a letter or the input comes from a file where the last number isn't -99 or which contains a letter.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law