Solved What is the problem in my C# code??

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

mohittomar13

Thread Starter
Joined
May 2, 2011
Messages
372
First Name
Mohit Tomar
I have written the following code to the exercise question given at the end of the book from which I'm learning C#.. However, my code gives me an error and I'm not able to find the reason. Can someone please review the code and mention the problem area

Code:
///summary
            ///Write a program, which creates an array containing all Latin letters.
            ///The user inputs a word from the console and as result the program
            ///prints to the console the indices of the characters matching array
            static void Main()
            {
            char ch = 'A';
            char[] alph = new char[26];
            for (int row = 0; row < 26; row++)
            {
                alph[row] = ch;
                ch++;
            }
            for (int row = 0; row < 26; row++)
            {
                Console.Write(alph[row] + " ");
            }
            Console.WriteLine();
            Console.Write("Enter word: ");
            string word = Console.ReadLine();
            string wordUpper = word.ToUpper();
            int wrdIdx = 0;
            for (int i = 0; true; i++)
            {
                if (wordUpper[wrdIdx] == alph[i]) //If below if is removed then this gives as error. Same error of overflow
                {
                    Console.Write(i + " ");
                    wrdIdx++;
                    i = 0;
                    if (wordUpper[wrdIdx] == wordUpper.Length - 1) //This line is giving error if i remove it then the error shows above in the IF statement.
                        break;
                }
            }
            }


This is the output and error message that I get:

Code:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
A B C D E F G H I J K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
Enter word: mohit
12 14 7 8 19
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.String.get_Chars(Int32 index)
   at Revision.Program.Main(String[] args) in C:\Users\mohit\source\Revision\Revision\Program.cs:line 644

What I have tried:

Tried to debug the code. But as I'm a beginner so not able to understand whats going on in the debugger.

I request to please provide a very basic answer without using anything external from what I have written. As i dont know much about other functionalities provided by the various classes and methods. Please try to correct only the current code.

Please also excuse me for my English.

Thanks in advance.
 

mohittomar13

Thread Starter
Joined
May 2, 2011
Messages
372
First Name
Mohit Tomar
I did a little update to the code to include Index at the top. New code is:
Code:
 char ch = 'A';
            char[] alph = new char[26];
            for (int row = 0; row < 26; row++)
            {
                alph[row] = ch;
                ch++;
            }
            for (int numIdx = 00; numIdx < 26; numIdx++)
            {
                Console.Write(numIdx + " ");
            }
            Console.WriteLine();
            for (int row = 0; row < 26; row++)
            {
                if (row < 10)
                {
                    Console.Write(alph[row] + " ");
                }
                else
                {
                    Console.Write(alph[row] + "  ");
                }
            }
            Console.WriteLine();
            Console.Write("Enter word: ");
            string word = Console.ReadLine();
            string wordUpper = word.ToUpper();
            int wrdIdx = 0;
            for (int i = 0; true; i++)
            {
                if (wordUpper[wrdIdx] == alph[i])
                {
                    Console.Write(i + " ");
                    wrdIdx++;
                    i = 0;
                    if (wordUpper[wrdIdx] == wordUpper.Length - 1)
                        break;
                }
            }
 

mohittomar13

Thread Starter
Joined
May 2, 2011
Messages
372
First Name
Mohit Tomar
Yes, I couldn't find an answer there. However, checked it again today and a new solution is posted. I will try that if it works I will mark this solved. Thanks.
 

mohittomar13

Thread Starter
Joined
May 2, 2011
Messages
372
First Name
Mohit Tomar
Yes, the solution worked. Actual, I was trying to compare Char Type with Int32 and that is why it was giving me the error. :) :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top