I am new to C++ and attempting to complete an exercise my question is

How you can use cin.ignore to ignore every character up to and including a specified character – for example a full stop, ’.’.

Recommended Answers

All 12 Replies

Thank you for your prompt response however, this does not answer my question. I have read previous threads but none of them seem to be quite what I need.

I have written a program but it does not work the way I would like to?

>How you can use cin.ignore to ignore every character
>up to and including a specified character
I'd start by reading my reference manual and seeing that the second argument to ignore is a delimiter.

>I have written a program but it does not work the way I would like to?
Then you're doing it wrong, obviously. Post your code so we can do more than tell you that you're doing it wrong.

If you do cin.ignore(100, 'a'), that ignores the next 100 characters or until you hit the first 'a' character, whichever comes first.

To do what you are trying to do, which is stop at a SPECIFIC CHARACTER, you should do cin.ignore(100, 'SPECIFIC CHARACTER');

100 doesn't really mean anything, it just has to be big enough to make it so that it will always stop at the specific character first.

cin.ignore(this post);

commented: cin.ignore(you) if your not careful +0

Thank you for your comment I have tried your suggestion but it still does not work to ignore a specific char like the fullstop.
Please see my source code below that I have used. I look forward to your reply.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{

string message;


	cout << "please enter a message ";
	cin >> message;
	cin.ignore(100,'.'); // to ignore the full stop

	cout << message;

	return 0;
}

cin.ignore() does not affect any of the characters in the std::string, only characters in the input buffer. If you want to delete the period from std::string then you have to do it some other way. For example, one way to do it is like this:

std::string line;
line = "Hello World.";
size_t pos = line.find('.');
if(pos != string::npos)
   line = line.substr(0,pos);
cout << string << "\n";

Again at the beginning of my thread the question was to use cin.ignore. I say this only because it is a question that forms part of my lecturers exercise. I feel that I will have to consult him next year becasue so far his question doesnt seem to have a conclusive answer from anyone todate!! Thanks for your help in the meantime

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{

string message;


	cout << "please enter a message ";
	cin >> message;
	cin.ignore(100,'.'); // to ignore the full stop

	cout << message;

	return 0;
}

Lets trace the program. When you execute it, the first thing it does is print out "please enter a message" and wait for an input.

So lets say you type "what. is. up." and push enter to let it continue. "message" is a string variable, so its going to have no problem taking the entire phrase "what. is. up.". Its not going to stop on any of the '.'s. Then the ignore statement basically does nothing because there is nothing left in the input buffer. The reason this is the case is because its going to execute each statement one at a time.

I'm not completely sure what the goal you are trying to achieve. But the way your program is set up the cin.ignore statement basically does nothing. Are you trying to get it to ignore the end line character? Because thats '\n' and not '.'. But again, when you are dealing with string input you shouldn't have to worry about the end line in inputs.

Edit: If the goal is to get the '.' out of an input, here's a way to do it that has nothing to do with C-ignore. When prompting the input, tell the user not to use a period when typing in their input. And if you want to be really savvy you can always search the input after you get it for '.'s to make sure they followed your instruction.

Thanks for all of your help it seems my original code was correct but was misunderstood what it was doing.
cin.ignore(100,'.'); // to ignore the full stop but prints everything after the full stop.

Furthermore I have learnt that using #include<limits> there is another cin.ignore that uses max limits and avoids using a number.

It took you three months to say thanks? ;) Fair enough.

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.