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;

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 jencas

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.