Question

i need help fixing my code. here is the assignment: Make a class that contains the...

i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers.

here is my code. it works for the long method but not the double:

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

namespace Conversion
{
   public class MyFunctions
   {
       public static bool IsLong(string s)
       {
           bool b = true;
           int i;
           s = s.Trim();
           for (i = 0; (i < s.Length); i = i + 1)
           {
               b = b && ((s[i] >= '0') && (s[i] <= '9'));
           }
           return (b);
       }
       public static bool IsDouble(string s)
       {
           bool b = true;
           int i;
           s = s.Trim();
           for (i = 0; (i < s.Length); i = i + 1)
           {
               b = b && ((s[i] >= '.') && (s[i] <= '.'));
           }
           return (b);
       }
       public static long StringToLong(string s)
       {
           long num1 = 0;
           int i;
           s = s.Trim();
           if (IsLong(s))
           {
               for (i = 0; (i < s.Length); i = i + 1)
               {
                   num1 = (num1 * 10) + (s[i] - '0');
                   Console.WriteLine("after " + num1 + " : " + s[i]);
               }
           }
           else { }
           return (num1);
       }
       public static double StringToDouble(string s)
       {
           double num2 = 0;
           int i;
           s = s.Trim();
           if (IsDouble(s))
           {
               for (i = 0; (i < s.Length); i = i + 1)
               {
                   num2 = (num2 * 10) + (s[i] - '0');
                   Console.WriteLine("after " + num2 + " : " + s[i]);
               }
           }
           else { }
           return (num2);
       }
       class Program
       {
           static void Main(string[] args)
           {
               string inp;
               long num1;
               double num2;

               Console.Write("Enter your long : ");
               inp = Console.ReadLine();
               while (inp[0] != 's')
               {
                   Console.WriteLine("check : " + IsLong(inp));
                   if (IsLong(inp))
                   {
                       num1 = MyFunctions.StringToLong(inp);
                       Console.WriteLine("long " + num1);
                   }
                   else { }
                  

                   Console.Write("Enter your double : ");
                   inp = Console.ReadLine();
                   while (inp[0] != 's')
                   {
                       Console.WriteLine("check : " + IsDouble(inp));
                       if (IsDouble(inp))
                       {
                           num2 = MyFunctions.StringToDouble(inp);
                           Console.WriteLine("double " + num2);
                       }
                       else { }
                      
                       Console.ReadKey();
                   }
               }
           }
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

OUTPUT SCREENSHOT:

COPY CODE:

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

namespace Cosole_csharp_code1
{
class Program
{
bool isLong(string s)
{
long k;
bool b = long.TryParse(s, out k);
return b;
}

bool isDouble(String s)
{
double d;
bool b = double.TryParse(s, out d);
return b;
}
long stringToLong(string s)
{
return Convert.ToInt64(s);
}
double stringToDouble(string s)
{
return Convert.ToDouble(s);
}
static void Main(string[] args)

{
Program ob = new Program();
Console.WriteLine("string to long is {0}",ob.stringToLong("234"));
Console.WriteLine("string to double is {0}", ob.stringToDouble("23.4"));
Console.WriteLine("is \"dsd\" double ? {0}",ob.isDouble("dsd"));
Console.WriteLine("is \"23.3\" double ? {0}",ob.isDouble("23.3"));
Console.WriteLine("is \"34\" long ? {0}",ob.isLong("34"));
Console.WriteLine("is \"34.1\" long ? {0}",ob.isLong("34.1"));
Console.ReadKey();
}
}
}

Add a comment
Know the answer?
Add Answer to:
i need help fixing my code. here is the assignment: Make a class that contains the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello in C#. I need help understanding and knwoing what i missed in my program. The...

    Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...

  • Could anybody give me comment here to help me understand this code? please, I want details...

    Could anybody give me comment here to help me understand this code? please, I want details comment. thank you in advance using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Framing { class Program { static void Main(string[] args) { Dictionary charEncoding = new Dictionary(); charEncoding.Add("A", "01000111"); charEncoding.Add("B", "11100011"); charEncoding.Add("C", "11000001"); charEncoding.Add("ESC", "11100000"); charEncoding.Add("FLAG", "01111110"); Console.WriteLine("Enter Sequence: "); string input = Console.ReadLine().Trim().ToUpper(); Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0')); foreach (string str in input.Split(' '))...

  • ==Modify the M8B by using C#== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

    ==Modify the M8B by using C#== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace M8B {     class Magic8Ball     {         static void Main(string[] args)         {             string question;             Console.WriteLine("I am the Magic 8-ball! Ask me a question and I will give you an answer.");             Console.Write("Your question: ");             question = Console.ReadLine();             Console.WriteLine("\nLet me part the mists of the time for you.");             Random rnd = new Random();             int roll =...

  • C#, I am getting error placing them all in one file pls set them in the...

    C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null;    }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...

  • [C#] I need to convert this if/else statements into Switch I have the code for the...

    [C#] I need to convert this if/else statements into Switch I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else.      int x1, x2, y1, y2;                 Console.WriteLine("What is smallest value of x:");                 x1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of x:");                 x2 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is smallest value of y:");                 y1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of y:");                 y2 =...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • Greetings everyone I am having a little trouble with this one. I bolded my switch case...

    Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT