stop reading input with cin when reach a delimiter

Hello,
Is their a function for cin to stop reading input when reaching a delimiter.
I have to read a line with multiple fields.

(int), (8 character max char array), (60 character max char array),



1
2
3
4
5
6
7
8
9
  char temp[61] = { '\0' };

			istr >> m_spotNum;
			istr.ignore();

			istr >> std::setw(8) >> m_plate;
			istr.ignore();
			istr >> std::setw(60) >> temp;
			istr.ignore();


the last field is a dynamic array so i store it in a static array for now and copy it after with other code.

is their another function i can add when reading for m_plate and temp to stop and move on when a delimiter is found.

And is is their a way i could implement the ignore() function into the same line just to make it cleaner?
What is the delimiter?

If you want to stop processing input after a delimiter is reached, then have a loop and break out of the loop.
Last edited on
the delimiter is comma, is it not possible to change the delimiter in cin for the next field input?
something like
 
cin >> setw(8) >> setdel(',') >> m_plate;


Or maybe it's more complicated than that?
How would a loop fit into my code, that would require I grab each character individualy in the stream untill the comma is reached correct?
I have found a solution via the get() function

1
2
3
4
5
6
7
8
9
10
char temp[61] = { '\0' };

			istr >> m_spotNum;
			istr.ignore();

			istr.get(m_plate, 8, ',');
			istr.ignore();

			istr.get(temp, 60, ',');
			istr.ignore();
...is it not possible to change the delimiter in cin for the next field input?
cin is an object representing the input stream. operator>> is what is delimited by whitespace.

If you want to delimit input by another character other than whitespace, you can use std::getline. Your use of istream.get is essentially the same thing except you need to pre-allocate the char* buffer.

For example,
1
2
3
4
5
string line;
while (getline(cin, line, ','))
{
    cout << line << '\n';
}


This apply to any input stream, not just cin, btw.

Alternative example, essentially is the same thing you did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream fin("input.txt");
     
    int number;
    while (fin >> number)
    {
        fin.ignore(); // ignore the comma after the number, assuming it immediately follows (no whitespace)
        
        std::string username;
        std::getline(fin >> std::ws, username, ',');
        
        std::string description;
        std::getline(fin >> std::ws, description, ',');
        
        std::cout << number << " " << username << " " << description << '\n';
    }
}

input: (replace fin with cin if you desire)
42, deli, chuggachiggawugga,
19, style,  moreletters,


output:
42 deli chuggachiggawugga
19 style moreletters


Neither of our codes would handle m_plate or temp being greater than 8 or 60 characters, respectively. If you don't trust your input, that is something to think about.
Last edited on
How could we check if it failed? would it just be a simple check for error from get or getline?
That is something i would like to implement.

at the end of the function i check if my istream object failed, if yes then it sets the value to invalid. Is that sufficient?
Last edited on
I was just pointing out something to think about in the future. I don't think it's too important at this stage. so I don't have a robust solution on hand.
It's more just something to think about: If you don't trust the input coming into your program, how should you,
(1) check that it's invalid, and (2) deal with it if it is invalid.

Also, think about how flexible you want your input to be. For example, should it be an error if your line begins with
5 ,

instead of
5,

Just stuff to think about.

The best way to check your own assumptions to form a hypothesis and test it.
Last edited on
Ah i see, okay thanks.
Topic archived. No new replies allowed.