Reputation: 341
What I want it to do: I'm trying to code it so the user is initially asked if they have any symptoms, and the user replies with an input, if the input is wrong, they get an error otherwise, they proceed, and select their medical condition, after they've selected one, they have the option of selecting another, if yes, it loops, if not, it breaks out of the loop.
What actually happens: It takes in user input just fine, but loops everything incorrectly.
public static void userInput()
{
Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
"\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
"\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
" 15. prescribePainkillers \n 16. gastroenteritis");
bool loop = true;
while(loop == true)
{
bool valid = false;
while (valid == false)
{
string StringInput = Console.ReadLine();
int input = 0;
if (int.TryParse(StringInput, out input))
{
valid = true;
bool done = false;
while (done == false)
{
Console.WriteLine("Thank you, Any other conditions? Y or N");
string temp = Console.ReadLine();
temp = Convert.ToString(temp);
switch (temp.ToUpper())
{
case "Y":
done = false;
loop = true;
break;
case "N":
done = true;
loop = false;
break;
}
}
}
switch (input)
{
case 1:
FACTS["headache"] = true;
break;
case 2:
FACTS["vomiting"] = true;
break;
case 3:
FACTS["cramp"] = true;
break;
case 4:
FACTS["rash"] = true;
break;
case 5:
FACTS["cough"] = true;
break;
case 6:
FACTS["fatigue"] = true;
break;
case 7:
FACTS["nausea"] = true;
break;
case 8:
FACTS["dizziness"] = true;
break;
case 9:
FACTS["cold"] = true;
break;
case 10:
FACTS["chestPain"] = true;
break;
case 11:
FACTS["infection"] = true;
break;
case 12:
FACTS["flu"] = true;
break;
case 13:
FACTS["healthy"] = true;
break;
case 14:
FACTS["prescribeAntibiotics"] = true;
break;
case 15:
FACTS["prescribePainkillers"] = true;
break;
case 16:
FACTS["gastroenteritis"] = true;
break;
default:
Console.WriteLine("Sorry, please enter a number.");
break;
}
}
}
}
Upvotes: 0
Views: 93
Reputation: 11364
Have you thought about trying to use ENUM classes?
public enum Symptoms
{
HEADACHE =1,
VOMITING =2,
CRAMP =3,
RASH =4,
COUGH = 5,
FATIGUE =6,
NAUSEA =7,
DIZZINESS =8,
COLD =9,
CHESTPAIN =10,
INFECTION =11,
FLU =12,
HEALTHY =13,
PRESCRIBEANTIBIOTICS =14,
PRESCRIBEPAINKILLERS =15,
GATROENTERITIS =16
}
and you can use this in your process.. Rids of a lot of process.
public static void userInput()
{
while (true)
{
Console.Clear();
Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
"\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
"\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
" 15. prescribePainkillers \n 16. gastroenteritis ");
string StringInput = Console.ReadLine();
int.TryParse(StringInput, out int number);
if (number > 0 && number < 17)
{
// you can do other things as well here with the symptom
Symptoms symptom = (Symptoms)Enum.Parse(typeof(Symptoms), StringInput);
FACTS[symptom.ToString()] = true;
}
Console.WriteLine("Do you want to exit [N]?");
if (Console.ReadKey().Key == ConsoleKey.N)
break;
}
}
and you can fix your AddFacts method to following:
public static void addFACTS()
{
foreach (var x in Enum.GetValues(typeof(Symptoms)))
FACTS.Add(x.ToString(), false);
}
Upvotes: 1
Reputation: 763
I think you need 'done = true;' (not false) in both 'Y' and 'N' cases. Otherwise you will stuck in this circle. And it's better if you will ask for new conditions after 'switch' statement. So, put 'switch' inside 'if (int.TryParse...' condition right before 'while (done == false)' loop.
public static void userInput()
{
var FACTS = new Dictionary<string, bool>();
Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
"\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
"\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
" 15. prescribePainkillers \n 16. gastroenteritis");
bool loop = true;
while (loop == true)
{
bool valid = false;
while (valid == false)
{
string StringInput = Console.ReadLine();
int input = 0;
if (int.TryParse(StringInput, out input))
{
valid = true;
switch (input)
{
case 1:
FACTS["headache"] = true;
break;
case 2:
FACTS["vomiting"] = true;
break;
case 3:
FACTS["cramp"] = true;
break;
case 4:
FACTS["rash"] = true;
break;
case 5:
FACTS["cough"] = true;
break;
case 6:
FACTS["fatigue"] = true;
break;
case 7:
FACTS["nausea"] = true;
break;
case 8:
FACTS["dizziness"] = true;
break;
case 9:
FACTS["cold"] = true;
break;
case 10:
FACTS["chestPain"] = true;
break;
case 11:
FACTS["infection"] = true;
break;
case 12:
FACTS["flu"] = true;
break;
case 13:
FACTS["healthy"] = true;
break;
case 14:
FACTS["prescribeAntibiotics"] = true;
break;
case 15:
FACTS["prescribePainkillers"] = true;
break;
case 16:
FACTS["gastroenteritis"] = true;
break;
default:
Console.WriteLine("Sorry, please enter a number.");
valid = false;
break;
}
if (valid == true)
{
bool done = false;
while (done == false)
{
Console.WriteLine("Thank you, Any other conditions? Y or N");
string temp = Console.ReadLine();
temp = Convert.ToString(temp);
switch (temp.ToUpper())
{
case "Y":
done = true;
loop = true;
break;
case "N":
done = true;
loop = false;
break;
}
}
}
}
}
}
}
P.S. Just added some slight changes to process the case of entering wrong number.
Upvotes: 1