Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Cant locate my error in this code
       
Author Reply
Shaker AlSalem
posted 15 posts
since Feb 20, 2012 
from

Cant locate my error in this code

  Posted on: 23 Feb 2012       
Cant figure out why it giving me error at line 116, can anyone please tell me where did i go wrong? 
Thanks!


Some people provide phone numbers using one or more alphabetic characters. Write a method that will take a single letter and display the corresponding number. You may want to take a look at your telephone for this...an old telephone that is, your smartphone will not work for this. If the character does not correspond to any number display a message stating the fact. If the character is a numerical digit, the same numerical digit must be returned. Allow upper case and lowercase characters to be accepted.  In the main, write a small application that will ask the user for a "fancy" phone number, and by calling the method, display the equivalent in just numerical digits. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shaker
{
class Program
{
public static void Main()
{

Console.Write("Enter Phone Number: ");
convertcharacter1();

}

private static void convertcharacter1()
{

int i = 0;
string character1 = Console.ReadLine();
Console.WriteLine(character1);

while (true)
{
char character = char.Parse(character1.Substring(i, 1).ToLower());

switch (character)
{
case '0':
character1 += "0";
break;

case '1': character1 += "1";
break;

case '2': character1 += "2";
break;

case '3': character1 += "3";
break;

case '4': character1 += "4";
break;

case '5': character1 += "5";
break;

case '6': character1 += "6";
break;

case '7': character1 += "7";
break;

case '8': character1 += "8";
break;

case '9': character1 += "9";
break;

case '-': character1 += "-";
break;

case 'a':
case 'b':
case 'c': character1 += "2";
break;

case 'd':
case 'e':
case 'f': character1 += "3";
break;

case 'g':
case 'h':
case 'i': character1 += "4";
break;

case 'j':
case 'k':
case 'l': character1 += "5";
break;

case 'm':
case 'n':
case 'o': character1 += "6";
break;

case 'p':
case 'q':
case 'r':
case 's': character1 += "7";
break;

case 't':
case 'u':
case 'v': character1 += "8";
break;

case 'w':
case 'x':
case 'y':
case 'z': character1 += "9";
break;


i++;


}


}
}
}
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 23 Feb 2012        0  
For the future, when you get a compiler error such as here then please tell us what the error message is and tell us which line is encountering the error. When I say which line, I do not mean line number; tell us somehow what line is encountering the error in the sense of telling us what the source code is at that line and/or identify the line in your source code so we do not have to guess.

My guess is that you have an unbalanced brackets condition. I think you are missing an end bracket ("}"). In other words, I think you have one more "{" than "}".
Thinking is a feeling; pleasant for some and unpleasant for others.
Shaker AlSalem
posted  15 posts
since  Feb 20, 2012 
from 

 Re: Cant locate my error in this code
  Posted on: 24 Feb 2012        0  
I will thanks for letting me know, but then i fixed the it looking like this, when run it and  input a number or text i get the same result..
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shaker
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Enter Phone Number: ");
            convertcharacter1();
            
        }

        private static void convertcharacter1()
        {
            
            int i = 0;
            string character1 = Console.ReadLine();
            Console.WriteLine(character1);
            
            while (true)
            {
                char character = char.Parse(character1.Substring(i, 1).ToLower());

                switch (character)
                {
                    case '0':
                        character1 += "0";
                        break;

                    case '1': character1 += "1";
                        break;

                    case '2': character1 += "2";
                        break;

                    case '3': character1 += "3";
                        break;

                    case '4': character1 += "4";
                        break;

                    case '5': character1 += "5";
                        break;

                    case '6': character1 += "6";
                        break;

                    case '7': character1 += "7";
                        break;

                    case '8': character1 += "8";
                        break;

                    case '9': character1 += "9";
                        break;

                    case '-': character1 += "-";
                        break;

                    case 'a':
                    case 'b':
                    case 'c': character1 += "2";
                        break;

                    case 'd':
                    case 'e':
                    case 'f': character1 += "3";
                        break;

                    case 'g':
                    case 'h':
                    case 'i': character1 += "4";
                        break;

                    case 'j':
                    case 'k':
                    case 'l': character1 += "5";
                        break;

                    case 'm':
                    case 'n':
                    case 'o': character1 += "6";
                        break;

                    case 'p':
                    case 'q':
                    case 'r':
                    case 's': character1 += "7";
                        break;

                    case 't':
                    case 'u':
                    case 'v': character1 += "8";
                        break;

                    case 'w':
                    case 'x':
                    case 'y':
                    case 'z': character1 += "9";
                        break;


                        

                }
                i++;
            }

           
        }
    }
}
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 24 Feb 2012        0  
I am not sure I understand what you need to do but note that you are taking characters from character1 and also putting characters at the end of it. I also do not see how your program knows to stop. And I do not see anything that will show the result.

So I think instead of using character1 as the output you need to put the new characters into a different string.

Instead of using a "while" statement you can use a "for" or "foreach" statement. If you use a for statement then you can start at 0 and end at the string length and increment by 1. So then you can remove the line that has "++i;". I could tell you exactly what the source code should be but it will be better it you figure it out. If you use a foreach then you can do your loop for each character in the string.

Then at the end of the program put a statement to show the result.
Thinking is a feeling; pleasant for some and unpleasant for others.
Shaker AlSalem
posted  15 posts
since  Feb 20, 2012 
from 

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
I would really appreciate it if you do, Ive been trying to use for and foreach, cant get it done!
Thanks a lot!
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
Please try. I want you to try. Tell me what you do not understand and/or what you think will work.

I think the foreach is easy to understand. You want to do it for each character in the string.
Thinking is a feeling; pleasant for some and unpleasant for others.
theLizard
posted  598 posts
since  Oct 18, 2009 
from 

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012   Accepted Answer     0  
This is your lucky day.

Most people do not see past the text book solutions, in your case you are trying to match characters, what you should be doing is matching charachters by their value ie 0 = 48, a = 97, A = 67

Try this code, I have only done to 0, 1, 2 (abc) and 3 (def) for you, you do the rest..


        //----------------------------------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            string s = "abc123456";
            string b;

            char c;

            for (int i = 0; i < s.Length; i++)
            {
                b = GetCharValue((c = Convert.ToChar(s.Substring(i, 1))));
            }

       }
        //----------------------------------------------------------------------
        private string GetCharValue(char c)
        {
            string s="";

            if (c == 48) //0
                s = Convert.ToString(c);

            if(c == 49) //1
                s = Convert.ToString(c);

            if (c == 50 || c >= 65 && c <= 67 || c >= 97 && c <= 99) //2
                s = "2";

            if (c == 51 || c >= 68 && c <= 70 || c >= 100 && c <= 102) //3
                s = "3";

            return (s);
       
        }

theLizard
http://www.c-sharplizards.com
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
theLizard, I think that kind of answer is not helpful. You are not the only one that does all the programming for students of programming and we have members that do not know how to program because all their programming is done for them. Students do not learn when they get everything done for them.

You are certainly correct that the problem can be done much easier. I was trying to teach the student how to program. Sample code helps but it does not teach how to think in terms of what to do and then converting that to code.
Thinking is a feeling; pleasant for some and unpleasant for others.
Prime b
posted  420 posts
since  Dec 12, 2011 
from 

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
True! You need to figure out yourself how to do such simple coding. At least attempt and post it here so we can look at it and then we'll give you advice what to do next. It's different if you had something complicated like card game for example where you have to use different classes and have a lot of complicated logic for a beginner.
Practice makes it perfect && All science is either computer programming or stamp collecting
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
In my sample above, instead of:

ConversionCharacters[((byte )c)-97]

you can just use:

ConversionCharacters[c-97]
Thinking is a feeling; pleasant for some and unpleasant for others.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
Yes, Prime b. I really am not sure what to do for you. I do want to help. I feel your concern that you might not do well in school. I really am not sure what to do for you. I am still willing to help you but I think it is better to do more than simply do the programming for you.
Thinking is a feeling; pleasant for some and unpleasant for others.
Prime b
posted  420 posts
since  Dec 12, 2011 
from 

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
I am 100% agree with you Sam. I will never learn to program if someone else will do all the homework and projects for me. But it's not that I am lazy or I don't spend enough time doing it.  I work on the problem hours before I post it, and when I do post something I honestly don't know to solve it. Especially cards.... I have never played card game in my life( parents issues), so not only I have to learn how to program, but I also have to figure out the cards.  It would have been different if this class was some C# intro, but no, this class is C# adv, with completion of this class I will receive a certificate. And just so you know, when people do help me how to solve it, I learn from it. What I do is I close the solution then I go back and I am trying to rewrite the program, and I do until I do it flawlessly. It takes me maybe 1-10 times, 10 times was the highest for me to solve a problem without getting stuck. So earlier this summer when you and vulpes did help me to review for that class, I closed your and his solution and would practice that problem until I understood. Hopefully this helps you to understand me.



Practice makes it perfect && All science is either computer programming or stamp collecting
theLizard
posted  598 posts
since  Oct 18, 2009 
from 

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
Sam,

One problem with text book solutions is that it inhibits people from thinking about real world solutions, by this I mean using text book examples without understanding how and why the code works.

Take List<t> for example, do you know how and why it works?  Do you know what is going on behind the simplicity of List.Add("Item") or List.Remove("Item").

If the OP takes my code and uses it without trying to understand the principles that make it work then the OP will NOT become a programmer, just a code hacker.

If my code does not help the OP see that there are alternatives to every solution then what can I say.

The only way that people will learn is by understanding what goes on, if you don't know what goes on you can never develop new solutions to simple tasks when you are confronted with new problems relating to the same task.

I do not use C# for anything other than web development in code behind, It took me about 2 minutes to provide an alternative to what the OP was doing which to me looked like a painful way for programming a simple task.

The op was doing things the hard way, I am showing an easier way with less code and without using too much of the built in functionality that clouds creative solutions, the OP can learn by this or not, that is the OPs decision.

I also think that by showing real world examples, people will learn much faster.

theLizard
http://www.c-sharplizards.com
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 25 Feb 2012        0  
I am talking about the "for" statement. Shaker only said that Shaker could not get the for statement to work. I just wanted Shaker to show an effort to work on the solution instead of simply asking for someone to do it all. So I agree that it is beneficial to increase "understanding how and why the code works" and that is what I was doing. I said "If you use a for statement then you can start at 0 and end at the string length and increment by 1"; note how similar that is to "for (int i = 0; i < s.Length; i++)" in your code. I just wanted Shaker to try. It would have been easier for me to just post "for (int i = 0; i < s.Length; i++)".

It would have taken more of my time for me to teach Shaker how to think like a programmer so Shaker would be better prepared in the future. It is not a matter of me being lazy or disrespectful. It is more a matter of having respect for a person's ability to learn instead of doing the work for them.
Thinking is a feeling; pleasant for some and unpleasant for others.
Shaker AlSalem
posted  15 posts
since  Feb 20, 2012 
from 

 Re: Cant locate my error in this code
  Posted on: 26 Feb 2012        0  
I appreciate Sam trying to make me learn, but i did try and post what i tried so i can get more help. theLizard thanks a lot i really appreciate ur help. I'm taking this C# class because its a requirement for my CIS major, and after that I will never ever need it this language again. I always knew and was convinced programming wasnt my thing because i know i didnt have the patience for it. I work at an IT department, building computers and maintaining them, as well as the network. In addition to that, Im studying for my CCNA certificate, and to add on that im taking 5 other classes, Impossible for me to have the time to learn this new language, thats why i come to you guys asking for help, and i appreciate theLizard for that, also you Sam. I had to tell you guys what im going through so you will understand its not about me not working hard.
Thanks.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Cant locate my error in this code
  Posted on: 26 Feb 2012        0  
Shaker, I just wanted you to say more than "Ive been trying to use for and foreach, cant get it done". If you had explained what you do not understand and/or what yo have tried then I would have explained how to do it.

Note that I answered your original question. You should have created a new thread for the new question.
Thinking is a feeling; pleasant for some and unpleasant for others.
       
6 Months Free & No Setup Fees ASP.NET Hosting!
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved