Thread: Read from input skipping characters

  1. #1
    Registered User
    Join Date
    Nov 2007

    Question Read from input skipping characters

    I have a program that works like this from the command prompt

    program < input

    the input is a large list of numbers.

    i need to read every 3rd number.
    at the moment i flick through the whole list like so...
    char c
    int number, number1
    while (cin >> number1){
              number = number + number1;
                 cin >> c
    how can i skip to the third number (assuming its between 0-9) each time, without having to feed the entire input through a char?

    thanks, Karim

  2. #2
    Registered User
    Join Date
    Nov 2007
    You could put the numbers into an array such as array[x]. Then create a loop like for(x=0; ;x+=3). The loop would move to the number 3 spots after the original number in the array. That should get you every third number, then you could print it off or whatever you want to do with it. I'm pretty inexperienced but i thought I'd try to help. Gotta start somewhere.

  3. #3
    Registered User
    Join Date
    May 2007
    Also, the thing about "int" is that it doesn't exclude two or three or four digit numbers. I would ask, that kind of list is it? Is it one huge string of numbers, or is there any separation going on, such as spaces, commas, etc.? If there are such things, then the separation into the array will happen automatically. Of course, the problem with arrays is that you have to declare the number of elements each array will have, and so, you could end up giving far too many, or possibly too few. If you can figure out a way for that, or if you don't mind declaring more elements than you need, then use int number[x] or some such thing. If it's just one long string of them, the probably you'll need to put it into a char array, because that only allows one of either a character or an integer (at least I think so), which will then also do the array sorting automatically. I think this is also where vectors come in handy, but I don't know anything about vectors yet.

    The loop that SirTalkAlot415 gave you has one problem. If you initialize "x" at zero, it will give you the first number, the fourth number, and then every third after that. If you want to start with the third, initialize "x" at two (which is the third input) and then have it go every third after that with his "x+=3".

  4. #4
    Registered User
    Join Date
    Nov 2007

    thanks for your quick response

    i need the program to be as quick as possible, putting the input into an array
    wouldn't this mean manipulating the entire input first? which would be slower or the same speed as my current solution i think?

    i was thinking more along the lines of extracting data from the input as its stored by the system (input buffer?) i would imagine this would be the fastest solution but i have no idea how to achieve this.

    any ideas?

    thanks, karim

  5. #5
    Registered User
    Join Date
    Nov 2007
    no worries cin.ignore seems to work well thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. trying to read input into an array
    By trprince in forum C Programming
    Replies: 16
    Last Post: 11-17-2007, 06:20 PM
  3. Input from file no white space characters
    By Dan17 in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2006, 08:28 AM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM