But the problem I am having is that the console reads these commands as answers, so here is what I want to do, make the console ignore all other text, and only accept the correct answer to the question, and once it reads the correct answer it then displays some text like: "Congratz the correct answer was " + userAnswer"
How do I do this? Here is my code?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace Quiz
{
class Program
{
private static string dataFile = @"C:\quiz2.txt";
private static Dictionary<string, string> questionArray = new Dictionary<string, string>();
private static int score = 0;
private static int credit = 100;
private string user = Console.ReadLine();
static void Main(string[] args)
{
var originalLines = File.ReadAllLines("C:\\quiz2.txt");
var shuffledLines = originalLines.OrderBy(line => Guid.NewGuid()).ToArray();
File.WriteAllLines("C:\\quiz2.txt", shuffledLines);
LoadData();
Console.WriteLine("Please enter a username.");
string user;
user = Console.ReadLine();
Console.WriteLine("Hello " + user + " and welcome to TrivKey, the fast thinking game! Type .h to start.");
string start = Console.ReadLine();
if (start.StartsWith(".h"))
{
StartQuiz();
}
}
private static void LoadData()
{
FileInfo file = new FileInfo(dataFile);
if (file.Exists)
{
using (StreamReader reader = new StreamReader(file.FullName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] splitted = line.Split('|');
if (splitted.Length == 2)
{
questionArray.Add(splitted[0], splitted[1]);
}
}
}
}
}
private static void StartQuiz()
{
for (int i = 0; i < questionArray.Count; i++)
{
AskQuestion(i);
}
}
private static void AskQuestion(int questionIndex)
{
string question = questionArray.Keys.ElementAt(questionIndex);
string correctAnswer = questionArray.Values.ElementAt(questionIndex);
Console.Write("{0} ", question);
string userAnswer = Console.ReadLine();
userAnswer = userAnswer.ToLower();
if (userAnswer != correctAnswer)
{
Console.WriteLine("Incorrect answer. Here is your next question...");
credit -= 1;
}
else
{
score += 1;
Console.WriteLine("Congratz the correct answer was " + userAnswer);
credit -= 1;
}
string start = Console.ReadLine();
if (start.StartsWith(".c"))
{
Console.WriteLine("You have {0}", credit + " credits remaining.");
}
if (start.StartsWith(".s"))
{
Console.WriteLine("Your current score is:{0}", score);
}
}
}
}
Also I am trying to add a small delay between each question(about 5 seconds) weather the user gets it right or not. Or if the user doesn?t answer correctly in 60 seconds a message should pop up and say something like "Time's up! You next question will appear shortly." And then after that 5 second delay display a new question.
If you guys will help me with this with some sample code or you can implement the code into mine, it would be very appreciated.
Thanks and enjoy your day!
This post has been edited by T0xificati0n: 11 September 2011 - 06:46 AM

New Topic/Question
Reply



MultiQuote




|