35

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;
    }
}
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
FuzionSki
  • 451
  • 1
  • 6
  • 8
  • 3
    You might want to make your title better reflect your question. Also, you should clarify your question, it's not really clear what you're asking. – Andrew Marshall Mar 28 '11 at 07:28
  • 2
    The problem is that `cin >> y` is only storing the first word of the line the user types, the asker wants to know how to store the entire line in y, such that `file << y` writes the full line to the file. – Bjarke Freund-Hansen Mar 28 '11 at 07:32

4 Answers4

102

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.

ybakos
  • 8,152
  • 7
  • 46
  • 74
10
#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;
}
hidayat
  • 9,493
  • 13
  • 51
  • 66
  • 5
    The important part being: `getline(std::cin, y);` instead of `cin >> y;`. – Bjarke Freund-Hansen Mar 28 '11 at 07:37
  • 1
    you also need the cin >> ws; else the getline will only read a new line – hidayat Mar 28 '11 at 07:39
  • 12
    When writing code as an answer to a question, please do *never* use `using namespace std;` (you should actually almost never do this, but especially not in posts that are potentially read by total beginners, who then pick it up and think it is OK). Code posted in answers is supposed to be setting a good example. – Björn Pollex Mar 28 '11 at 07:41
4
string str;
getline(cin, str);
cin >> ws;

You can use getline function to read the whole line instead of reading word by word. And The cin>>ws is there to skip white spaces. And you find some detail about it here : http://en.cppreference.com/w/cpp/io/manip/ws

mtyong
  • 121
  • 1
  • 3
  • 13
0

Cin only gets input for 1 word. In order to get input for a sentence, you need to use a getLine(cin, y) in order to get a sentence of input. You can also make multiple variables for each word and then tyou use cin to get the input like this cin >> response1, response2, response3, response3, etc;.

Optimus
  • 24
  • 8