Reputation:
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.
Upvotes: 0
Views: 400
Reputation: 100
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]);
}
}
}
Upvotes: 0
Reputation: 357
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);
}
Upvotes: 0
Reputation: 52260
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.
Upvotes: 5