I have a questionnaire that gets info from the user
it asks a question like name etc then asks if hes satisfied with the name
then after all the questions it gives a summary of data supplied and then asks if its ok to proceed
if the user presses y it exits that part of the program
if n it hits the user with the questions again
a completed questionnaire followed by y works fine
but when you say n and are hit with the questions again and say y
the summary is printed out again and only on the second time the y exits the program
i printed out the bool and its always yes after pressing y and should exit the loop but it doesnt
would really appreciate some help to fix this and also a note on how to troubleshoot problems like this
heres the code:
The questionaire class:
and the ConsoleManager class with the yes no function
lastly if this is a terrible way of doing something like this and you have a better solution please advise me.
it asks a question like name etc then asks if hes satisfied with the name
then after all the questions it gives a summary of data supplied and then asks if its ok to proceed
if the user presses y it exits that part of the program
if n it hits the user with the questions again
a completed questionnaire followed by y works fine
but when you say n and are hit with the questions again and say y
the summary is printed out again and only on the second time the y exits the program
i printed out the bool and its always yes after pressing y and should exit the loop but it doesnt
would really appreciate some help to fix this and also a note on how to troubleshoot problems like this
heres the code:
The questionaire class:
C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace FootballLeague
{
class Questionaire
{
private string managerName;
private int managerAge;
private string managerAddress;
private string leagueName;
public void WelcomeUser ()
{
Console.WriteLine("Welcome to football league manager");
Console.WriteLine("**********************************");
}
public void UserQuestionaire()
{
bool hasName = false;
bool hasAge = false;
bool hasAddress = false;
bool hasLeagueName = false;
bool happyWithAnswers = false;
while (!happyWithAnswers)
{
while (!hasName)
{
managerName = ConsoleManager.GetLimitedString("Please enter your name (5 to 15 characters):", 5, 15);
hasName = ConsoleManager.GetYesNo("Are you happy with " + managerName + " as your managers name?");
}
while (!hasAge)
{
managerAge = ConsoleManager.GetInt32MinMax("Please enter your age (18 to 75 years):", 18, 75);
hasAge = ConsoleManager.GetYesNo("Are you happy with " + managerAge + " as your managers age?");
}
//mobile number goes here
while (!hasAddress)
{
managerAddress = ConsoleManager.GetLimitedString("Please enter your address (5 to 30 characters):", 5, 30);
hasAddress = ConsoleManager.GetYesNo("Are you happy with " + managerAddress + " as your managers address?");
}
while (!hasLeagueName)
{
leagueName = ConsoleManager.GetLimitedString("Please enter the league name (5 to 20 characters):", 5, 20);
hasLeagueName = ConsoleManager.GetYesNo("Are you happy with " + leagueName + " as the league's name?");
}
Console.WriteLine("\nYou entered:");
Console.WriteLine(managerName + " as your manager name.");
Console.WriteLine(managerAge + " as your managers age.");
Console.WriteLine(managerAddress + " as your managers address.");
Console.WriteLine(leagueName + " as the leagues name.");
bool happy = ConsoleManager.GetYesNo("\nAre you happy with this final selection?");
if (happy)
{
happyWithAnswers = true;
}
else
{
UserQuestionaire();
}
}
}
}
}
and the ConsoleManager class with the yes no function
C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace FootballLeague
{
public static class ConsoleManager
{
public static int GetInt32(string prompt)
{
Console.WriteLine(prompt);
var isValid = int.TryParse(Console.ReadLine(), out var number);
while (!isValid)
{
Console.WriteLine("Please enter a valid number.");
Console.WriteLine(prompt);
isValid = int.TryParse(Console.ReadLine(), out number);
}
return number;
}
public static int GetInt32MinMax(string prompt, int minNumber, int maxNumber)
{
Console.WriteLine(prompt);
int number;
while (!(int.TryParse(Console.ReadLine(), out number) && IsInRange()))
{
Console.WriteLine("Please enter a valid number.");
Console.WriteLine(prompt);
}
return number;
bool IsInRange() => minNumber <= number && number <= maxNumber;
}
public static double GetDouble (string prompt)
{
Console.WriteLine(prompt);
var isValid = double.TryParse(Console.ReadLine(), out var number);
while (!isValid)
{
Console.WriteLine("Please enter a valid number.");
Console.WriteLine(prompt);
isValid = double.TryParse(Console.ReadLine(), out number);
}
return number;
}
public static bool GetYesNo(string prompt)
{
Console.Write($"{prompt} [Y/N]: ");
while (true)
{
char ch = Console.ReadKey(true).KeyChar;
ch = char.ToUpper(ch);
switch (ch)
{
case 'Y':
case 'N':
Console.WriteLine(ch);
return ch == 'Y';
}
}
}
public static string GetLimitedString(string prompt, int minCharacters, int maxCharacters)
{
do
{
Console.WriteLine(prompt);
var userString = Console.ReadLine();
if (userString.Length >= minCharacters && userString.Length <= maxCharacters)
{
return userString;
}
else
{
Console.WriteLine("That does not meet the criteria.");
}
} while (true);
}
}
}
lastly if this is a terrible way of doing something like this and you have a better solution please advise me.