PLease can someone help? I need to create a program that reads a list of units and names "eg ounce,gram,28" then asks for a user input, and then converts and displays the result. So far all i have been able to do is get it to read the first line, but nothing else.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Soft140AssPt3V2
{
    class Program
    {
        static void Main(string[] args)
        {
            Main:
            string line, Start, Finish, j, l;
            double Factor, Output, Amount;
            string[] SplitData = new string [2];
            StreamReader Units = new StreamReader("../../convert.txt");
            while ((line = Units.ReadLine()) != null)
            {
                SplitData = line.Split(',');
                Start = SplitData[0];
                Finish = SplitData[1];
                Factor = Convert.ToDouble(SplitData[2]);


                //Get inputs
                Console.WriteLine("Please input the amount, to and from type (Ex. 5,ounces,grams):");
                string Input = Console.ReadLine();
                string[] Measurements = Input.Split(',', ' ', '/', '.');
                Amount = Convert.ToDouble(Measurements[0]);
                j = Measurements[1];
                l = Measurements[2];

                if (j == Start)
                {
                    Output = (Factor * Amount);
                    Console.WriteLine("{0} {1} equals {2} {3}", Amount, Measurements[1], Output, Measurements[2]);
                    Console.ReadLine();
                    goto Main;
                }

                else
                {

                }

            }
            Units.Close();
        }
    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

can you please paste your convert.txt file here or just tell me the format of the text in that file

Yep it's:
ounce,gram,28.0
pound,ounce,16.0
pound,kilogram,0.454
pint,litre,0.568
inch,centimetre,2.5
mile,inch,63360.0

can you please paste your convert.txt file here or just tell me the format of the text in that file

Yep it's:
ounce,gram,28.0
pound,ounce,16.0
pound,kilogram,0.454
pint,litre,0.568
inch,centimetre,2.5
mile,inch,63360.0

And what would be the user input?

something along the lines of "5,ounce,gram"
though this is not set if there's an easier way

I dont get it. You will get some values from the file (like: gram, ounce, 5).
Then you will ask the user to insert something. But I still dont get what would that be? I dont understand your previous post. Can you be a bit more specific?
Mitja

The problem with your code is that you read one line and then ask user for input holding the current line. Do it otherwise mean first ask for input and then read the file for solution for asking input you can use a do-while loop and after taking input read the file but this includes repeatedly reading of one file... Another solution is that read you file and store all the values in arrays like ....Start[], Finish[], Factor[]..... and after taking input check in these arrays in this method file will be read only once and your output will be right

I dont get it. You will get some values from the file (like: gram, ounce, 5).
Then you will ask the user to insert something. But I still dont get what would that be? I dont understand your previous post. Can you be a bit more specific?
Mitja

Right, if the user entered say "5,ounce,gram" then it should work out how many grams are in 5 ounces. It obtains the data to do this from the .txt file "eg. ounce,gram,28" and then i should do the multiplication and output the result.
Hope that's better :D

The problem with your code is that you read one line and then ask user for input holding the current line. Do it otherwise mean first ask for input and then read the file for solution for asking input you can use a do-while loop and after taking input read the file but this includes repeatedly reading of one file... Another solution is that read you file and store all the values in arrays like ....Start[], Finish[], Factor[]..... and after taking input check in these arrays in this method file will be read only once and your output will be right

I kinda get what your last bit was on about, but would you mind elaborating for me please.

You want to convert what your user gives you input according to his entered units is that right? if yes then read your file in array of string and take input from user specifying what is the number what is the current unit what he want to convert to....
Now for checking input split one string from the array of your file lines in start, finish and factor and match if matched give output and break your loop unless check till you reach to your array.... Hope you got my point clearly if yes please let me know :)

You want to convert what your user gives you input according to his entered units is that right? if yes then read your file in array of string and take input from user specifying what is the number what is the current unit what he want to convert to....
Now for checking input split one string from the array of your file lines in start, finish and factor and match if matched give output and break your loop unless check till you reach to your array.... Hope you got my point clearly if yes please let me know :)

If i'm honest, no. But i'm gonna keep trying to figure it out.

This Code still has some problems but i think it is enough to clear my point to you,

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

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

string line, Start, Finish, j, l,lineTotal="";//linetotal to take all lines from the file
double Factor, Output, Amount;
string[] SplitData = new string [2];

string[] fileLines;
StreamReader Units = new StreamReader("../../convert.txt");
while ((line = Units.ReadLine()) != null)
{
    lineTotal+=line+"\n";
}
    fileLines=lineTotal.Split('\n');//place file lines in an array of string
Main:
//Get inputs
Console.WriteLine("Please input the amount, to and from type (Ex. 5,ounces,grams):");
string Input = Console.ReadLine();
    for(int i=0;i<fileLines.Length-1;i++)
    {
        Console.WriteLine(fileLines[i]);
        SplitData = fileLines[i].Split(',');
        Start = SplitData[0];
        Finish = SplitData[1];
        Factor = Convert.ToDouble(SplitData[2]);
       // Console.WriteLine(Start + " - " + Finish + " - " + Factor + " - ");
    string[] Measurements = Input.Split(',', ' ', '/', '.');
    Amount = Convert.ToDouble(Measurements[0]);
    j = Measurements[1];
    l = Measurements[2];

    if (j == Start)
    {
        Output = (Factor * Amount);
        Console.WriteLine("{0} {1} equals {2} {3}", Amount, Measurements[1], Output, Measurements[2]);
        Console.ReadLine();

        goto Main;
        //break;
       }

else
    {
       // Console.WriteLine("Not Matched");
       // Console.ReadLine();

    }

}
Units.Close();
}
}


}

Edited 5 Years Ago by abelLazm: n/a

This article has been dead for over six months. Start a new discussion instead.