-
cin.ignore()
Hey guys I was sitting on Efnet irc and this kid came in saying that he needed to use cin.ignore() to block input after the first letter on a string(for a school assignment). I was just wondering if this is possible(it intriges(sp) me) and if so how would you go about it.
-
Hmm, seems like you would need to ignore it after its been typed..
odd..
It intrigues(sp) me also.
-
http://cplusplus.com/ref/iostream/istream/ignore.html
Code:
// istream ignore
#include <iostream>
using namespace std;
int main () {
char first, last;
cout << "Enter your first and last names: ";
first=cin.get();
cin.ignore(256,' ');
last=cin.get();
cout << "Your initials are " << first << last;
return 0;
}
-
If I wanted to have the option to have 3 names, how would I make it skip the last name, if the person has only 2 names?
I tried to make it like this, but it only works where there are 3 names.
Code:
int d = 0;
char a, b, c;
while (d == 0)
{
cout << "Inset your name ";
a = cin.get();
cin.ignore(30, ' ');
b = cin.get();
cin.ignore(30, ' ');
c = cin.get();
cout << endl << "Your intials are: " << a << "." << b << "."<< c << ".\n\n";
-
explicitly ask the user if they want to enter a first name/last name/other name, and if the answer is yes, then get their input, otherwise, move on. Alternatively, input the name as a single string and parse the string into the first name/middle name/last name/honorary name/etc" based on spaces between the "names".
-
I would actually keep your as is, but then check to see if c is a newline character. If c is a '\n' then they only have two names.
-
By the way, are you certain that you need to make multiple calls to ignore() like that?
-
I'm pretty new to programming, so I have no idea if I need to do it like this.
I have a new problem. Now it works with 2 names, but not 3 :(
Here is the code
Code:
main()
{
int d = 0;
char a, b, c;
while (d == 0)
{
cout << "Inset your name ";
a = cin.get();
cin.ignore(30, ' ');
b = cin.get();
if (!(c = '\n'))
{
cin.ignore(30, ' ');
c = cin.get();
}
cout << endl << "Your intials are: " << a << "." << b;
if (!(c = '\n'))
cout << "."<< c << ".\n\n";
else
cout << ".\n\n";
}
return 0;
}
And how do I clear the char variables, when it returns in the while loop?
-
Kinda, but no. Plus you don't need the infinite loop. Additionally, I was playing with it, ignore() is the most retardedly useless function ever.
Code:
#include <iostream>
int main(void) {
bool done = false;
char a, b, c;
while(!done) {
std::cout << "Inset your name ";
a = cin.get();
std::cin.ignore(30, ' ');
b = std::cin.get();
std::cin.ignore(30, ' ');
c = cin.get();
if(c == '\n')
std::cout << std::endl << "Your intials are: " << a << "." << b << std::endl;
else
std::cout << std::endl << "Your intials are: " << a << "." << b << "."<< c << std::endl;
std::cout << "continue?";
done = (std::cin.get() == 'y')?false:true;
}
return 0;
}
-
Ah hah! I've learned when something seems useless, you are using it wrong.
http://www.augustcouncil.com/~tgibso...al/iotips.html
For those interested how to use ignore().