Hello

I am trying to make a program that converts different units. The conversion factors are stored within a txt file and i need to read them in.

i have this code so far; but it doesn't work.

using System;
using System.IO;

namespace Task3
{
    class TextFileReader
    {
        static void Main(string[] args)
        {

            main:
            int counter = 0;
            string line, Convertto, Convertfrom, inputfromtxtfile;
            Int32 amount, change;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
            new System.IO.StreamReader("C:/Documents and  Settings/Paul/My Documents/Visual Studio 2005/Projects/ConsoleApplication6/convert.txt");

            while ((line = file.ReadLine()) != null)
            {
                counter++;
            }



            // input amount
            Console.WriteLine ("\r\nPlease Input The Amount you Would Like To Convert");
            amount = Convert.ToInt32(Console.ReadLine());

            //Input the first unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert From");
            Convertfrom = Console.ReadLine();

            //Input The second unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert To");
            Convertto = Console.ReadLine();

            //Convert from ounce to gram
            if (Convertfrom == "ounces")
            {
                if (Convertto == "grams")
                {
                    if (counter == 0)
                    {
                        inputfromtxtfile = line.Split(',')[2];
                        change = Convert.ToInt32(inputfromtxtfile);


                        int output = amount * change;

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;
                    }
  }

i know that you use streamreader to read in the text file. but i need to read a certain word. I have split the text up with commas in between each word.

like this for example on one line.

ounces,grams,28.0

i need to read in that 28.0 in from the text file and use it in my conversion math.

so change will be

change = amount * (insert the read in number here)

Recommended Answers

All 14 Replies

but it doesn't work.

It would help alot if you would say WHAT doesn't work.
Some remarks to start:
I (believe me I'm not alone) hate a goto statement. Although I can live with only one though. Try to eliminate it if you can.

1 ounce = 28.34952 grams so why are change and amount integers?

Why do you want to store this info in a file, store it in your program, an ounce will never change to another amount of grams.

Oh and while I'm at it : put your code in code tags so it will be more easy to read.

Hiya

Sorry i don't think i made it clear what i want to do.

scrap the code i have done im probably way off the mark. i will give you the scenario.

I have a .txt file that has the conversion factors in it.

it is set out with commas in between each word. so;

ounces,grams,28.0 (28.0 is the conversion factor between ounce and grams)

basicly i need to read the conversion factor from the file into the C# code.

so if the user says i want to convert 5 ounces to grams. the program does the conversion acquiring the conversion factor from the .txt file.

amount = 5 in this case

if (convertfrom == ounces)
               if (convertto == grams)
                  converted = amount * (insert the conversion factor from the file here);
                  console.WriteLine(converted)

Well try this

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //string read in from our file
            string str = "ounces,grams,28.0";
            //allow space and comma as separators
            string[] splittedstring = str.Split(new Char[] { ' ', ','});
            //splittedstring[0] now contains "ounces" if all is well
            //splittedstring[1] now contains "grams" if all is well
            //splittedstring[2] now contains "28.0" if all is well
            Console.WriteLine(splittedstring[0]);
            Console.WriteLine(splittedstring[1]);
            Console.WriteLine(splittedstring[2]);

            Console.ReadKey();
        }
    }
}

Still remember 1 ounce = 28.34952 grams. NOT 28.0 grams.

First of all thank you for your help so far.

I have this. now.

using System;
using System.IO;

namespace Task3
{
    class TextFileReader
    {
        static void Main(string[] args)
        {

            main:
            int counter = 0;
            string Convertto, Convertfrom;
            Int32 amount, factor;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
            new System.IO.StreamReader("C:/Documents and Settings/Paul/My Documents/Visual Studio 2005/Projects/ConsoleApplication6/convert.txt");
            
           
            //string read in from our file

            string str = "ounces,grams,28.0";

            //allow space and comma as separators

           String[] splittedint = str.Split(new Char[] { ' ', ','});

            
            
            // input amount
            Console.WriteLine ("\r\nPlease Input The Amount you Would Like To Convert");
            amount = Convert.ToInt32(Console.ReadLine());

            //Input the first unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert From");
            Convertfrom = Console.ReadLine();
 
            //Input The second unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert To");
            Convertto = Console.ReadLine();

          
            

            //Convert from ounce to gram
            if (Convertfrom == "ounces")
            {
                if (Convertto == "grams")
                {
                   
                factor = Convert.ToInt32(splittedint[2]);                                             
                        Int32 output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;

                 }
              }
          }



      }
  }

My code says it cannot convert splittedint[2] to Int32. But i need to convert that to int so i can do the conversion.

I want it so the user can choose to input many different types of units to convert from and too. then the output must say

i.e. in this case; 5 ounces is 140 grams.

5 ounces is 141.7476 grams, not 140.
That matters in a lot of different cases.( A Mars lander crashed on the surface of Mars just because of that. Any idea what that costs?)
Ever heard of a datatype called double? Try it out.

Cant believe i didn't get that one. of course you use a double *doh*


Last thing how can i extend that so it deals with several lines of code in the same format.

i.e.
ounce,grams,28.0(will change this later)
pound,ounce,16.0
pint,litre,0.568
inch,centimetre,2.5
mile,inch,63360.0

i need to read in all of these and do the same as before only with a different if function this time obviously.

i.e.

if (convertfrom == "pounds")
    if (convertto == "ounces")
        convert.ToDouble splittedint[(insert appropriate number]
         Console.WriteLine("\r\n{0} {1} is {2} {3}", amount,  Convertfrom, output, Convertto);
                      goto main;

To remind you :
ounce,gram,28.34952
pound,ounce,16.0
pint(US),liter,0.4731765
inch,centimeter,2.54
mile,inch,63360.0
You talk about litre and centimetre, parlez vous français?
May I point you to another flaw in your programming scheme : what happens if I want to convert a "dog" to a "cat"?

I am going to do validation after i get the core program working.

Does the code you first gave me actually read from the .txt file? or just define string str as "ounces,grams,28.0" ?

i need to read the data from the .txt file itself. i have added a counter that inputs all the lines of code from the .txt file, it looks like this

System.IO.StreamReader file =
            new System.IO.StreamReader("C:/Documents and Settings/Paul/My Documents/Visual Studio 2005/Projects/ConsoleApplication6/convert.txt");

            int counter = 0;
            string line;


            while ((line = file.ReadLine()) != null)
            {
                counter++;
            }

The counter reads in each line obviously. I need to split each line into each word now, and then use the Conversion factors.

Just used a string. Your file code seems correct.So go for it!

using System;
using System.IO;

namespace Task3
{
    class TextFileReader
    {
        static void Main(string[] args)
        {

        main:

            string Convertto, Convertfrom;
            Int32 amount;
            Double output, factor;

            // Read the file.
            System.IO.StreamReader file =
            new System.IO.StreamReader("C:/Documents and Settings/Paul/My Documents/Visual Studio 2005/Projects/ConsoleApplication6/convert.txt");
            int counter = 0;
            string line;

            // input amount
            Console.WriteLine("\r\nPlease Input The Amount you Would Like To Convert");
            amount = Convert.ToInt32(Console.ReadLine());

            //Input the first unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert From");
            Convertfrom = Console.ReadLine();

            //Input The second unit
            Console.WriteLine("\r\nPlease Input The Unit You Would Like To Convert To");
            Convertto = Console.ReadLine();

            while ((line = file.ReadLine()) != null)
            {
                counter++;
                String[] splittedint = line.Split(new Char[] { ' ', ',' });

                //Convert from ounce to gram
                if (Convertfrom == "ounces")
                {
                    if (Convertto == "grams")
                    {

                        factor = Convert.ToDouble(splittedint[2]);
                        output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;

                    }
                }
            

            
                counter++;
               


                if (Convertfrom == "pounds")
                {
                    if (Convertto == "ounces")
                    {

                        factor = Convert.ToDouble(splittedint[2]);
                        output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;
                    }


                }

                counter++;



                if (Convertfrom == "pints")
                {
                    if (Convertto == "litres")
                    {

                        factor = Convert.ToDouble(splittedint[2]);
                        output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;
                    }


                }

                counter++;



                if (Convertfrom == "inches")
                {
                    if (Convertto == "centimetres")
                    {

                        factor = Convert.ToDouble(splittedint[2]);
                        output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;
                    }


                }

                counter++;



                if (Convertfrom == "miles")
                {
                    if (Convertto == "inches")
                    {

                        factor = Convert.ToDouble(splittedint[2]);
                        output = (amount * factor);

                        Console.WriteLine("\r\n{0} {1} is {2} {3}", amount, Convertfrom, output, Convertto);
                        goto main;
                    }


                }
            }

        }

    }
}

ok i got that. however it doesn't appear to change to the next line. So the answer i get for all conversions is 140.

i was thinking rather then using that while loop it would be more appropriate to read in the whole file and then split the whole file into words using the .split function. but i am confused as to how to do this.

GET RID OF THE GOTO!
Did I not told you before? Goto=trouble. You don't have to believe me but it is!
Make amount a double to, what in the case you have an amount of 1.5?

ok the goto is on its way out.

I need to do it so that if convertfrom doesn't == ounces. The program moves to the next line of the .txt file and then splits that line. so i can then do the if convertfrom == pounds bit.

forget my rubbish programming ways i.e. goto just for a moment and help me solve the problem in hand.

I was a bit short in my comment perhaps(I hate goto's you know)
It is however the goto that causes all your problems.
After the conversion of ounces and grams you do : goto main
Which opens a new streamreader file again and reads in the first line of that file...
Try to avoid rubbish programming : ha lets code something whatever, if it's wrong we will fix it later.
No! Try to do it good in the first place, make a design on paper and code carefully, test your code as soon as you can etc.
But who am I to tell you that, I made the same mistakes! Hence my dislike of goto!

Thank you for your help it is now working. This site has been much help i rate it very highly.

foxypj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.