i want to add a line at run time in title, but i m unable to do so..
in main() getline(-,-) is working fine but not in the below code, it simply skip it when i put values for Course Name and press enter it go to the next line which is code.. can any one tell me any solution..

operating system Vista 64bit
Visual Studio 2005

case '2':
	{
		string course_name;
		string code;
		string title;
		system("CLS");
		cout << "  Add Subject in course\n\n     ";
		do
		{
			cout << "Please Enter Course Name in Which u want to add Subjects ";
			cin >> course_name;
			ifstream infile;
			string temp = course_name + ".txt";
			
			infile.open(temp.c_str());
			if(infile.fail())
			{
				cout << "No " << course_name << " is available " << endl;
				break;
			}
			_getch();
			cout << "Please Enter Subject Name ";
			getline(cin,title);
			cout << " Title is " << title << endl;
			cout << "Please Enter Subject Code";
			cin >> code;		
			course.addSubject(course_name,code,title);
			cout << "You want to add more subjects ? Y/N ";		
		}
		while(_getch()=='y');	
		cout << "Press any key to go back " << endl;
		_getch();
				welcome_screen();
				newSelection = menu_system();//these lines are the same as
				processChoice1(newSelection);//in main, where we began

	}
  break;

Dani AI

Generated

Most likely cause: the newline left in the input buffer after using cin >> course_name is being consumed by the following getline, so getline returns an empty string. In ’s snippet the code does cin >> course_name; (which stops at whitespace and leaves the '\n') then later calls _getch() and getline. _getch() reads from the console directly and does not remove the newline from the C++ stream, so getline immediately sees that newline and returns an empty line. This is the common “mixing operator>> and getline” pitfall that ’s link was pointing toward.

Three simple fixes (pick one):

  • Read whole lines everywhere (best if names can contain spaces):

    std::string course_name;
    std::getline(std::cin, course_name);
    std::getline(std::cin, title);
  • If you keep operator>> for course_name, clear the rest of that input line before calling getline:

    #include <limits>
    std::cin >> course_name;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::getline(std::cin, title);
  • Use the std::ws manipulator to skip leading whitespace/newline:

    std::getline(std::cin >> std::ws, title);

Notes and cautions:

  • std::cin.clear() only resets stream error flags; it does not remove characters already in the buffer. Use ignore(...) to drop the leftover '\n'.
  • _getch() (conio.h) doesn’t interact with std::cin buffering; avoid inserting it between stream reads if you want predictable behavior.
  • For debugging, int c = std::cin.peek(); will show the next character in the stream (e.g. '\n').

Regarding the earlier thread about initialization: std::string s; is default-constructed to an empty string (no garbage). That differs from uninitialized C-style char arrays. See and ’s exchange for the original discussion.

Recommended Answers

All 8 Replies

>it simply skip it when i put values for Course Name and press enter it go to the next line which is code.. can any one tell me any solution..

Yes, this will help you out :)

Oh, and heres just a freindly tip. When you declare variables, its preferable to initialize them immediatly, or they will contain 'garbage' that might or might not cause problems. You can initialize them to their null state, like for strings, (" "), for integers (0) and so on :)

Oh, and heres just a freindly tip. When you declare variables, its preferable to initialize them immediatly, or they will contain 'garbage' that might or might not cause problems. You can initialize them to their null state, like for strings, (" "), for integers (0) and so on :)

Not agreed about the strings...
Consider the following line of code:
string s; , will it contain garbage?
No.

Not agreed about the strings...
Consider the following line of code:
string s; , will it contain garbage?
No.

It can. If you do not initialize it, it can contain the last value a variable of that type had. Uninitialized variables can go do any memory slot, regardless of whether the slots are vacant or not ;)

It can. If you do not initialize it, it can contain the last value a variable of that type had. Uninitialized variables can go do any memory slot, regardless of whether the slots are vacant or not ;)

Wrong!

If you're talking about C-strings, in other words: character arrays, then it can contain garbage if you don't initialize it.
When talking about C++ strings you don't have to initialize it, your string doesn't contain garbage when you don't initialize it.

Consider the following examples:

// Using a C-string
char cstring[50];
cout << cstring << endl; // garbage in most of the times
// Using a C++ string
string cppstring;
cout << cppstring << endl; // won't print garbage on your screen
Member Avatar for Member #248612

It can. If you do not initialize it, it can contain the last value a variable of that type had. Uninitialized variables can go do any memory slot, regardless of whether the slots are vacant or not ;)

Rubbish!

string s;

calls the standard ctor for variable s, which in turn provides the correct initialization.

Thanks for lot of post regarding my problem... "string will never contain garbage" i m agree with this line...

Wrong!

If you're talking about C-strings, in other words: character arrays, then it can contain garbage if you don't initialize it.
When talking about C++ strings you don't have to initialize it, your string doesn't contain garbage when you don't initialize it.

Consider the following examples:

// Using a C-string
char cstring[50];
cout << cstring << endl; // garbage in most of the times
// Using a C++ string
string cppstring;
cout << cppstring << endl; // won't print garbage on your screen

Oh, all right. Makes sense. I just tested it out right now, and got proved wrong myself. Thanks for correcting me, but i think i'll just keep initializing Strings just the same. Thats my way, and thats how ive been coding ever since i started.
Still, thanks. :)

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.