#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    C# Test array, having problems displaying comments.


    Hello

    I am trying to write a program that asks users for a first name, last name and a test score. This will be stored in an array of structs.

    The name and test scores will be entered 3 times(same size as array). After each iteration (ie ask for first name, last name and score), I want to display a comment based on the test score.

    Example:

    Ask for first name.
    *get first name*

    Ask for last name.
    *get last name*

    Ask for test score.
    *get test score*

    Display one of these depending on test score.
    "That is an excellent score!"
    "That is a good score."
    "That is an average score."

    *loop questions again*

    Needless to say, it's not quite working. :o

    I would appreciate it if anyone could help work out where I have gone wrong.

    Many thanks!
    DHuck


    Code:
    namespace Test_Array
    {
        class Program
        {
            public struct TestScores
            {
                public string firstName;
                public string lastName;
                public int testScore;
            }
            static void Main(string[] args)
            {
                const int LENGTH = 3;
    
                TestScores[] scoreArray = new TestScores[LENGTH];
    
                EnterScores(scoreArray);
                PauseProgram();
            }
    
            public static void EnterScores(TestScores[] scoreArray)
            {
                for (int i = 0; i < scoreArray.Length; i++)
                {
                    Console.WriteLine("What is the students first name?");
                    scoreArray[i].firstName = Console.ReadLine();
                    Console.WriteLine("What is the students last name?");
                    scoreArray[i].lastName = Console.ReadLine();
                    Console.WriteLine("What is the first score?");
                    scoreArray[i].testScore = Convert.ToInt16(Console.ReadLine());
    
                    ShowScoreComments(scoreArray);
                }
            }
    
            public static void ShowScoreComments(TestScores[] scoreArray)
            {
                for (int i = 0; i < scoreArray.Length; i++)
                {
                    if (scoreArray[i].testScore >= 85 && scoreArray[i].testScore <= 100)
                        Console.WriteLine("That is an excellent score!");
                    else if (scoreArray[i].testScore >= 65 && scoreArray[i].testScore <= 84)
                        Console.WriteLine("That is a good score.");
                    else
                        Console.WriteLine("That is an average score.");
                }
            }
    
            public static void PauseProgram() 
            {
                Console.ReadKey();
            }
        }
    }
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    it's not quite working.
    Going forward, know that this is not a sufficient "bug report". We need details, like what it's doing that's unexpected. That said,

    c# Code:
    public static void EnterScores(TestScores[] scoreArray)
            {
                for (int i = 0; i < scoreArray.Length; i++)
                {
                    Console.WriteLine("What is the students first name?");
                    scoreArray[i].firstName = Console.ReadLine();
                    Console.WriteLine("What is the students last name?");
                    scoreArray[i].lastName = Console.ReadLine();
                    Console.WriteLine("What is the first score?");
                    scoreArray[i].testScore = Convert.ToInt16(Console.ReadLine());
     
                    ShowScoreComments(scoreArray);
                }
            }


    That function call to ShowScoreComments. Are you sure you want that to be called every time you go through the loop?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    Hello

    Thanks for the reply. I've put the function call to ShowScoreComments at the end of the loop as I want the comment displayed after each "block" of 3 questions. Is there a better way to do that?

    What the program is doing that is unexpected is as follows. This is the results of the test run I just made.

    ................

    *first loop of questions*

    Console.WriteLine("What is the students first name?");
    *enter Peter*
    Console.WriteLine("What is the students last name?");
    *enter James*
    Console.WriteLine("What is the first score?");
    *enter 56*

    Console then displays:
    That is an average score.
    That is an average score.
    That is an average score.

    *correct answer, but it should only display the message one time*

    ..........

    *loop questions for the second time*

    Console.WriteLine("What is the students first name?");
    *enter Micheal*
    Console.WriteLine("What is the students last name?");
    *enter Kelly*
    Console.WriteLine("What is the first score?");
    *enter 99*

    Console then displays:
    That is an average score.
    That is an excellent score!
    That is an average score.

    *it should only display "That is an excellent score."*

    ..........

    *loop questions for third and final time*

    Console.WriteLine("What is the students first name?");
    *enter Barry*
    Console.WriteLine("What is the students last name?");
    *enter Thatcher*
    Console.WriteLine("What is the first score?");
    *enter 77*

    Console then displays:
    That is an average score.
    That is an excellent score!
    That is a good score.

    *it should only display "That is a good score".

    ...........

    I hope that makes sense lol. Thanks again for your input, it's much appreciated.

    DHuck

IMN logo majestic logo threadwatch logo seochat tools logo