0

I understand this is a question that has been asked before and I've gone through a lot of them but I really need some help.

I'm trying to create a console program for homework that takes user input of a number and then converts that number to its word counterpart. At the moment, I can create an array from the input and then generate an index. I can also return the char value as well as the index for that value. ie if input is 72 then it will return 7 = 0 and 2 = 1.

Now I basically have to create a class that will take that information and then run the code to return the words, trouble is though, I cannot make sense of classes at all. I've read all the notes, I've done extra reading and watched videos and I just cannot understand it at all. Asking my teacher gets me nowhere at all.

    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("This program will convert numbers that you enter into their spoken English counterpart.\nExample: 100 = One Hundred.\n\nPlease enter your desired number to convert: ");
        string getinput = (Console.ReadLine());
        char[] getarray = getinput.ToCharArray();

        for (int getindex = 0; getindex < getarray.Length; getindex++)
        {
            char getchar = getarray[getindex];
            Console.WriteLine("getchar: " + getchar);
            //Console.WriteLine("getarray: " + getarray);
            Console.WriteLine("getindex: " + getindex);
            //int convertinput = Convert.ToInt32(getinput);
        }
    }
    class ToText
    {
        //attributes
        private string[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; //each value is part of the array
        private string[] teens = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; //array 0 in this set = "eleven" or tens[0] = "eleven"
        private string[] tens = { "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
        private string[] moreThan99 = { "hundred", "thousand", "million", "billion", "trillion" };

        //methods
        public string ConvertToText()
        {

        }

        //constructors
        public ToText()
        {

        }

        //destructors
        ~ToText()
        {

        }
    }
}

I'm trying to understand what I need to do to push that information that I've created over to the class and then back again.

If anyone could help me understand what I'm doing, that would be much appreciated.

Thanks.

  • "I cannot make sense of classes at all" - in what respect? – ProgrammingLlama Oct 23 '18 at 06:48
  • use dictionaries and all your problems will be solved....,make the number the key and the word the value, and you can just return the index as words. – mahlatse Oct 23 '18 at 06:52
  • I'm having trouble understanding what I need to do to push the information I've created from the userinput over to the class, and what to do once I get there. I see a lot of things about "this." keywords and constructors/destructors, but what do each of these things do? How do I use them? From what I've read, constructors are what you use to call the class? I think? But then how does that fit in with the context of what I'm trying to do here? I'm just a bit lost. –  Oct 23 '18 at 06:55
  • A class is nothing but a _template_ of a vessel that stores data. That is to say, you have to create an instance of that template to store data. Imagine you have a "Person" class. By itself, it doesn't have any useful information. But you can create an instance of "Person" for each of the people you want to store information about: `Person a = new Person { Name = "John" }; Person b = new Person { Name = "GITS" };`, for example. These classes both conform to the same template, but contain different information. – ProgrammingLlama Oct 23 '18 at 06:59
  • `this` is just a special keyword which references the instance of the class which your code is running in. So if `Person` has a method Test(): `public void Test() { this.Name = "The one and only " + this.Name; }`, calling `a.Test()` will reference the person class describing me, so `a.Name` will become "The one and only John", whereas doing the same on `b` will change its name to `The one and only GITS." – ProgrammingLlama Oct 23 '18 at 07:01
  • I'd recommend finding a general object oriented programming tutorial for more detailed information on the concepts. Some docs: [Constructors](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors), [this keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this), [methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods), [properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) – ProgrammingLlama Oct 23 '18 at 07:03
  • [Fields](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields) – ProgrammingLlama Oct 23 '18 at 07:05
  • 1
    In the context of your application, I think you might want a [static class](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members) without a constructor/destructor. Then you could just call `ToText.ConvertToText(value);` (assuming you changed ConvertToText to accept a number value). – ProgrammingLlama Oct 23 '18 at 07:07
  • @John: So `var a = new Person` is me generating a new instance of that template? Also, thank you for your help on this, I'm going to read up on those links you've shared right away. @mahlatse: Would that basically be just going like `1 = "one", 2 = "two", 100 = "one hundred"`? I'll look into dictionaries as well, thanks. –  Oct 23 '18 at 07:14
  • 2
    Was it your idea to turn the input into a `char` array, or was that part of the assignment? Are you "allowed" to use `Convert.ToInt32(getinput)`? Do you know about the [modulo or remainder operator](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/remainder-operator)? -- If you feel overwhelmed by a task, it's often a good idea to break it down into smaller parts and try to solve them individually. For example: How would you solve this task for numbers only between zero and nine? – Corak Oct 23 '18 at 07:16
  • @GITS Yeah, that's exactly what you're doing. The `new` keyword creates an instance. – ProgrammingLlama Oct 23 '18 at 07:20
  • 1
    Dictionaries definitely have their place. But for this task, you probably don't want to "hard code" every possible value. Even for "just" the non-negative `int` values, you'd have to explicitly write down over two billion entries (assuming enough memory). You probably want to find a smarter way to do that. ^_^ – Corak Oct 23 '18 at 07:22
  • @Corak: The only necessity for this assignment is to use a class. Anything else is free game. I turned it into a char because I have relatively limited knowledge on arrays and someone said I should approach it as an array. We've covered that operator fairly briefly. My thought was to basically convert it to an array and get the index. So if I enter one number like 7, that will have the index of 0. Then I take that index and apply that to the ones class variable. What I'm having trouble with at the moment, is just understanding how classes interact with the main code overall. –  Oct 23 '18 at 07:46
  • @gits_ - ah, okay. The thing with strings is, they are usually read from left to right, while numbers "grow" from right to left. So for example with the string `"123"` the "ones" value (`3`) is at index 2, while with the string `"12345"` the "ones" value (`5`) is now at index 4. So you might want to "reverse" the array (`"123"` => `[3, 2, 1]`), so that the "ones" values will always have the index 0, the "tens" values always have the index 1, "hundreds" values always 2 and so on. -- but your question seems to aim at another direction. – Corak Oct 23 '18 at 10:07
  • @Corak: Yeah someone else pointed this out to me just a little while ago so I'll be sure to make that change. Thank you for your help. :) –  Oct 23 '18 at 10:20

3 Answers3

5

I cannot make sense of classes at all

From reading your example I infer the question to be

"I've declared a class and some methods. How do I call it?"

Surprisingly, there isn't another answer for this on SO (that I could find) so I guess someone has to answer.

Once you're done declaring your class (you still have to put some logic into its methods, but I think you know that) you need to figure out how to call it. Since your program is very small, you will probably want to call it from your Main method.

The class is just a type. Like any type, in order to do anything interesting with it, you have to create an instance and assign it to a variable. In this example, I've named the variable instance but you should use a descriptive name.

var instance = new ToText();

Once you have the instance, you can call its methods.

var output = instance.ConvertToText(input);

In context it would look like this:

static public void Main(string[] args)
{
    //Your other code might go here
    var instance = new ToText();
    var output = instance.ConvertToText(input);
    //and here
}

That is how you create an instance of a class (a.k.a. an object) and call its methods.

Note: There are lots of other problems in your code, but I'm just trying to answer this one question. And you should be aware that there are ways to use classes without instantiating an instance (known as "static") but that doesn't seem to be the focus of your assignment.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • 4
    +1 for patiance and giving an explanation instead of just linking a tutorial! Faith in SO community restored ;) – nilsK Oct 23 '18 at 07:56
  • Thank you so much for taking the time to explain this to me, this clears up quite a bit. Also yeah, I understand it's quite a basic question but I was struggling so much to get my head around like the actual structure and how it relates overall, so thank you for your patience as well. –  Oct 23 '18 at 10:18
0

You can call your class/method in two ways

First you can create your convertotext method as static.

public static string ConvertToText(string NumberToConvert)
{
  char[] getarray = NumberToConvert.ToCharArray();

    for (int getindex = 0; getindex < getarray.Length; getindex++)
    {
        char getchar = getarray[getindex];
        Console.WriteLine("getchar: " + getchar);
        //Console.WriteLine("getarray: " + getarray);
        Console.WriteLine("getindex: " + getindex);
        //int convertinput = Convert.ToInt32(getinput);
    }
    return ""; // return the Converted value

}

static means that you don't need to create an instance of your ToText class and then in your main method you can call it like this

static void Main(string[] args)
{
    Console.WriteLine("This program will convert numbers that you enter into their spoken English counterpart.\nExample: 100 = One Hundred.\n\nPlease enter your desired number to convert: ");
    // Static method call
    string ConvertedValue=ToText.ConvertToText(Console.ReadLine());
    Console.WriteLine(ConvertedValue);
}

Or you can just Create a instance method

public string ConvertToText(string NumberToConvert)
{
 char[] getarray = NumberToConvert.ToCharArray();

 for (int getindex = 0; getindex < getarray.Length; getindex++)
 {
    char getchar = getarray[getindex];
    Console.WriteLine("getchar: " + getchar);
    //Console.WriteLine("getarray: " + getarray);
    Console.WriteLine("getindex: " + getindex);
    //int convertinput = Convert.ToInt32(getinput);
 }
 return ""; // return the Converted value
}

and call it like following:

static void Main(string[] args){
   Console.WriteLine("This program will convert numbers that you enter into their spoken English counterpart.\nExample: 100 = One Hundred.\n\nPlease enter your desired number to convert: ");
   //Need to create an instance/object of ToText. Then call the ConvertToText via ToText Instance/Object
  ToText ToText=new ToText();
  string ConvertedValue=ToText.ConvertToText(Console.ReadLine());
  Console.WriteLine(ConvertedValue);
}
Hassan Ata Ullah
  • 347
  • 1
  • 2
  • 16
  • Ah okay, that makes a lot of sense now. Thank you for your help! –  Oct 23 '18 at 10:20
  • Glad I could help. You should take a look at this answer on how to make the conversion itself: https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp – Hassan Ata Ullah Oct 23 '18 at 10:54
0

this is my attempt. I can get it do 1 - 19. It passes the value from the main class to another class via a method. Can you post the completed solution when done as I'm also trying to solve this now.

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

        //Ask the user to input a number
        Console.WriteLine("Please enter a number and this will convert it into the the word version of the number:");


        //Inputs from console are in a string format as readline returns string
        string UserInput = Console.ReadLine();

        //Make an instance of the ConvertNumber class where the method numberConversion lives. I've called the instance numberConversion.
        ConvertNumber numberConversion = new ConvertNumber();

        //Here I'm saying numberConversion use your method NumberTooText and I'm sending the UserInput from this class along with it.
        numberConversion.NumberTooText(UserInput);

        }
    }


    class ConvertNumber
        {
            //I combined your first two arrays for one to nineteen
            private string[] oneToNineteen = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; //each value is part of the array

            //Method for number to text
            public void NumberTooText(string userinput) {

             //As I wanted to iterate and use the number for the array index we need to convert the string we got from program cs to int
                int number = Int32.Parse(userinput);

            //If the number is 19 or less the console will write from the array[userInputNumber]
// In the else statement you can create a method for greater than 19
                if (number <= 19) {
                    Console.WriteLine(oneToNineteen[number]);
                    }
                }

            }
RumbleJungle
  • 100
  • 6
  • 1
    Look at the following on how to convert Number to its string representation. https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp – Hassan Ata Ullah Oct 23 '18 at 10:56
  • Thanks, I have no idea how people think to do that. Guess it comes with time and knowledge. – RumbleJungle Oct 23 '18 at 11:52