I need to output the results from a .dat file then have it read in data and assign a new * for every 2% each record equals.

It is meant to appear as this:
0 10 20 30 40 50 60 70 80 90 100
| | | | | | | | | | |
*****************************************************************
**** A
************** B
********************* C
****** D
E
**** F

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

namespace Grades_10181838_Software_Development
{
    class Grades
    {
        public static void Main(string[] args)
        {
            int Agrade = 0;
            int Bgrade = 0;
            int Cgrade = 0;
            int Dgrade = 0;
            int Egrade = 0;
            int Fgrade = 0;

            TextReader reader = new StreamReader("grades.dat");
            string records;
            records = reader.ReadLine();

            while (records != null)
            {
                try
                {
                    records = reader.ReadLine();

                    if (records == "A")
                        Agrade++; //
                    if (records == "B")
                        Bgrade++; //increment grade by 1
                    if (records == "C")
                        Cgrade++; //increment grade by 1
                    if (records == "D")
                        Dgrade++; //increment grade by 1
                    if (records == "E")
                        Egrade++; //increment grade by 1
                    if (records == "F")
                        Fgrade++;//increment grade by 1
                }
                catch (Exception)
                {
                    Console.WriteLine("Error Check .dat File");
                }
                
                {
                    
                    {
                        Console.WriteLine("0\t10\t20\t30\t40\t50\t60\t70\t80\t90\t100");
                        Console.WriteLine("|\t|\t|\t|\t|\t|\t|\t|\t|\t|\t|");
                        Console.WriteLine("********************************************************************************");

                        int resultA = Agrade * 100 / 50;
                        int resultB = Bgrade * 100 / 50;
                        int resultC = Cgrade * 100 / 50;
                        int resultD = Dgrade * 100 / 50;
                        int resultE = Egrade * 100 / 50;
                        int resultF = Fgrade * 100 / 50;

                        int counter = 0;
                        while (counter != resultA)
                        {
                            Console.WriteLine("*");
                            counter ++;
                            break;
                        }
                          while (counter != resultB)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        while (counter != resultC)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        while (counter != resultD)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        while (counter != resultE)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        while (counter != resultF)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        Console.ReadLine();

                    }


                }
            }
        }
    }
}

Recommended Answers

All 9 Replies

It can write the * but will loop forever as if theres a loop in a loop

Fixed formatting slightly but now got issue as it dont read the files contents anymore it could be something to do with counter as it may read first output then stop. Otherwise its an issue with Console.ReadLine() cause it needs to read whole document and count the grades in the file to add to counter.

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

namespace Grades_10181838_Software_Development
{
    class Grades
    {
        public static void Main(string[] args)
        {
            int Agrade = 0;
            int Bgrade = 0;
            int Cgrade = 0;
            int Dgrade = 0;
            int Egrade = 0;
            int Fgrade = 0;

            TextReader reader = new StreamReader("grades.dat");
            string records;
            records = reader.ReadToEnd();

            
            {             
                try
                {
                    while (records != null)

                    if (records == "A")
                        Agrade++; //
                    else
                    records = reader.ReadLine();
                    if (records == "B")
                        Bgrade++; //increment grade by 1
                    else
                    records = reader.ReadLine();
                    if (records == "C")
                        Cgrade++; //increment grade by 1
                    else
                    records = reader.ReadLine();
                    if (records == "D")
                        Dgrade++; //increment grade by 1
                    else
                    records = reader.ReadLine();
                    if (records == "E")
                        Egrade++; //increment grade by 1
                    else
                    records = reader.ReadLine();
                    if (records == "F")
                        Fgrade++;//increment grade by 1
                    records = reader.ReadLine();
                }
                catch (Exception)
                {
                    Console.WriteLine("Error Check .dat File");
                }
                
 
                    
 
                        Console.WriteLine("0\t10\t20\t30\t40\t50\t60\t70\t80\t90\t100");
                        Console.WriteLine("|\t|\t|\t|\t|\t|\t|\t|\t|\t|\t|");
                        Console.WriteLine("********************************************************************************");

                        int resultA = Agrade * 100 / 50;
                        int resultB = Bgrade * 100 / 50;
                        int resultC = Cgrade * 100 / 50;
                        int resultD = Dgrade * 100 / 50;
                        int resultE = Egrade * 100 / 50;
                        int resultF = Fgrade * 100 / 50;

                        int counter = 0;
                        while (counter <= resultA)
                        {
                            Console.WriteLine("*");
                            counter++;
                            Console.WriteLine("A");
                        }
                        counter = 0;          
                        while (counter <= resultB)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultC)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultD)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultE)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultF)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        Console.ReadLine();

                    }


                }
            }
        }

Here is how I count the grades from the dat file.

while (line != null) //Whilst line is not empty
            {
                if (line == "A") //check if the line reads as A
                {
                    gradeA++; //Incrememnt by one
                    counter++; //Add one to counter
                }
                else
                if (line == "B") //check if the line reads as B
                    {
                        gradeB++; //Incrememnt by one
                        counter++; //Add one to counter
                    }
                else
                if (line == "C") //check if the line reads as C
                    {
                        gradeC++; //Incrememnt by one
                        counter++; //Add one to counter
                    }
                else
                if (line == "D") //check if the line reads as D
                    {
                        gradeD++; //Incrememnt by one
                        counter++; //Add one to counter
                    }
                else
                if (line == "E") //check if the line reads as E
                    {
                        gradeE++; //Incrememnt by one
                        counter++; //Add one to counter
                    }
                else
                if (line == "F") //check if the line reads as F
                    {
                        gradeF++; //Incrememnt by one
                        counter++; //Add one to counter
                    }
                line = tr.ReadLine(); //now load the next line from grades.dat

And here is how I draw the bar chat. This outputs to a web page but just ignore the <br> html tags and replace tw.WriteLine with Console.WriteLine and it should work fine

tw.WriteLine("A Grades:"); //Display a simple bar chart for each grade
            while (max <= gradeA) //Simple loop to draw appropriate number of *
            {
                tw.Write("*");
                max++;
            }
            tw.WriteLine("<br>"); //Page formatting
            max = 1; //reset to 1 so we can repeat for the next grade

            tw.WriteLine("B Grades:");
            while (max <= gradeB)
            {
                tw.Write("*");
                max++;
            }
            tw.WriteLine("<br>");
            max = 1;

            tw.WriteLine("C Grades:");
            while (max <= gradeC)
            {
                tw.Write("*");
                max++;
            }
            tw.WriteLine("<br>");
            max = 1;

            tw.WriteLine("D Grades:");
            while (max <= gradeD)
            {
                tw.Write("*");
                max++;
            }
            tw.WriteLine("<br>");
            max = 1;

            tw.WriteLine("E Grades:");
            while (max <= gradeE)
            {
                tw.Write("*");
                max++;
            }
            tw.WriteLine("<br>");
            max = 1;

            tw.WriteLine("F Grades:");
            while (max <= gradeF)
            {
                tw.Write("*");
                max++;
            }

1 quick question in your example code you have declared a tw. now where would you recommend declaring this in relation to reading the file, is it not possible to count and write at same time in one set of code rather than two sets as this is causing my conflicts often.

        tw.WriteLine("A Grades:"); //Display a simple bar chart for each grade
        while (max <= gradeA) //Simple loop to draw appropriate number of *
        {
            tw.Write("*");
            max++;
        }
        tw.WriteLine("<br>"); //Page formatting
        max = 1; //reset to 1 so we can repeat for the next grade

        tw.WriteLine("B Grades:");
        while (max <= gradeB)
        {
            tw.Write("*");
            max++;
        }
        tw.WriteLine("<br>");
        max = 1;

        tw.WriteLine("C Grades:");
        while (max <= gradeC)
        {
            tw.Write("*");
            max++;
        }
        tw.WriteLine("<br>");
        max = 1;

        tw.WriteLine("D Grades:");
        while (max <= gradeD)
        {
            tw.Write("*");
            max++;
        }
        tw.WriteLine("<br>");
        max = 1;

        tw.WriteLine("E Grades:");
        while (max <= gradeE)
        {
            tw.Write("*");
            max++;
        }
        tw.WriteLine("<br>");
        max = 1;

        tw.WriteLine("F Grades:");
        while (max <= gradeF)
        {
            tw.Write("*");
            max++;
        }

It is possible to count and write at the same time but it wouldn't be very efficient. It would require you to do something like:

Console.WriteLine("Grade A:");
while (line != null) //Whilst line is not empty
{
    if (line == "A") //check if the line reads as A
    {
        Console.WriteLine("*")
    }
    line = tr.ReadLine();
}

This means you have to loop through the entire document 6 times (once for each grade)

Whereas in the previous way of counting you only look through the file once, counting all of the grades as you go and then output a barchart.

What conflicts are you getting? I don't see a reason why there should be any if you use the same method as me.

Post up the code you have and I'll have a look.

This is the code as it stands currently however when run through console it displays nothing. The more I look at it the more confused I get.

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

namespace Grades_10181838_Software_Development
{
    class Grades
    {
        public static void Main(string[] args)
        {
            int Agrade = 0;
            int Bgrade = 0;
            int Cgrade = 0;
            int Dgrade = 0;
            int Egrade = 0;
            int Fgrade = 0;

            TextReader reader = new StreamReader("grades.dat");
            string records;
            records = reader.ReadLine();
                        
                try
                {
                    while (records != null)

                    if (records == "A")
                        Agrade++; //
                    else
                    if (records == "B")
                        Bgrade++; //increment grade by 1
                    else
                    if (records == "C")
                        Cgrade++; //increment grade by 1
                    else
                    if (records == "D")
                        Dgrade++; //increment grade by 1
                    else
                    if (records == "E")
                        Egrade++; //increment grade by 1
                    else
                    if (records == "F")
                        Fgrade++;//increment grade by 1
                    records = reader.ReadLine();
                }
                catch (Exception)
                {
                    Console.WriteLine("Error Check .dat File");
                }
                
 
                    
 
                        Console.WriteLine("0\t10\t20\t30\t40\t50\t60\t70\t80\t90\t100");
                        Console.WriteLine("|\t|\t|\t|\t|\t|\t|\t|\t|\t|\t|");
                        Console.WriteLine("********************************************************************************");

                        int resultA = Agrade * 100 / 50;
                        int resultB = Bgrade * 100 / 50;
                        int resultC = Cgrade * 100 / 50;
                        int resultD = Dgrade * 100 / 50;
                        int resultE = Egrade * 100 / 50;
                        int resultF = Fgrade * 100 / 50;

                        int counter = 0;
                        while (counter <= resultA)
                        {
                            Console.WriteLine("*");
                            counter++;
                            Console.WriteLine("A");
                        }
                        counter = 0;          
                        while (counter <= resultB)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultC)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultD)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultE)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        counter = 0; 
                        while (counter <= resultF)
                        {
                            Console.WriteLine("*");
                            counter++;
                        }
                        Console.ReadLine();

                    }


                }
            }

Needs curly braces around your while loop :) Took me a while to see that but that's the problem!

So open curly brace on line 27 and close it after records = reader.ReadLine(); Should work after that.

It has been fixed your help was fantastic only issue is its placing all records on same line but a simple addition write each grade on a new line should fix this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.