5 Replies - 1433 Views - Last Post: 31 October 2014 - 05:30 AM Rate Topic: -----

#1 lamentofking   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 233
  • Joined: 03-July 13

How to get rid of the new line in buffer

Posted 29 October 2014 - 05:12 PM

Hello all,

I have an int array of size 5 and I have my program to accept 5 integers between 10 and 100 inclusively. I should be able to type integers over and over again until I get 5 that are in the range, 10 <= x <= 100. Now when I get 5 that fall in that range the program should continue but instead it wants a 6th number before continuing. I'm suspecting the program is hanging on to a new line character. Anyway to ignore the new lines? Couldn't find anything for C# without clearing the screen.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DupElim
{

    //dup elimination

    class DupElimTest
    {
        public static void Main(string[] args)
        {
            //const int array_SIZE = 5;
            int[] array = new int[5];
            int choice = 0;

            Console.WriteLine("Enter 5 numbers between 10 and 100 inclusive:");

            int i = 0;
            choice = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Array Length is: " +  array.Length);
            while (i < array.Length)
            {

                if (choice >= 10 && choice <= 100)
                {
                    array[i] = choice;
                    i++;
                    Console.WriteLine("Array Length is: " + array.Length);
                }
                else
                    Console.WriteLine("10 to 100 inclusive please");
                

                choice = Convert.ToInt32(Console.ReadLine());
                //Console.WriteLine();
            }
Console.ReadKey();
}
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: How to get rid of the new line in buffer

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to get rid of the new line in buffer

Posted 29 October 2014 - 06:19 PM

Check the while statement and think about the values of your variables through each iteration.

Iteration 1 = I = 0
Iteration 2 - I = 1
Iteration 3 - I = 2
Iteration 4 - I = 3
Iteration 5 - I = 4

Therefore 'I' will always be less than the array.length, until iteration 6 where I = 5

Its common for people to forget this kind of thinking when new to zero-indexed problems. But you can see it if you put a breakpoint on the first line after the while statement and look at all your local values. We have some tutorials on debugging that will help you a lot and probably introduce you to some new tool in Visual Studio that you didn't know about.

See FAQ # 2 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler


UPDATE: you also have two readline statements. One at line 23 and one at line 38. That's never good to duplicate the functionality like that because it will always come back to bite you. Re-think your design so you don't have two different places where you are taking input from the user for the same need.

Its a pretty simple fix, but as this is clearly school homework we can't give you the answer.
Was This Post Helpful? 1
  • +
  • -

#3 lamentofking   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 233
  • Joined: 03-July 13

Re: How to get rid of the new line in buffer

Posted 29 October 2014 - 10:25 PM

View PosttlhIn`toq, on 29 October 2014 - 09:19 PM, said:

Check the while statement and think about the values of your variables through each iteration.

Iteration 1 = I = 0
Iteration 2 - I = 1
Iteration 3 - I = 2
Iteration 4 - I = 3
Iteration 5 - I = 4

Therefore 'I' will always be less than the array.length, until iteration 6 where I = 5

Its common for people to forget this kind of thinking when new to zero-indexed problems. But you can see it if you put a breakpoint on the first line after the while statement and look at all your local values.

UPDATE: you also have two readline statements. One at line 23 and one at line 38. That's never good to duplicate the functionality like that because it will always come back to bite you. Re-think your design so you don't have two different places where you are taking input from the user for the same need.

Its a pretty simple fix, but as this is clearly school homework we can't give you the answer.
Thanks. I wish this was school homework but my school doesn't teach C#. The closest we have is C++ CLI. I'll definitely check out those tutorials. As I was deleting all of the FAQ stuff out of your post quote I saw a few topics of interest. But yeah I understand, you have to assume every programming question is homework.

Anyway to the task at hand, I actually learned in my intro programming class to take input once before while loop and at end of while loop loop. That was in C++ though. Is it different for C#? I went ahead and made that fix that you were hiding from me and now it doesn't ask for extra input so that's good. And as the class name implies, I was able to eliminate duplicate inputs when outputting array contents to the screen. Thanks for your help and the resources. Still can't believe you don't need the input before the while loop and at end of while loop though. Guess things change, I learned that 2 years ago.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,468
  • Joined: 05-May 12

Re: How to get rid of the new line in buffer

Posted 30 October 2014 - 05:26 AM

View Postlamentofking, on 30 October 2014 - 01:25 AM, said:

Anyway to the task at hand, I actually learned in my intro programming class to take input once before while loop and at end of while loop loop. That was in C++ though. Is it different for C#?

You follow that pattern when the loop condition is the end of file:
read from file
while (not end of file)
{
     // do work with input that was read in

     read from file
}



The code you wasn't using the end of file as the loop condition. What you had was
read from file
while (some condition not related to the file)
{
    // do work with input that was read in

    read from file
}


Was This Post Helpful? 1
  • +
  • -

#5 vpdic   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 24-November 13

Re: How to get rid of the new line in buffer

Posted 31 October 2014 - 04:42 AM

I recently read a clarification bit on the following types of loops that I'd recommend looking into. It's extremely likely that no matter what you write these bits of code will be used eventually.


In a nut shell...

DoWhile
//a loop that will always fire at least once, and continue to fire while condition(s) are met. Conditions are checked at the bottom of the loop. Ideal for when you want to perform actions at least once and continue performing said actions until conditions are met.




While
//a loop that checks the condition first, won't trigger if condition is already met, checks condition after every loop.
//Generally used to loop only when conditions are met.



For
//a loop that generally sets an initial starting index, commonly used with one condition (eg 1 < 5), and allows starting index to be easily manipulated per loop. Found mostly when not wanting to pass through all entries in an array.



Foreach
//A loop that will run through every entry within a specified array. Best when you know that you need to process all data within an array.



I'd point you to a reference but I pulled my information from a book so you'll have to do some searching on your own there.

This post has been edited by vpdic: 31 October 2014 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,468
  • Joined: 05-May 12

Re: How to get rid of the new line in buffer

Posted 31 October 2014 - 05:30 AM

The for is not just for arrays. Think of a for loop as syntactic sugar for a while loop. Given the structure of a for loop:
for(initialization; conditions; increment/decrement)
{
    do work here
}



It can be written as a while loop:
initialization
while (conditions)
{
    do work here
    increment/decrement
}



The foreach is not restricted to just arrays. It'll work with anything that implements the IEnumerable or IEnumerable<T> interface.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1