some strange getline error

K.I.L.E.R

Retarded moron
Veteran
I'm making a very simple program but unfortunately getline doesn't work.
Code:
int main(void)
{
	int mnuOpt=0;
	char name[6];

	mnuOpt = menu(mnuOpt);

	switch(mnuOpt)
	{
	case 1:
		cin.getline (name, 6);
		break;

	case 2:
		return 0;
	}

	cout << name << endl;
	
	system("pause");
	return 0;
}

The menu just send back the value of mnuOpt and does nothing with any other variable.

When I say getline doesn't work, I mean that it doesn't work. It won't let me input a damn thing.
It looks just like the compiler skips it completely.

I fixed it just now by placing a cin.ignore(num, '/n') and my case 1 looks like this:

Code:
	case 1:
		cin.ignore(100,'\n');
		cin.getline (name, 30);
		break;

I assume the buffer is full and that is why it couldn't get anymore input?
Right? Wrong?

I tried it with VC++ 6 and Dev C++ both have the same result.

Thanks
 
Do you mean that your menu() function simply returns the value you pass into it? mnuOpt is initialised to 0, but there is no 0 case in your switch.
 
Yer, I assume that it won't matter what you initialise it to because the menu function returns the value that will be used in the switch statement and I used a while loop to make sure only 1 or 2 can be pressed if you are to break out of the loop.
 
Myrmecophagavir said:
So why do you pass mnuOpt into menu()?

Damn. You got me there. Bad coding practice on my part. I was tought to init all variables at the start of main.
 
Yes, you should make sure you initialise your variables to sensible values which can prevent some bugs. But that's got nothing to do with passing it into your function as a parameter?
 
Yeh, I know.

My teacher encourages everyone to pass variables a lot and that's where I picked up the habbit.
I was even told off for not passing variables to other functions during class.

So I assumed it was proper practice to do so.

Thanks
 
Your teacher was probably talking about global variables - often better to pass what information you need as parameters rather than having global variables all over the place.

No need to pass as many variables as you can needlessly though ;)
 
Back
Top