3 Replies - 714 Views - Last Post: 10 May 2016 - 05:39 AM Rate Topic: -----

#1 Mrasyidi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-May 16

Why won't my If loop, loop

Posted 09 May 2016 - 06:29 AM

Hi guys,




I would like to ask regarding to if statement. I have been tried any single step to make my C# code looping. Basically, my code should be able to run to asking the user to put number to do conversion (for example 5,ounce,gram and the result should be your number is 5 equal 140 gram) and my program would not asking to enter the number again. However, if I'm press enter it would go to else condition and showing, for example is pound,ounce,16.0 Not Matched, Please try again. This is my code;




{

            string line, programStart, programEnd, a, b;

            double factor, endResult, amount;

            string[] fileLine;

            string[] SplitData = new string[2];

            StringBuilder lineTotal = new StringBuilder();




            //Read the file and display it line by line...

            System.IO.StreamReader file = 

                new System.IO.StreamReader("F:\\Algorithms\\Testing\\convert.txt");

            while ((line = file.ReadLine()) !=null)

            {

                lineTotal.Append(line + Environment.NewLine);

            }

            fileLine = lineTotal.ToString().Split(new string[]

                {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

            Console.WriteLine("File has been loaded\n");




            //Entering number to do conversion...

            //logic start here...

            Console.WriteLine("\nPlease enter the number and conversion: ");

            string userInput = Console.ReadLine();

            for (int i=0; i<=fileLine.Length + 1; i++)

            {

                Console.WriteLine(fileLine[i]);

                SplitData = fileLine[i].Split(',');

                programStart = SplitData[0];

                programEnd = SplitData[1];

                factor = Convert.ToDouble(SplitData[2]);




                //showing the result here...

                //the user should enter in the following order...

                //For example, 5,ounce,gram...

                string[] filter = userInput.Split(',', ' ', '/', '.');

                amount = Convert.ToDouble(filter[0]);

                a = filter[1];

                b = filter[2];




                if (a == programStart)

                {

                   endResult = (factor * amount);

                   Console.WriteLine("\nYour number is {0} equal {2} {3}", amount, filter[1],

                         endResult, filter[2]);

                   Console.ReadLine();

                }

                else

                {

                   Console.WriteLine("\nNot Macthed, Please try again!\n");

                   Console.ReadLine();

                }

            } 

                 

        }





I hope you will assist me in this some basic error.




Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Why won't my If loop, loop

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6832
  • View blog
  • Posts: 31,490
  • Joined: 10-May 07

Re: Why won't my If loop, loop

Posted 09 May 2016 - 07:00 AM

In your else code segment, rather than just writing static text alerting the comparison issue, output the values checked & visually verify what you think they should be vs what they actually are.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2756
  • View blog
  • Posts: 4,416
  • Joined: 21-June 11

Re: Why won't my If loop, loop

Posted 09 May 2016 - 08:25 AM

a gets its value from userInput, which is assigned exactly once, outside of the for-loop.
Was This Post Helpful? 2
  • +
  • -

#4 Mrasyidi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-May 16

Re: Why won't my If loop, loop

Posted 10 May 2016 - 05:39 AM

View Postsepp2k, on 09 May 2016 - 08:25 AM, said:

a gets its value from userInput, which is assigned exactly once, outside of the for-loop.


I see... Thank you very much now its working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1