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.
:D
[edit]Variable Length Arrays.Quote:
Good now? What are VLAs?
No, you still have a VLA. :) [/edit]Quote:
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.
:D
[edit]Variable Length Arrays.Quote:
Good now? What are VLAs?
No, you still have a VLA. :) [/edit]Quote:
Good now?
>> 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... :devil:
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.
>> 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.
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.
Fourth-last line.Quote:
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.)Quote:
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]
Ah sorry dwks I missed that line and yes of course you are correct :)
Yet another reason why RAII rocks :)Quote:
Ah sorry dwks I missed that line
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.
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.