I'm having a bit of trouble getting string input while i'm in a function. I was wondering how I could input a really long string (about the size of a paragraph).

cin >> entryA seems to work, but only with 1 word. Any more and it starts looping
getline(cin, entryA); doesn't even wait to get keyboard input

int newday()
{
    time_t rawtime;
    struct tm * timeinfo;
    char filename [80];
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    strftime (filename,80,"%y%m%d.txt",timeinfo);
    ofstream myfile2;

    int strlenA, c;
    string entryA;
    prompts(18);
    cin >> entryA; // this is the line i'm having trouble with

    strlenA = entryA.length();
    char entryB[strlenA];

    for (c=0; c<=strlenA; c++)
    {
        entryB[c] = entryA[c];
    }

    encrypt(entryB);
}

I would appreciate any help, thank you.

Recommended Answers

All 14 Replies

well im not sure if you have to have a cout statement to have a cin statement but cin.getline(entryA, "\n"); should work to get in a lot of input.

well im not sure if you have to have a cout statement to have a cin statement but cin.getline(entryA, "\n"); should work to get in a lot of input.

Thank you for helping! I replaced the cin line with cin.getline(entryA, "\n"); but I got an error:

355|error: no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, const char[2])'|

this is what I called:

#include <iostream.h>
#include <fstream.h> // For files
#include <windows.h> // For pause
#include <string.h>
#include <sstream>
#include <vector>
#include <time.h>

using namespace std;

using std :: cout;
using std :: endl;

is there something i'm missing?

oops sorry my bad. here is a little program for you to see how getline works.

#include <iostream>         //
#include <string>           //  see how i have no .h after the name of the header

using namespace std ;

int main()
{
	string temp;
	cout << "this program is to demonstrate cin.getline()\n";
	cout << "Please enter a sentance: ";
	getline(cin, temp);
	cout << "\nThis is what i recived: " << temp;
	cin.get();
	return 0;
}

also you are using depreciated headers. iostream.h, fstream.h, string.h should all not have the .h after them. this is not to say that you don't need them after your own header files. you just don't need them for these. maybe someone else can explain this better im not sure if im getting it right.

also you are using depreciated headers. iostream.h, fstream.h, string.h should all not have the .h after them. this is not to say that you don't need them after your own header files. you just don't need them for these. maybe someone else can explain this better im not sure if im getting it right.

Thanks, I fixed my headers.

But

getline(cin, entryA);

doesn't work for me when it's in a function for some reason. It's not even stopping to take input from the user...

im not sure what is going on then. could you re post your code you have now. also what compiler are you using?

This is what I have now:

int newday()
{
    time_t rawtime;
    struct tm * timeinfo;
    char filename [80];
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    strftime (filename,80,"%y%m%d.txt",timeinfo);
    ofstream myfile2;

    int strlenA, c;
    string entryA;
    prompts(18);
    getline(cin, entryA);  // this is the line i'm having trouble with

    strlenA = entryA.length();
    char entryB[strlenA];

    for (c=0; c<=strlenA; c++)
    {
        entryB[c] = entryA[c];
    }

    encrypt(entryB);
}

I'm using MinGW with Codeblocks.

what does prompst(18); do?

All it does is get a prompt and cout it to the screen.

in your code do you take in something with cin like cin >> foo; i have changed my sample program so you can see what happens if you take in input with cin >> and then call getline. the problem is there is a carriage return in the buffer so getline will grab that and return nothing.

#include <iostream>
#include <string>

using namespace std ;

int main()
{
	string temp;
	int a;
	cout << "this program is to demonstrate cin.getline()\n";
	cout << "\nplease enter a integer: ";
	cin >> a;
	cout << "Please enter a sentance: ";
	getline(cin, temp);
	cout << "\nThis is what i recived: " << temp;
	cin.get();
	return 0;
}

No, I cut out everything else in the function and it's still not working. I've tried changing the string's name to something else but that doesn't work either.

not in the function but in the code that the function was called from or from before that. even though im putting my getline function is in a different function it is still not working because of my cin >> a call.

#include <iostream>
#include <string>

using namespace std ;

void foo();

int main()
{
	string temp;
	int a;
	cout << "this program is to demonstrate cin.getline()\n";
	cout << "\nplease enter a integer: ";
	cin >> a;
	foo();
	cin.get();
	return 0;
}

void foo()
{
	string temp;
	cout << "Please enter a sentence: ";
	getline(cin, temp);
	cout << "\nThis is what i received: " << temp;
}

Yes, I have a switch/case before it and I had to input a number for the switch/case to work with... if it is interfering with getline, how would I prevent it from interfering?

// stuff before here
            do
            {
                wait();
                clean();
                prompts(16);
                prompts(17);
                prompts(2);
                cin >> jourB;
                journalmenu (jourB, useridB);
            } while(jourB <= 2);
// stuff after here

// skips to here

int journalmenu (int jourB, int useridB)
{
    clean ();

    switch( jourB )
    {
        case 1:
        newday();
        return jourB;
        break;
// and the rest of my program after this

after your cin >> jourB call cin.get(). that should get rid of your problem.

#include <iostream>
#include <string>

using namespace std ;

void foo();

int main()
{
	string temp;
	int a;
	cout << "this program is to demonstrate cin.getline()\n";
	cout << "\nplease enter a integer: ";
	cin >> a;
	cin.get();  //<-  gets rid of the newline in the buffer
	foo();
	cin.get();
	return 0;
}

void foo()
{
	string temp;
	cout << "Please enter a sentence: ";
	getline(cin, temp);
	cout << "\nThis is what i received: " << temp;
}

It works! Thank you very much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.