1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by mohittomar13, Oct 11, 2018.

Thread Status:
Not open for further replies.
Advertisement
  1. mohittomar13

    mohittomar13 Thread Starter

    Joined:
    May 2, 2011
    Messages:
    255
    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.
     
  2. mohittomar13

    mohittomar13 Thread Starter

    Joined:
    May 2, 2011
    Messages:
    255
    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;
                    }
                }
    
     
  3. SpywareDr

    SpywareDr

    Joined:
    Mar 30, 2014
    Messages:
    2,595
  4. mohittomar13

    mohittomar13 Thread Starter

    Joined:
    May 2, 2011
    Messages:
    255
    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.
     
  5. SpywareDr

    SpywareDr

    Joined:
    Mar 30, 2014
    Messages:
    2,595
  6. mohittomar13

    mohittomar13 Thread Starter

    Joined:
    May 2, 2011
    Messages:
    255
    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. :) :)
     
  7. SpywareDr

    SpywareDr

    Joined:
    Mar 30, 2014
    Messages:
    2,595
    Good to hear the problem's been resolved. Thanks for letting us know.
     
  8. CodeNAnime

    CodeNAnime

    Joined:
    May 13, 2018
    Messages:
    12
    Instead of true put i<alph.length()
     
  9. Sponsor

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 733,556 other people just like you!

Loading...
Tags:
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1217694

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice