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)