I am having trouble using cin.getline and classes. Here is the code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

class RememberPlans
{
	public:
		string date;
		string subject;
		string resources;
		string description;
};
RememberPlans nds;

// Function Prototypes
bool enterPlans(RememberPlans& nds);
void exportData(RememberPlans& nds);
void displayData(RememberPlans& nds);
int main()
{
	
	const int MAX = 10;
	RememberPlans nds[MAX];
	int index = 0;
	while (enterPlans(nds[index]) && index < MAX)
	{
		index++;
	}

	system("CLS");
	cout << "Here are the lesson plans you entered:\n";
	// Output the entries
	for (int i = 0; i < index; i++)
	{
		cout.width(3);
		displayData(nds[i]);
	}

	cout << "Time to export plans!";
    system("PAUSE");
	
	// Output the entries
	for (int i = 0; i < index; i++)
	{
		cout.width(3);
		exportData(nds[i]);
	}
system("PAUSE");	
return 0;

}

bool enterPlans(RememberPlans& nds)
{
	cout << "This program creates lesson plan documents.\nType export to view and export the the plans.\n\n";
	cout << "Enter subject: ";
	cin.getline(nds.subject, 50);

		if(nds.subject == "export" || nds.subject == "EXPORT")
		{
			return false;
		}

		cout << "\nEnter date: ";
		cin.getline(nds.date, 50);

		cout << "\nEnter resources (seperate with '-'): ";
		cin.getline(nds.resources, 250);

		cout << "\nEnter a brief descrition: ";
		cin.getline(nds.description, 500);	
		
		cout << "\n";
		system("CLS");

		return true;
	}
	
void exportData(RememberPlans& nds)
{
	ofstream LessonData ("LessonPlans.txt", std::ios::app);
	LessonData << "Date: ";
	LessonData << nds.date;
	LessonData << "\nSubject: ";
	LessonData << nds.subject;
	LessonData << "\nResources: ";
	LessonData << nds.resources;
	LessonData << "\nDescription: ";
	LessonData << nds.description;
	LessonData << "\n\n";
	LessonData.close();
					 
}

void displayData(RememberPlans& nds)
{
	
	cout << "Date: "
		 << nds.date
	     << "\nSubject: "
	     << nds.subject
		 << "\nResources: "
		 << nds.resources
	     << "\nDescription: "
		 << nds.description
		 << "\n\n";
         
					 
}

here is what my compiler gives me:

Compiling...
cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
LessonPlans.cc
c:\documents and settings\alex muir\my documents\visual studio 2008\projects\lessonplans\lessonplans.cc(60) : error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits
]
c:\documents and settings\alex muir\my documents\visual studio 2008\projects\lessonplans\lessonplans.cc(68) : error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits
]
c:\documents and settings\alex muir\my documents\visual studio 2008\projects\lessonplans\lessonplans.cc(71) : error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits
]
c:\documents and settings\alex muir\my documents\visual studio 2008\projects\lessonplans\lessonplans.cc(74) : error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits
]

Any help greatly appreciated

Recommended Answers

All 3 Replies

There are two forms of getline that concern you in this case. The first is a member function of cin:

char buffer[BUFSIZE];

cin.getline ( buffer, sizeof buffer );

Note that the argument is an array of char, and the maximum number of characters you want to fill it with. The second is a non-member function for the std::string class:

string bufferl

getline ( cin, buffer );

You're mixing those two up by passing a std::string object to the member function, but no such overload for std::string exists. Thus, you need to use the non-member function if you want to continue using the std::string class:

bool enterPlans(RememberPlans& nds)
{
    cout << "This program creates lesson plan documents.\nType export to view and export the the plans.\n\n";
    cout << "Enter subject: ";
    getline(cin, nds.subject);

    if(nds.subject == "export" || nds.subject == "EXPORT")
    {
        return false;
    }

    cout << "\nEnter date: ";
    getline(cin, nds.date);

    cout << "\nEnter resources (seperate with '-'): ";
    getline(cin, nds.resources);

    cout << "\nEnter a brief descrition: ";
    getline(cin, nds.description);	

    cout << "\n";
    system("CLS");

    return true;
}

You are using std::string, you need to be using array of char.

EDIT: Didn't see Narue Posted... My responses never do so well following hers... discard this :p

Thanks guys. I fixed it. :)

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.