Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need some help for a quiz program

    Hi,

    I'm trying to get a quiz to work but I'm needing some help. Does anyone know any code that has a if, for loop and an array in it and streams questions from a txt file?

  • #2
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Striker83 View Post
    Hi,

    I'm trying to get a quiz to work but I'm needing some help. Does anyone know any code that has a if, for loop and an array in it and streams questions from a txt file?
    I think... every language that can be classed as its own programming language can do that O.o.


    Do you have any other specifications? Do you want it online? Do you want a program to run? Do you want it to work on the moon?

    if 1 = yes
    php

    if 2 = yes
    c++

    if 3 = yes
    scheme

  • #3
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    From the way the problem is stated this sounds like a homework assignment to me. In which case you (Striker83) should post the code that you have now, and specify the exact problems you're having so that the members here can help you better.

  • #4
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    post what you've already figured out and it will be easier for us to help improve etc....
    streaming questions from a text file could be achieved relatively easily using the getline() method if you're using C++.

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for not being exact guys.

    It has to be in C#.

    Here's what I've got so far.

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



    namespace Quiz2
    {
    class Program
    {
    public static string[,] QuestionArray = new string[5, 10];
    public string QuestionText;
    public string[] Choices;
    public int Answer;

    static void Main(string[] args)
    {
    int correct = 0;
    using (StreamReader sr = new StreamReader("questions.txt"))
    {
    while (!sr.EndOfStream)
    {
    Console.Clear();
    for (int i = 0; i < 5; i++)
    {
    String line = sr.ReadLine();
    if (i > 0)
    {
    if (line.Substring(0, 1) == "#") correct = i;
    Console.WriteLine("{0}: {1}", i, line);
    }
    else
    {
    Console.WriteLine(line);
    }
    static void ShowScore()
    {
    int Counter;

    for (Counter = 0; Counter < 5; Counter++)
    { Console.WriteLine(""); }
    Console.WriteLine(" " + PlayersName.ToUpper());
    Console.WriteLine("");
    Console.WriteLine(" You scored " + theScore + " out of 5.");
    Console.WriteLine("");
    Exit();
    }

    {
    int Counter1, Counter2, AnswerNumber;
    char Choice;
    string Temp = "";
    string CorrectAnswer = "";

    AnswerNumber = QuestionNumber + 1;
    Choice = 'a';

    for (Counter1 = 0; Counter1 < 3; Counter1++)
    { Console.WriteLine(""); }
    Console.WriteLine(" QUIZ");
    Console.WriteLine("");
    Console.WriteLine(" " + AnswerNumber + ". " + QuestionArray[QuestionNumber, 0]);
    Console.WriteLine("");
    for (Counter1 = 1; Counter1 < 9; Counter1 = Counter1 + 2)
    {
    if (QuestionArray[QuestionNumber, Counter1][0] == '#')
    {
    switch (Counter1)
    {
    case 1: CorrectAnswer = "a"; break;
    case 3: CorrectAnswer = "b"; break;
    case 5: CorrectAnswer = "c"; break;
    case 7: CorrectAnswer = "d"; break;
    }
    for (Counter2 = 1; Counter2 < QuestionArray[QuestionNumber, Counter1].Length; Counter2++) // Remove # and display rest of answer
    Temp = Temp + QuestionArray[QuestionNumber, Counter1][Counter2];
    Console.WriteLine(" " + Choice + ". " + Temp);
    }
    else
    {
    Console.WriteLine(" " + Choice + ". " + QuestionArray[QuestionNumber, Counter1]);
    }
    Choice++;
    }

    for (; ; )
    {
    Console.Write("Select Answer: ");
    ConsoleKeyInfo cki = Console.ReadKey();
    if (cki.KeyChar.ToString() == correct.ToString())
    {
    Console.WriteLine(" - Correct!");
    Console.WriteLine("Press any key for next question...");
    Console.ReadKey();
    break;
    }
    else
    {
    Console.WriteLine(" - Try again!");
    }
    }
    }
    }
    }

    }
    }
    }
    }
    I'm sure there's a lot wrong but I only started C# last week.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •