Jump to content
  • Advertisement
Sign in to follow this  
Avont29

cin.getline

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream> using std::cin; using std::cout; int main() { char text[10]; cout << "Please enter a word\n"; cin.getline(text,10); cout << text << endl; return 0; } so that program above ^^^^ lets the user type in a word and the cin.getline stores it in the array named "text", but what if you don't want a certain number to store, like you want to store whatever the user enter, or 100 words or less, would you need an array each time you do something like this, or can you have one giant array that stores words

Share this post


Link to post
Share on other sites
Advertisement

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

int main()
{
string input;
cout << "Please enter a word\n";
getline(cin, input, ' ');
cout << input << endl;
return 0;
}

Share this post


Link to post
Share on other sites
std::istream::getline() reads a line, not just a word ("get line"). It is, however, limited to the size of the buffer you pass it. std::getline() works with std::string and does not have that limitation.


#include <iostream>
#include <string>

using namespace std;

int main()
{
string line;
cout << "Do type something\n";
getline(line);
cout << line << endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Avont29
hey, which is better for like enemies on games, structors or classes?


both structs and classes are class types. The only difference is in that structs have public access by default while classes have private access by default. It is just a question of preference and convenience.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!