cin.ignore

cin.ignore

Page 1 of 1

11 Replies - 4775 Views - Last Post: 31 December 2008 - 02:27 PM Rate Topic: -----

#1 Tmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-December 08

cin.ignore

Posted 30 December 2008 - 05:22 AM

hello people, i am still stuck with this cin.ignore, i have been told it is possible by just using the cin.ignore, i have had a few replys back which have helped but i am not ment to use a function, i have tried to do it but dont seem to work i have add new code which is below

if possible please help


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

int main()

{

	char cha1,cha2;

	cout << "Please enter a message: ";
				cin >> cha1;

	cin.ignore(100',');
	cha2 = cin.get();

	cout << "The message entered was '" << cha1 << "'." << endl;
	cout << "Then the program ignored " <<cha2 << " character\n";



	return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: cin.ignore

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: cin.ignore

Posted 30 December 2008 - 10:57 AM

Modified title to be more descriptive of the question.
Was This Post Helpful? 0
  • +
  • -

#3 wrxhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-December 08

Re: cin.ignore

Posted 30 December 2008 - 11:14 AM

I'm lost on what your asking for.... do you want to use cin.ignore or not?
Was This Post Helpful? 0
  • +
  • -

#4 Tmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-December 08

Re: cin.ignore

Posted 31 December 2008 - 05:29 AM

i want to use the cin.ignore but people keep giving answers back which does not use this and uses a function
Was This Post Helpful? 0
  • +
  • -

#5 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: cin.ignore

Posted 31 December 2008 - 10:32 AM

Lets trace the program:

You prompt the user with "enter a message". And the cin>>cha1 lets you enter a message. Lets say you type in "Hello,world!"

Cha1 and Cha2 are both character variables, so it will take the H. The next line, cin.ignore() will ignore all the way up to the ','. Then cin.get(), gets the next character in the input buffer... which is w.

The problem is that cin.ignore... ignores the character. You can't cin.get() to go back and get the character you just ignored.
Was This Post Helpful? 0
  • +
  • -

#6 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: cin.ignore

Posted 31 December 2008 - 11:21 AM

View PostTmy, on 30 Dec, 2008 - 04:22 AM, said:

hello people, i am still stuck with this cin.ignore, i have been told it is possible by just using the cin.ignore, i have had a few replys back which have helped but i am not ment to use a function, i have tried to do it but dont seem to work i have add new code which is below

if possible please help


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

int main()

{

	char cha1,cha2;

	cout << "Please enter a message: ";
				cin >> cha1;

	cin.ignore(100',');
	cha2 = cin.get();

	cout << "The message entered was '" << cha1 << "'." << endl;
	cout << "Then the program ignored " <<cha2 << " character\n";



	return 0;

}



Try changing a couple things.
when you declared
char cha1, cha2;
[code]
you declared it to only hold one character.  If you want it to hold more than one character, you have to tell it to.
[code=cpp]
char* cha1; //Like this
char cha1[1024]; //or like this, this will hold 1024 characters.



your
cin.ignore(100, ',');
is a lil impulsive. Assuming you only want to get rid of the 'White Space' character (or the <Enter>) it should be like this
cin.ignore(1);



This is EXACTLY what my book says about cin.ignore

View PostLearn C++ in 21 Days, on 30 Dec, 2008 - 04:22 AM, said:

Using cin.ignore()

At times, you want to ignore the remaining characters on a line until you hit either end of line (EOL) or end of file (EOF). The member function ignore() serves this purpose. ignore() takes two parameters: the maximum number of characters to ignore and the termination character. If you write ignore(80, '\n'), up to 80 characters will be thrown away until a newline character is found. The newline is then thrown away and the ignore() statement ends.


Hope this helps

This post has been edited by UG Cyber: 31 December 2008 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 Tmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-December 08

Re: cin.ignore

Posted 31 December 2008 - 01:20 PM

hi UB Cyber,

how can i make the program ignore a full stop or comma using this the cin.ignore only?

your extract from you book is useful i just dont know how to do it tho

thanks

This post has been edited by Tmy: 31 December 2008 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: cin.ignore

Posted 31 December 2008 - 01:40 PM

View PostTmy, on 31 Dec, 2008 - 12:20 PM, said:

hi UB Cyber,

how can i make the program ignore a full stop or comma using this the cin.ignore only?

your extract from you book is useful i just dont know how to do it tho

thanks


I don't think there is a way to use cin.ignore to ignore a '.' or a ',' without knowing what the input is going to be. You would have to know exactly where the '.' or ',' is in the input. Otherwise the only way to "ignore" a '.' or ',' is to go through and manually skip over it, but you wouldn't use cin.ignore to do that.

Generally cin.ignore isn't used to do the task you are trying to accomplish. It would be much easier to just read in each character one by one, if its not '.' or ',' you would print it, else don't print it. Its MUCH easier than using cin.ignore as it would only be like 10 lines of code and it would work for ANY input.
Was This Post Helpful? 0
  • +
  • -

#9 Tmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-December 08

Re: cin.ignore

Posted 31 December 2008 - 01:47 PM

Quote

I don't think there is a way to use cin.ignore to ignore a '.' or a ',' without knowing what the input is going to be. You would have to know exactly where the '.' or ',' is in the input. Otherwise the only way to "ignore" a '.' or ',' is to go through and manually skip over it, but you wouldn't use cin.ignore to do that.

Generally cin.ignore isn't used to do the task you are trying to accomplish. It would be much easier to just read in each character one by one, if its not '.' or ',' you would print it, else don't print it. Its MUCH easier than using cin.ignore as it would only be like 10 lines of code and it would work for ANY input.


thanks for your reply its an question on one of my exercises the exact question is

Find out how you can use cin.ignore to ignore every character up to and including a specified character for example a full stop, '.' or comma ','

i have tried and better tried

This post has been edited by Tmy: 31 December 2008 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#10 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: cin.ignore

Posted 31 December 2008 - 02:01 PM

View PostTmy, on 31 Dec, 2008 - 12:47 PM, said:

Quote

I don't think there is a way to use cin.ignore to ignore a '.' or a ',' without knowing what the input is going to be. You would have to know exactly where the '.' or ',' is in the input. Otherwise the only way to "ignore" a '.' or ',' is to go through and manually skip over it, but you wouldn't use cin.ignore to do that.

Generally cin.ignore isn't used to do the task you are trying to accomplish. It would be much easier to just read in each character one by one, if its not '.' or ',' you would print it, else don't print it. Its MUCH easier than using cin.ignore as it would only be like 10 lines of code and it would work for ANY input.


thanks for your reply its an question on one of my exercises the exact question is

Find out how you can use cin.ignore to ignore every character up to and including a specified character for example a full stop, '.' or comma ','

i have tried and better tried


Ok... thats simple. cin.ignore(1000,'.') or ',' if you want to ignore the comma.

I was under the impression you wanted to also print out everything before the ',' or '.', which would be much harder to do.

The command cin.ignore(1000,'.') will ignore the first 1000 characters or up to the first '.', which ever comes first.
Was This Post Helpful? 0
  • +
  • -

#11 Tmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-December 08

Re: cin.ignore

Posted 31 December 2008 - 02:04 PM

Quote

Ok... thats simple. cin.ignore(1000,'.') or ',' if you want to ignore the comma.

I was under the impression you wanted to also print out everything before the ',' or '.', which would be much harder to do.

The command cin.ignore(1000,'.') will ignore the first 1000 characters or up to the first '.', which ever comes first.


thanks how would i embed it within my program in my original thread?

could you show me an example? please

This post has been edited by Tmy: 31 December 2008 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#12 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: cin.ignore

Posted 31 December 2008 - 02:27 PM

View PostTmy, on 31 Dec, 2008 - 01:04 PM, said:

Quote

Ok... thats simple. cin.ignore(1000,'.') or ',' if you want to ignore the comma.

I was under the impression you wanted to also print out everything before the ',' or '.', which would be much harder to do.

The command cin.ignore(1000,'.') will ignore the first 1000 characters or up to the first '.', which ever comes first.


thanks how would i embed it within my program in my original thread?

could you show me an example? please


char onechar;
cout<<"type out a message"<<endl;
cin>>onechar;
cin.ignore(1000,'.');



I think this should work if you put this in the main function. Define a character variable. Prompt user to type out a message. Then input 1 character... cin.ignore will ignore up to and including the first '.'.

When you are prompted for an input, you type in a message, you only actually input one character while the rest of the input sits in the input buffer.


Pretty sure that works.

This post has been edited by agentkirb: 31 December 2008 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1