Hi, I am new here. I got this homework due tonight. I have been working on this code for a while. This works fine and compiles but it crashes at run time. I am thinking there might be some problems with my logic here. Please help me. Here is my code.
You say this compiles? It doesn't in VS2005 as is. I can make a few changes to make it compile, however. You have a few problems here:
1. "int n" at the top of main() should be a constant expression. This means in this case that it should have a const qualifier in front of it and should reference a concrete number (1000 or 100 for example...)
2. Since you are intending for the value of n to be user entered, this makes 1 somewhat moot, but also means that you cannot use automatic storage for your arrays. You should use pointers instead, i.e. "char *charArray; charArray = new char[ n ];" Then before the break you would do a "delete[] charArray". If you choose to do this then you will need to make each case statement a compound statement block. (i.e. put '{' immediately after the case X: and put a '}' immediately before the "break;".
3. You are looping from 1 to n inclusive... C / C++ arrays go from 0 to n exclusive ( n-1 ). This means you need to change your loop to read "for ( int i = 0; i < n; i++ )" This may not cause a crash (it may as well) but it is certainly incorrect. This goes for ALL of your loops. In addition, if you are counting down in a loop you should do the opposite logically: "for( j = i; j >= 0; j-- )"