1

I am writing code for the code selection where user can select upto 5 courses but not less than 3. user can quit by pressing -1 but he should not quit if courses are less than 3. When user press -1 and courses are less than 3, code stops working.

string[] subjects = { "English", "CS", "Math", "Eng", "Urdu", "Isl", "Web Dev" };
int ip = 0;
bool req = false;
string[] selected = new string[5];
do
{
    for (int i = 0; i < 5 && ip != -1; i++)
    {

        Console.WriteLine("Enter the Subject " + (i + 1) + " you want to select.");
        ip = Convert.ToInt32(Console.ReadLine());
        if (ip == 0)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 1)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 2)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 3)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 4)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 5)
        {
            selected[i] = subjects[ip];
        }
        else if (ip == 6)
        {
            selected[i] = subjects[ip];
        }
        if (ip == -1 && selected.Length < 3)
        {
            Console.WriteLine("You must select atleast 3 course.");
            req = true;
        }
    }
} while (!req);

  • Welcome to SO! Congratulations, your question is well formatted and clear, and as so, @mhdv could provide an answer. One thing to improve only: you said "code stops working"; you should describe it better; probably the Console freezes because, when you input -1, you transformed it in an [Endless Loop](https://stackoverflow.com/questions/1401159/which-is-the-correct-c-sharp-infinite-loop-for-or-while-true). [Do not forget to accept his answer](https://stackoverflow.com/help/someone-answers) so others will know that his code works. – Marcelo Scofano Diniz Oct 30 '22 at 21:39

1 Answers1

1

Looks like you're at the beginning of your coding journey.

I don't know whats your desired result here, but here are some tips:

  1. First of all get rid of those unnecessary ifs (ip == 0, ip == 1, etc.). You can swap it to one simple ip != -1
  2. Next, your req wont take effect immediately after 3rd loop, because its still in first iteration of do...while (which is also unnecessary there)
  3. Is using string array requirement? Because you're creating array which has length of 5 from the beginning. Then you're actually only replacing values of each element.

As I said, I don't know desired result, but maybe give this a try:

    string[] subjects = { "English", "CS", "Math", "Eng", "Urdu", "Isl", "Web Dev" };
    int ip = 0;
    
    List<string> selected = new List<string>();
    
    for (int i = 0; i < 5; i++)
    {

        Console.WriteLine($"Enter the Subject {(i + 1)} you want to select.");
        ip = Convert.ToInt32(Console.ReadLine());

        if (ip != -1)
        {
            selected.Add(subjects[ip]);
        }
        else
        {
            if(selected.Count < 3)
            {
                Console.WriteLine("You must select atleast 3 course.");
                i--;
            }
            else
                break;
        }
    }
    Console.WriteLine($"Your selected courses: {string.Join(';',selected)}");
mhdv
  • 85
  • 11