Reputation: 119
I am trying to utilize 2 while loops for a C# console program. The outer is to repeat the program menu and functions until the user exits. The inner loop I am using as validation of user input ( which needs to be parsed into an int).
The inner loop seems to work fine but once program gets to the switch statement, it seems to loop the switch condition endlessly. So instead of write to console once I believe the outer while loop is making it write endlessly. Only case which sets flag to false stops as expected.
Can someone explain what is happening? how would I update the flag so the outer while loop is not endlessly executing the functions in the switch? The program should go back to the user menu and await input in all cases except when user selects 7 to end program. I have been staring at this for hours. Please instruct me!
using System;
using static System.Console;
using System.Collections.Generic;
namespace Project02AreaCalculator
{
class Program
{
static private bool flag = true;
static void Main(string[] args)
{
string inputvalue = "";
int caseSwitch;
// repeat program until flag is set to false by user selecting 7 to exit
do
{
// repeat menu until user input can be parsed into an integer
while (!int.TryParse(inputvalue, out caseSwitch))
{
flag = false;
Console.WriteLine("Shape Area Calculator");
Console.WriteLine("******************************************");
Console.WriteLine("\t1. Circle");
Console.WriteLine("\t2. Square");
Console.WriteLine("\t3. Rectangle");
Console.WriteLine("\t4. Rhombus");
Console.WriteLine("\t5. Parallelogram");
Console.WriteLine("\t6. Trapezoid");
Console.WriteLine("\t7. Exit");
Console.WriteLine("******************************************");
Console.WriteLine("Select a shape type to calculate");
inputvalue = ReadLine();
}
//convert input to integer and assign to caseSwitch
caseSwitch = Convert.ToInt32(inputvalue);
switch (caseSwitch)
{
case 1:
CalculateCircle();
//Clear();
flag = true;
break;
case 2:
CalculateSquare();
// Clear();
flag = true;
break;
case 3:
CalculateRectangle();
//Clear();
flag = true;
break;
case 4:
CalculateRhombus();
//Clear();
flag = true;
break;
case 5:
CalculateParallelogram();
//Clear();
flag = true;
break;
case 6:
CalculateTrapezoid();
//Clear();
flag = true;
break;
case 7:
//Clear();
Console.WriteLine("Goodbye! Press any key to end");
ReadKey();
flag = false;
break;
default:
// Clear();
Console.WriteLine("Invalid Selection, Select 1-7");
flag = true;
break;
}
} while (caseSwitch != 7);
}//end main
//****************METHODS SECTION ****************************************************************************
static private void CalculateCircle()
{
//area = pi * radius * radius (or pi times radius squared)
Console.Clear();
Console.WriteLine("Circle Area Calculator");
Console.WriteLine("Enter the length of the circle's radius (-1 to exit back to menu):");
Console.ReadKey();
}
static private void CalculateSquare()
{
//area = side * side (or side squared)
Console.Clear();
Console.WriteLine("Square Area Calculator");
Console.WriteLine("Enter the length of one side of the square (-1 to exit back to menu):");
Console.ReadKey();
}
static private void CalculateRectangle()
{
//area = Length * Width
Console.Clear();
Console.WriteLine("Rectangle Area Calculator");
Console.WriteLine("Enter the length of one side of the Rectangle (-1 to exit back to menu):");
Console.WriteLine("Enter the length of an opposing side of the Rectangle (-1 to exit back to menu):");
Console.ReadKey();
}
static private void CalculateRhombus()
{
//area = ½ a * b (a and b being diagonals)
Console.Clear();
Console.WriteLine("Rhombus Area Calculator");
Console.WriteLine("Enter the length of one diagonal of the rhombus (-1 to exit back to menu):");
Console.WriteLine("Enter the length of the other diagonal of the rhombus (-1 to exit back to menu):");
Console.ReadKey();
}
static private void CalculateParallelogram()
{
//area = base * height
Console.WriteLine("Parallelogram Area Calculator");
Console.WriteLine("Enter the length of the base of the Parallelogram (-1 to exit back to menu):");
Console.WriteLine("Enter the height of the Parallogram (-1 to exit back to menu):");
Console.ReadKey();
}
static private void CalculateTrapezoid()
{
//area = ½ height * (largeBase + smallBase)
Console.WriteLine("Trapezoid Area Calculator");
Console.ReadKey();
}
}
}
Upvotes: 0
Views: 121
Reputation: 59
Try adding
inputvalue="";
After
caseSwitch = Convert.ToInt32(inputvalue);
Upvotes: 1
Reputation: 61
Just to make your program run do these below change,
Reset the value of inputvalue and caseSwitch before checking for user input like below,
...
...
do
{
inputvalue = "";
caseSwitch = 0;
// repeat menu until user input can be parsed into an integer
while (!int.TryParse(inputvalue, out caseSwitch))
{
...
...
And also, below statement is also not required
caseSwitch = Convert.ToInt32(inputvalue);
Other issue are,
In all of the functions it's asking for user input but input is not being used or stored in any variable.
Even in some of the functions like CalculateParallelogram, CalculateRhombus, user is asked to enter two values length and height and only one input is taken and that too single key (Console.ReadKey()).
So Change ReadKey to ReadLine and take two inputs where it's been asked. There are other logical issue also which needs to be improved in this code.
Upvotes: 1