index out of range??!!
index out of range??!!
(OP)
Hi all,
Thanks for a wonderful forum
The code below is set to ask the user for an input to calculate an octal number.
I am stuck because I have tried to use the for loop to reverse write the string stored in revOctal and reorder it correctly to assign to fullOctal.
I get an error Index Out of Range Exception was unhandled..??!
I checked the code, if I input 999, the code returns 7471
Which has 4 characters...
so I made the loop go through it backwards, by setting I to the length of the revOctal, so 4, and then while i != 0; i-- , add the full octal (blank) to the revOctal so 1 (which is position 4) in this case, and then to the 3rd character and then the second etc...
but it gives the above error.
What is weird is that if I state i = revOctal.Length - 1, it works, but only outputs 3 characters?!!
Please help, this is a humdinger!
Thank you
Thanks for a wonderful forum

The code below is set to ask the user for an input to calculate an octal number.
CODE --> c#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace doExerciseAttempt { class Program { static void Main(string[] args) { int dec = 0; int rem = 0; int quot = 0; string revOctal = ""; string fullOctal = ""; dec = ReadAns("Please enter your number "); do { quot = CalculateQuot(dec); rem = calculateRem(dec); string octal = rem.ToString(); Console.WriteLine("This is the octal number {0} ", octal); octal = rem.ToString(); revOctal += octal; Console.ReadLine(); Console.WriteLine("This is the quotient {0} ", quot); Console.WriteLine("This is the remaider {0} ", rem); Console.ReadLine(); dec = quot; } while (dec != 0); Console.WriteLine("This is the reverse octal number {0} ", revOctal); Console.ReadLine(); for (int i = revOctal.Length; i != 0 ; i--) { fullOctal += revOctal[i]; } Console.WriteLine("This is the octal number {0} ", fullOctal); Console.ReadLine(); } private static int CalculateQuot(int dec) { return dec / 8; } private static int calculateRem(int dec) { return dec % 8; } private static int ReadAns(string p) { Console.Write(p); string ans = Console.ReadLine(); return int.Parse(ans); } } }
I am stuck because I have tried to use the for loop to reverse write the string stored in revOctal and reorder it correctly to assign to fullOctal.
I get an error Index Out of Range Exception was unhandled..??!
I checked the code, if I input 999, the code returns 7471
Which has 4 characters...
so I made the loop go through it backwards, by setting I to the length of the revOctal, so 4, and then while i != 0; i-- , add the full octal (blank) to the revOctal so 1 (which is position 4) in this case, and then to the 3rd character and then the second etc...
but it gives the above error.
What is weird is that if I state i = revOctal.Length - 1, it works, but only outputs 3 characters?!!
Please help, this is a humdinger!
Thank you

Thank you,
Kind regards
Triacona
RE: index out of range??!!
CODE
CODE
RE: index out of range??!!
Why is it -1
that would give 3, but if > or = 0 then it would include 0 so 4 characters, but if no -1 then it would include 5 characters?
Why could I not use i != 0?
would it not go 4,3,2,1 which is also 4 characters?
or is it because i is decremented only after the loop is executed and therefore will include one more (0)?
What if i moved the -- to the front of i so --i?
Thanks for all your help
Thank you,
Kind regards
Triacona
RE: index out of range??!!
Why is it -1
that would give 3, but if > or = 0 then it would include 0 so 4 characters, but if no -1 then it would include 5 characters?
Why could I not use i != 0?
would it not go 4,3,2,1 which is also 4 characters?
or is it because i is decremented only after the loop is executed and therefore will include one more (0)?
What if i moved the -- to the front of i so --i?
Thanks for all your help
Thank you,
Kind regards
Triacona
RE: index out of range??!!
It is because i starts out as 0 and therefore has to iterate through the loop at least once from 0 position??
if it is starting from 0 instead of 4, then why is the number outputting correctly, if it is 0 is that a null value in the characters and therefore does not display?
If you have 4 characters, does the numbering of each start at 0, 1, 2, 3...
If so then why does the first i not display the 0 position of the character set?
E.g. if revOctal = 7471, that is 4 characters in total...
therefore 7 is the 0 position in the character array?
therefore 4 is the 1 position in the character array?
therefore 7 is the 2 position in the character array?
therefore 1 is the 3 position in the character array?
so
CODE
CODE
CODE
CODE
Is this how you would count characters?
so it must be that i is not 0 but is designated the length - 1 so therefore iterates through from 3 to 0?
Thanks again for your help
Thank you,
Kind regards
Triacona
RE: index out of range??!!
CODE
The first i does not display the 0th position because it is assigned to length-1.
Correct
In your case, you are going through the loop backwards so you use the second idiom. It doesn't matter whether you have ++i or i++. In theory, it is marginally faster to do ++i instead of i++ because i++ saves the value, increments and returns the saved value, which isn't used. The compiler will normally optimize this out.
Don't quite understand what you mean by counting characters. The number of characters in the string is in the length attribute - you don't need to count them.