Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a complete line from the user using cin?

Tags:

c++

iostream

Here is my current C++ code. I would like to know how to write a line of code. Would I still use cin.getline(y) or something different? I've checked, but can't find anything. When I run it, it works perfectly except it only types one word instead of the full lines I need it to output. This is what I need help with. I've outlined it in the code.

Thanks for helping

#include <iostream> #include <cstdlib> #include <cstring> #include <fstream>  using namespace std;  int main() {     char x;      cout << "Would you like to write to a file?" << endl;     cin >> x;     if (x == 'y' || x == 'Y')     {         char y[3000];         cout << "What would you like to write." << endl;         cin >> y;         ofstream file;         file.open("Characters.txt");         file << strlen(y) << " Characters." << endl;         file << endl;         file << y; // <-- HERE How do i write the full line instead of one word          file.close();           cout << "Done. \a" << endl;     }     else     {         cout << "K, Bye." << endl;     } } 
like image 503
FuzionSki Avatar asked Mar 28 '11 07:03

FuzionSki


People also ask

How do you read an entire line in C++?

The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.

Does CIN read a line?

Using cin. You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.

What function do you call to read a string from CIN C++?

We can use the function getline, which takes two parameters. The first is the name of the input stream, and the second is the name of the string variable into which the string is to be read. For example, getline(cin, newString);

How do you take CIN input?

It accepts input from a standard input device, such as a keyboard, and is linked to stdin, the regular C input source. For reading inputs, the extraction operator(>>) is combined with the object cin. The data is extracted from the object cin, which is entered using the keyboard by the extraction operator.


2 Answers

The code cin >> y; only reads in one word, not the whole line. To get a line, use:

string response; getline(cin, response); 

Then response will contain the contents of the entire line.

like image 80
ybakos Avatar answered Sep 24 '22 15:09

ybakos


#include <iostream> #include <cstdlib> #include <cstring> #include <fstream> #include <string>  int main() {     char write_to_file;     std::cout << "Would you like to write to a file?" << std::endl;     std::cin >> write_to_file;     std::cin >> std::ws;     if (write_to_file == 'y' || write_to_file == 'Y')     {         std::string str;         std::cout << "What would you like to write." << std::endl;          std::getline(std::cin, str);         std::ofstream file;         file.open("Characters.txt");         file << str.size() << " Characters." << std::endl;         file << std::endl;         file << str;          file.close();          std::cout << "Done. \a" << std::endl;     }     else         std::cout << "K, Bye." << std::endl; } 
like image 35
hidayat Avatar answered Sep 22 '22 15:09

hidayat