0

I'm trying to build a simple country capitals quiz program using c#. Currently, I am giving the user a random country from the countries list and asking for input on what the capital is. Then there is a conditional that checks if the correct capital is equal to the input. After this completes, I need to be able to remove the country and capital asked from each array so that the loop does not repeat them. Here is the code I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace program {
  class MainClass {
    public static void Main (string[] args) {
      string[] countries = new string[] {"Germany", "Poland", "France", 
                                        "United States", "Russia", "Japan"};
      string[] capitals = new string[] {"berlin", "warsaw", "paris", 
                                       "washington dc", "moscow", "tokyo"};

      int score = 0;
      int length = countries.Length;

      string current_country = countries[random_number];
      string current_capital = capitals[random_number];

      for (int i = 0; i <= length;) {
        Random rnd = new Random();
        int random_number = rnd.Next(0, length);
        Console.WriteLine("What is the capital of {0}?", current_country);
        string capital_input = Console.ReadLine();

        if (capital_input == current_capital) {
          Console.WriteLine("Correct!");
          countries.Remove[random_number];
          capitals.Remove[random_number];
          score += 1;
          continue;
        } else {
          Console.WriteLine("Incorrect!");
          countries.Remove[random_number];
          capitals.Remove[random_number];
          continue;
      }

      if (length == 0) {
        break;
      }
    }

    int score_percent = score * 20;
    Console.WriteLine("Congratulations! You scored {0}% of questions 
                      correct.", score_percent);
  }
}

}

The program fails to compile with these errors:

exit status 1
main.cs(27,34): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(28,28): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(33,29): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
main.cs(34,28): error CS0201: Only assignment, call, increment, decrement, 
await, and new object expressions can be used as a statement
Compilation failed: 4 error(s), 0 warnings

Any ideas?

3
  • 5
    The first problem is that you can't remove elements from arrays, or add to them. I suspect you want List<string> instead. Next, Remove is a method, not an indexer - so you want Remove(...) rather than Remove[...]. Commented Feb 8, 2018 at 19:48
  • Third, you're using random_number before you've declared it. So put current_country and current_capital inside the loop, below random_number Commented Feb 8, 2018 at 19:52
  • Dont declare the Random in the loop though, make it a static member of the class and call the Next() function inside the loop. Commented Feb 8, 2018 at 19:57

2 Answers 2

1

Consider using the following:

List<string> remainingCountries = new List<string>(){ "US", "Germany", "Russia"};
void RemoveIncorrectGuess(string countryToRemove)
{
   remainingCountries.Remove(countryToRemove);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Mostly a good answer, but the OP asked to remove "by index", so you should have offered a solution using RemoveAt
0

There are many issues in your code. If you want working code then use this:

 public static void Main(string[] args) {
    List<string> countries = new List<string> { "Germany", "Poland", "France", "United States", "Russia", "Japan" };
    List<string> capitals = new List<string> { "berlin", "warsaw", "paris", "washington dc", "moscow", "tokyo" };

    int score = 0;
    Random rnd = new Random();

    while(countries.Count > 0) {
        int random_number = rnd.Next(0, countries.Count);

        string current_country = countries[random_number];
        string current_capital = capitals[random_number];

        Console.WriteLine("What is the capital of {0}?", current_country);

        string capital_input = Console.ReadLine();

        if (!string.IsNullOrWhiteSpace(capital_input) && capital_input.Equals(current_capital, StringComparison.InvariantCultureIgnoreCase)) {
            Console.WriteLine("Correct!");
            score++;
        } else {
            Console.WriteLine("Incorrect!");
        }
        countries.RemoveAt(random_number);
        capitals.RemoveAt(random_number);
    }

    int score_percent = score * 20;
    Console.WriteLine("Congratulations! You scored {0}% of questions correct.", score_percent);
    Console.ReadKey();
}

Now I try to explain some cases (also using comments):

  1. You can't remove elements from arrays using Remove[random_number]. As said J.Skeet: Remove is a method, not an indexer
  2. It would be better to use List<string> instead. Then you can remove elements like RemoveAt(random_number)
  3. You're using random_number before you've declared it.
  4. Don't declare the Random in the loop.
  5. In this case using while instead of for is more preferable
  6. Check of input capital with ignore case

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.