-1

So i have a project where i need to store 8 integers, between 1-10, and recall them into a histogram The histogram is made up of the character * only. (forgive bad formatting, this is my first post) The program works, but i think my if statements for int restriction (1-10) could be simplified. My goal is to achieve this output with as little code as possible

The whole code, and nothing but the code (and comments)

                     Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
                     Console.ReadKey();

            //create list to store values
            List<int> values = new List<int>();

            //loop to collect values
            for (int i = 1; i < 9; i++)
            {
                //label for data validation start point
                 retry:
                        Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
                //variable assigned to user input
                value = Console.ReadLine();

                //Convert string to integer
                if (!int.TryParse(value, out validValue)) 
                { 
                        Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
            
                 values.Add(validValue); 
            }
                 
            for (int i = 0; i < 8; i++ )
            {
                Console.WriteLine();
                for(int id = 0; id < values[i]; id++)
               
                         Console.Write("*");
            }
            
                         Console.ReadKey();

This is the area im thinking could be cleaner

if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };

Im open to any suggestions as to how i could clean this or any part of the project up.

There is a more difficult aspect to this project, like a do this if you want to show off kind of thing, where i would need the histogram to be displayed vertically. I figure this would be done with 2d arrays? i havent really got a clue other than needing 80 spaces and *'s in the right spaces lol.

Ive only been coding for a month or so and it being learner based, any help or suggestions would be welcomed

Thank you

Chris

  • 1
    You're doing a good job so far. Keep working on asking the questions in a way that you can figure out how to answer it. A couple of suggestions... *If at all possible, never use `goto`*. Try defining a variable `int validValue = 0;` and then use a `while` loop to keep cycling back until the user defines it `while (validValue == 0) { //ask user to define it }`. – Nate W Mar 05 '21 at 18:59
  • why? no goto?? And thank you for the kind words – ChristAHFER Mar 06 '21 at 18:48
  • Try searching something like https://www.bing.com/search?q=why+not+use+goto – Nate W Mar 08 '21 at 13:30

1 Answers1

0

Here's a sample of how you could reconstruct your logic with a couple of pointers.

Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
Console.ReadKey();

//create list to store values
List<int> values = new List<int>();

//loop to collect values (no need to run from 1 - 9; 0 - 8 works fine.)
for (int i = 0; i < 8; i++)
{
    //label for data validation start point
    int validValue = 0;

    //Use a while loop instead of a goto
    while (validValue == 0)
    {
        Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
        //variable assigned to user input
        var value = Console.ReadLine();

        //Convert string to integer
        //If the first question in the predicate fails, it won't go on to ask the other questions
        if (int.TryParse(value, out validValue) && validValue > 0 && validValue < 10)
        {
            values.Add(validValue);
        }
        else
        {
            Console.WriteLine("~Incorrect Data Input~");
        }
        //If the code reaches this point and validValue hasn't been changed, it'll be 0 and the loop will run again
    }
}

for (int i = 0; i < 8; i++)
{
    Console.WriteLine();
    for (int id = 0; id < values[i]; id++)

        Console.Write("*");
}

Console.ReadKey();
Nate W
  • 187
  • 12
  • Yes Nate, thank you mate. Thats what i was looking for &&. So why use a while loop as opposed to for?? – ChristAHFER Mar 06 '21 at 18:49
  • This loops at the first writeline output – ChristAHFER Mar 08 '21 at 10:53
  • As far as I can understand the purpose of your program, this does exactly the same thing as you posted, but with replacing the `goto` with a `while` loop. – Nate W Mar 08 '21 at 13:33
  • Just to clarify as to the first question you asked, there are both kinds of loops here. The `for` loop is handling incrementing through the 8 values you're trying to collect, and within each iteration of that loop, the `while` loop is making sure that each pass collects usable, parsed, valid data. On each `for` loop cycle, the `while` will loop as many times as it needs to until the user enters valid data. – Nate W Mar 08 '21 at 15:26
  • Yes i have a basic understand of the loops being used here. I tried the code you provided and the program would not stop for user input. It kept requesting a value over and over. I cant grasp why it does this, it doesnt make sense that it doesnt wait for user input – ChristAHFER Mar 09 '21 at 14:39
  • Hmm, this is odd. I had written this code in Visual Studio and it seemed to work the way I expected it to. Do you think something might have gotten mixed up with curly braces `{ }`? I'll modify my answer to extend it to include the surrounding lines of code, perhaps that will help to clarify. – Nate W Mar 09 '21 at 21:10
  • Thanks for getting back to me Nate. No i checked all the code blocks and they are fine, the code deffinatley instructs to wait for user input but the compiler acts like it loops before this point. ```while (validValue == 0) { Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");``` at this point, over and over :S – ChristAHFER Mar 12 '21 at 13:38
  • Also to add, i kept my code as it was and tried to compile the 3 IF statements for validation into one like ``` if (int.TryParse(value, out validValue) && validValue > 0 && validValue < 10) ``` Like this, and it wouldnt work. It took values over 10 under 10 and would break when a string was entered – ChristAHFER Mar 12 '21 at 13:40
  • I think the discrepancy is between your intent and my understanding. I thought that you wanted a program that the user would be requested to enter a number 8 times. In your code, you have this entitled `//loop to collect values`, which exactly the same as my code loops 8 times. If this loop is objectionable, I must not be understanding something about what you're intending the code to do. – Nate W Mar 12 '21 at 13:55