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
Thank you,
Kind regards
Triacona
Thanks for a wonderful forum
The code below is set to ask the user for an input to calculate an octal number.
Code:
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