# newb question

• 12-07-2007
rugger78
newb question
hey sorry if this comes off as too simple but i am trying to write a simple program and right now i'm only dealing with the I/O but for some reason i am having real trouble inputing a string. The code is a little messy because i have been tryign everythign i can think of to make it work but to no avail. Can someone point out my stupid mistake so i can get back to working on it. THe problem is the geline inside the else
Code:

```#include <iostream> #include <string> #include <cctype> using namespace std; void optimal(int len, string input) {} void least(int len, string input) {} void first(int len, string input) {} void clock(int len, string input) {} int main() {     string input;     char token = ' ', in[256];     int length, other, index = 0, x = 0;     cout << "Welcome to my Caching Algorithm Simulator.\n" << endl;     cout << "Enter 0 for default string or 1 to enter custom stream: ";     length = cin.get();     cout << length << "\n\n";     if (length == '0') {         input = "2 3 2 1 5 2 4 5 3 2 5 2";         }     else {         cout << "Enter your page address stream.\n> ";         cin.getline ( in, 256 );         }     cout << "\nPlease choose your algorithm o(pt), l(ru), f(ifo), c(lock): ";         cout << "\n\n\nKLFD: " << in;     while (x == 0) {         cin >> token;         switch (tolower(token)) {             case 'o':        optimal(length, input);                         x++;                         break;             case 'l':        least(length, input);                         x++;                         break;             case 'f':        first(length, input);                         x++;                         break;             case 'c':        clock(length, input);                         x++;                         break;             default:        cout << "\n\nSorry you entered incorrect option." << endl;                         cout << "o, l, f, or c: ";                         break;             }         }     //print board     return 0;     }```
• 12-07-2007
anon
getline for string is used like:
Code:

`getline(cout, string);`
The version you have is meant for C style strings.
• 12-07-2007
rugger78
i have tried using getline ( cin, input ); also but that did not work either. my code does not allow me to enter anythign when i reach it. that is the problem im having
• 12-07-2007
anon
Sorry, yes, that's what I meant (cin, not cout).

However, your problem seems to be that cin.get() leaves characters in the stream (at least newline, unless you type more than one character) which getline will happily read.

To get rid of extra characters include <limits> and ignore the rest of the characters:
Code:

`cin.ignore(numeric_limits<streamsize>::max(), '\n');`
And use the string version of getline.
• 12-07-2007
rugger78
thanks a ton it works now, it was driving me crazy