Advertisement
having issues with using the cin.getline funtion. Am i putting it in the wrong place or am i doing something else wrong?
cout<<"Enter Coordintates of polygon (seperated by commas) " << endl;
cin >> coor;
cin.getline (coor, 100);
strtok (coor, ",");
while (tokenPtr != NULL)
{cout << tokenPtr << '\n';
tokenPtr = strtok (NULL, ",");
cout<<coor;
}
Any help is appriciated...
cout<<"Enter Coordintates of polygon (seperated by commas) " << endl;
cin >> coor;
cin.getline (coor, 100);
strtok (coor, ",");
while (tokenPtr != NULL)
{cout << tokenPtr << '\n';
tokenPtr = strtok (NULL, ",");
cout<<coor;
}
Any help is appriciated...
Advertisement
Advertisement
-
Unsu...
Re: sorry, more home work!
Sat, May 15, 2004 - 6:31 AMgetline was always a bitch.
You have to clear the buffer before you use it. Every time it's used I think actually. So you should flush the buffer on the line above it (I may be wrong about that).
I forget how to clear the buffer though. -
-
Unsu...
Re: sorry, more home work!
Sat, May 15, 2004 - 6:52 AMOh oh, disregard everything I just said. I know what it is now.
The explanation of what's happening is more complex than the solution so bare with me:
If cin.getline is not provided a "stop" character as a third argument it uses '/n' (newline) as the default.
The problem is when you type in your input for the cin >> coor statement you have to press <enter>, which is put into the input buffer as a '/n' (newline) character. So now the first character the cin.getline comes across is the '/n' character. So it just reads it and goes to the next statement, so it looks to the user like it was skipped.
the solution is right after the cin >> coor; line put a cin.ignore() statement.
The cin.ignore() statement by default (without a argument) discards one character in the input buffer which is what you want to use in this case. But just so you know, you can put in two arguments, the first is how many characters you wish the input buffer to discard and the second is a character you want to stop discarding at (it will stop at whichever comes first).
So, seeing as it's so simple, and the explanation might be a little confusing I'll do it for you just to eschew obfuscation:
cout<<"Enter Coordintates of polygon (seperated by commas) " << endl;
cin >> coor;
cin.ignore() //You need this here. Stupid isn't it?
cin.getline (coor, 100);
strtok (coor, ",");
while (tokenPtr != NULL)
{cout << tokenPtr << '\n';
tokenPtr = strtok (NULL, ",");
cout<<coor;
}
-
-
Re: sorry, more home work!
Sat, May 22, 2004 - 11:43 AMThis code is so not C++. Yes, you use cout and cin, but that's about it. And strtok is a bad, bad, C-library function. It uses a global buffer, so don't ever use it in multi-threaded applications. Have a look at this: www.relisoft.com/book/lang...3stub.html . It is still pre-std::string, but it's a good starting point. A better implementation is described here: www.relisoft.com/book/tech/6lib.html .
Bartosz -
-
Unsu...
Re: sorry, more home work!
Sat, May 22, 2004 - 6:26 PMWhat do you mean "this code is not C++"? It's the kind of stuff they have you do in beggining C++. They are just trying to teach them very basic stuff, not worry about avoiding global buffers and what not. Say what you will about the methods, but they usually tell them what functions to use before they do the assignment. I told him about cin.ignore() because that's a function they most definitely would have been using when they are using cin.getline().
The formatting was a little whack because tribe.net changes omits some spacing and newlines, and it's not a complete function of course, but that's just because it's a fragment.
-
-
Re: sorry, more home work!
Mon, May 31, 2004 - 1:34 PMYou're right, most beginning courses just rehash the old C. It doesn't mean that's a good approach, though. All this stuff will have to be unlearned at later stages.
C++ has built-in so much intelligent string processing that it's a shame to use strtok.
Bartosz
-
-
-
-
-
Re: sorry, more home work!
Sun, May 23, 2004 - 7:30 AMwait.. why are you using getline and stream extraction? assuming coor is an appropriately sized char array, "cin >> coor" should be enough, like:
---------------------------------------
#include <iostream>
using namespace std;
int main()
{
char a[255];
cout << "enter coords (comma separated): " << endl;
cin >> a;
// debug output:
cout << a << endl;
//.. and tokenize...
}
--------------------------
and, for anything serious in C++ or Java, I'd roll my own Tokenizer.. -
-
Re: sorry, more home work!
Sun, May 23, 2004 - 7:41 AMsorry... my code above wouldn't work if they put commas in, since stream extraction goes by word and not by line, so you'd probably have to use just getline and not just cin:
cin.getline(a,255);
instead of
cin >> a;
(or be totalitarian about the users not putting spaces in with the coordinates)
-