|
-
January 3rd, 2012, 01:29 AM
#1
Having trouble building this converter without using goto
I am trying to have a statement within my cases inside my switch to send the program back to the start if the user inputs they want to continue and ends the program with goodbye if they say no. I don't understand how to backtrack to the beginning of the program without using goto.
Code:
using System;
namespace CSharp_Lesson_1_4_Practice
{
class Program
{
static void Main()
{
// The purpose of this program is to create a temperature converter from Celius to Fareinheit and vice versa.
string sInput1;
int iInput1;
string sInput2;
float iInput2;
float ftemp;
float ctemp;
string eval;
do
{
Console.WriteLine("This program converts temperatures between Celsius and Fahrenheit.\n");
Console.WriteLine("Which do you want to convert:");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius\n");
Console.Write("Enter 1 or 2 for your choice: ");
sInput1 = Console.ReadLine();
iInput1 = Int32.Parse(sInput1);
// Console.WriteLine(" {0}", iInput1);
switch (iInput1)
{
case 1:
Console.Write("Enter the temperature in Celcius so I can covnert it to Fahrenheit for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ftemp = (float)iInput2 * (float)1.8 + (float)32;
Console.Write("The temperature from Celcius to Farenheit is: {0}", ftemp);
Console.ReadLine();
do
{
Console.Write("Do you want to continue? ");
eval = Console.ReadLine();
} while (eval != "yes");
break;
case 2:
Console.Write("Enter the temperature in Fahrenheit so I can convert it to Celcius for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ctemp = ((float)iInput2 - (float)32) / (float)1.8;
Console.Write("The temperature from Farenheit to Celcius is: {0}", ctemp);
Console.ReadLine();
do
{
Console.Write("Do you want to continue? ");
eval = Console.ReadLine();
} while (eval != "yes");
break;
}
} while (iInput1 != 1 && iInput1 != 2);
Console.WriteLine("Goodbye!!!");
Console.ReadLine();
}
}
}
-
January 3rd, 2012, 03:01 PM
#2
Re: Having trouble building this converter without using goto
I made a couple minor changes to your structure:
Code:
string eval = "";
bool finished = false;
while (!finished)
{
Console.WriteLine("This program converts temperatures between Celsius and Fahrenheit.\n");
Console.WriteLine("Which do you want to convert:");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius\n");
Console.Write("Enter 1 or 2 for your choice: ");
sInput1 = Console.ReadLine();
iInput1 = Int32.Parse(sInput1);
// Console.WriteLine(" {0}", iInput1);
switch (iInput1)
{
case 1:
Console.Write("Enter the temperature in Celcius so I can covnert it to Fahrenheit for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ftemp = (float)iInput2 * (float)1.8 + (float)32;
Console.Write("The temperature from Celcius to Farenheit is: {0}", ftemp);
break;
case 2:
Console.Write("Enter the temperature in Fahrenheit so I can convert it to Celcius for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ctemp = ((float)iInput2 - (float)32) / (float)1.8;
Console.Write("The temperature from Farenheit to Celcius is: {0}", ctemp);
break;
}
Console.ReadLine();
Console.Write("Do you want to continue? ");
eval = Console.ReadLine();
if (eval != "yes")
{
finished = true;
}
}
Console.WriteLine("Goodbye!!!");
Console.ReadLine();
}
You don't need to keep looping when you ask to continue. Just ask it at the end of the switch case and then either exit the while loop if the answer is not 'yes' or keep looping if it is 'yes'.
Developing using:
.NET3.5 / VS 2010
-
January 3rd, 2012, 03:08 PM
#3
Re: Having trouble building this converter without using goto
You're on the right track, Jim Using a single loop control variable like 'finished' is good programming practice. I applied the same principle to the nested loops contained within the case structure as well.
Code:
using System;
namespace CSharp_Lesson_1_4_Practice
{
public static class Program
{
public static void Main()
{
// The purpose of this program is to create a temperature converter from Celius to Fareinheit and vice versa.
bool exitMain = false;
do
{
string inputTempStr;
string optionStr;
int optionInt;
float fTemp;
float cTemp;
bool exitConvert = false;
Console.WriteLine("This program converts temperatures between Celsius and Fahrenheit.\n");
Console.WriteLine("Which do you want to convert:");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");
Console.WriteLine("0. Exit\n");
Console.Write("Enter 0, 1 or 2 for your choice: ");
optionStr = Console.ReadLine();
optionInt = Int32.Parse(optionStr);
switch (optionInt)
{
case 0:
exitMain = true;
break;
case 1:
do
{
string eval;
// ask for user to input Celsius temperature
Console.Write("Enter the temperature in Celsius so I can convert it to Fahrenheit for you: ");
inputTempStr = Console.ReadLine();
cTemp = Single.Parse(inputTempStr);
// calculate Fahrenheit value
fTemp = ((cTemp * 1.8F) + 32.0F);
Console.Write("The temperature from Celsius to Fahrenheit is: {0}\n", fTemp);
// see if user wants to perform another Celsius to Fahrenheit conversion
Console.Write("Do you want to continue? (yes/no): ");
eval = Console.ReadLine();
if (eval.ToLower().Equals("no"))
{
exitConvert = true; // no? set convert loop control variable to false
}
}
while (!exitConvert); // exit temp conversion loop?
break;
case 2:
do
{
string eval;
// ask for user to input Celsius temperature
Console.Write("Enter the temperature in Fahrenheit so I can convert it to Celsius for you: ");
inputTempStr = Console.ReadLine();
fTemp = Single.Parse(inputTempStr);
// calculate Fahrenheit value
cTemp = ((fTemp - 32.0F) / 1.8F);
Console.Write("The temperature from Fahrenheit to Celsius is: {0}\n", cTemp);
// see if user wants to perform another Celsius to Fahrenheit conversion
Console.Write("Do you want to continue? (yes/no): ");
eval = Console.ReadLine();
if (eval.ToLower().Equals("no"))
{
exitConvert = true; // no? set convert loop control variable to false
}
}
while (!exitConvert); // exit temp conversion loop?
break;
}
}
while (!exitMain);
Console.WriteLine("Goodbye!!!");
Console.ReadLine();
}
}
}
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
January 6th, 2012, 10:12 AM
#4
Re: Having trouble building this converter without using goto
I ended up going to the Visual Studio Forums for my answer because I had to wait 3 days for this post to be approved. I came up with this:
Minimethod:
Code:
using System;
namespace CSharp_Lesson_1_4_Practice
{
class Minimethod
{
public static bool minim()
{
bool repeat1 = true;
bool repeat2 = false;
do
{
Console.Write("Do you want to continue? Yes or no?");
switch (Console.ReadLine())
{
case "yes":
repeat1 = true;
repeat2 = true;
break;
case "no":
repeat1 = false;
repeat2 = true;
break;
}
} while (!repeat2);
return repeat1;
}
}
}
Main Method
Code:
using System;
namespace CSharp_Lesson_1_4_Practice
{
class Program
{
static void Main()
{
// The purpose of this program is to create a temperature converter from Celius to Fareinheit and vice versa.
string sInput1;
int iInput1;
string sInput2;
float iInput2;
float ftemp;
float ctemp;
string eval;
bool repeat1 = true;
do
{
Console.WriteLine("This program converts temperatures between Celsius and Fahrenheit.\n");
Console.WriteLine("Which do you want to convert:");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius\n");
Console.Write("Enter 1 or 2 for your choice: ");
sInput1 = Console.ReadLine();
iInput1 = Int32.Parse(sInput1);
// Console.WriteLine(" {0}", iInput1);
switch (iInput1)
{
case 1:
Console.Write("Enter the temperature in Celcius so I can covnert it to Fahrenheit for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ftemp = (float)iInput2 * (float)1.8 + (float)32;
Console.Write("The temperature from Celcius to Farenheit is: {0}", ftemp);
Console.ReadLine();
repeat1 = Minimethod.minim();
break;
case 2:
Console.Write("Enter the temperature in Fahrenheit so I can convert it to Celcius for you: ");
sInput2 = Console.ReadLine();
iInput2 = float.Parse(sInput2);
ctemp = ((float)iInput2 - (float)32) / (float)1.8;
Console.Write("The temperature from Farenheit to Celcius is: {0}", ctemp);
Console.ReadLine();
repeat1 = Minimethod.minim();
break;
}
} while (repeat1);
Console.WriteLine("Goodbye!!!");
Console.ReadLine();
}
}
}
I will review your posts and see what I can learn from them. Thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|