Not to derail the post, but why do so many newbies post WTF in thier titles? lmao
- Anon
WTF? Need help with getline!!!
Because it was confusing the hell out of me since I didn't know how it was supposed to work. This is a common problem with us noobs :)
To use strings you have to make sure to do #include <string>. Here's a tutorial. Also, to translate what Zahlman said to "stupid" [smile]:
Say that you use cin and the user inputs 15, and presses return. The input buffer will now contain 15 and the return char. cin wants an integer, and therefore it takes 15 away, and puts it in your variabel. Then next time you use cin it will see that the buffer isn't empty, it still contains the return char. "Perfect, a string!" says cin and puts it in your string without asking the user for input. Now the buffer is empty, and the next cin will wait for the user to input something.
Consider this:
If you input "16 17", then cin will read 16 into n1. Then it wants another number, and discards characters until it finds a number, 17, that it puts into n2. Thus cin fills two variabels with one user input. You could also have written cin >> n1 >> n2; if you wanted.
When you use cin.ignore(1000, '\n') it will discard the next 1000 chars or until it encounters a new line, which will clear the buffer assuming that the user didn't input more than 1000 chars.
Hope this makes it easier to understand.
Oh, almost forgot. As Zalhman said, newer take input from the user from inside the class. Write a function outside your class or something, and let your constructor take the input as arguments, or write set-functions, like void SetDate(int date).
Say that you use cin and the user inputs 15, and presses return. The input buffer will now contain 15 and the return char. cin wants an integer, and therefore it takes 15 away, and puts it in your variabel. Then next time you use cin it will see that the buffer isn't empty, it still contains the return char. "Perfect, a string!" says cin and puts it in your string without asking the user for input. Now the buffer is empty, and the next cin will wait for the user to input something.
Consider this:
int n1, n2;cin >> n1;cin >> n2;
If you input "16 17", then cin will read 16 into n1. Then it wants another number, and discards characters until it finds a number, 17, that it puts into n2. Thus cin fills two variabels with one user input. You could also have written cin >> n1 >> n2; if you wanted.
When you use cin.ignore(1000, '\n') it will discard the next 1000 chars or until it encounters a new line, which will clear the buffer assuming that the user didn't input more than 1000 chars.
Hope this makes it easier to understand.
Oh, almost forgot. As Zalhman said, newer take input from the user from inside the class. Write a function outside your class or something, and let your constructor take the input as arguments, or write set-functions, like void SetDate(int date).
Thanks a million perost!
I tried using getline with strings in this example:
But it gives me this error:
7 | E:\Programming\Tests\Testbed.cpp | no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::istream&, std::string&, const char[2])'
Same thing happens with cin.getline(cin,theString,'\n');
Please help?!
Ps: the program also generates a table of objects. It must cut down member strings beyond a certain length to their maximum when it displays them. That was easy with character arrays, but how should I do it with a string?
I tried using getline with strings in this example:
#include<string>#include<iostream>using namespace std;int main(){ string theString; cin.getline(cin,theString,"\n"); cout<<"You entered: \n"<<theString<<endl; while(true); return 0;}
But it gives me this error:
7 | E:\Programming\Tests\Testbed.cpp | no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::istream&, std::string&, const char[2])'
Same thing happens with cin.getline(cin,theString,'\n');
Please help?!
Ps: the program also generates a table of objects. It must cut down member strings beyond a certain length to their maximum when it displays them. That was easy with character arrays, but how should I do it with a string?
you probably want to use std::getline
example:
Here I fixed your code
example:
std::string TestString;std::getline(std::cin, TestString, '\n');
Here I fixed your code
#include<string>#include<iostream>using namespace std;int main(){ string theString; getline(cin, theString, '\n'); cout<<"You entered: \n"<<theString<<endl; //while(true); cin.ignore(1); return 0;}
Big thanks!
Can someone please tell me how to display only the first x characters of a string?
Can someone please tell me how to display only the first x characters of a string?
Quote:Original post by TLAK1001
Big thanks!
Can someone please tell me how to display only the first x characters of a string?
How about this?
cout << myString.substr(0, x) << endl;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement