loop not ending why ?

1
2
3
4
5
6
7
8
9
10
11
12
int size=0;
cin >>size;
string testcase[size];

int ii=0;

while(size!=0){
cout <<ii<<size;
cin>>testcase[ii];
size=size-1;
ii=ii+1;
}


my this loop is not ending ..i don't see a point why ?
your loop is assuming that the user input will be positive. In case a negative value is entered then the loop condition will always be true because

size=size-1

means that size will never reach 0. Well that's the only reason I can see so far for your loop not ending.
First of all, to me, that loop likes highly confusing.

What exactly is it you're trying to accomplish?

The loop is explicitly instructed to loop while "size" is unequal to, or not equal to the value of 0.

As long as any value is not 0 it will loop.

Also, that loop will only work with positive integers.

And you should define a value for "testcase."
Last edited on
This code doesn't even compile in VC++.
The array, testcase, must have an constant index known at compile-time.
If the size happens to be negative, the program should crash the moment it reaches line 3.
But if it compiles over this error, then it may as well go over such an error.
you need a constant expression while declaring the array of the string .
well the input will never be negative thats why i have written this sort of code.


@bluecoder : are you damn sure about it ?
@ron2797:
He is so sure because it is a defined.
When declaring an array, the index of the array must be constant, and known at compile time.
What makes you so sure that the input will never be negative?
Who's to stop the user from entering -1 when prompted for the size?
string testcase[size];

that would actually compile in mingw and gcc.

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
Last edited on
Topic archived. No new replies allowed.