• We are currently performing site maintenance, parts of civfanatics are currently offline, but will come back online in the coming days (this includes any time you see the message "account suspended"). For more updates please see here.

C++ peoples...

Speedo

Esse Quam Videri
Joined
May 29, 2003
Messages
4,891
Location
NC USA
I'm at a bit of a loss about the problem with this. I'm not getting any compiler errors or warnings, but the program skips over the first input
cin.getline(name, 250);
like it's not even there.

Ideas?

Code:
void commandList(string fileName, ofstream &profile) {
	char name[250], phrase[250];
	cout << "Enter the name for this command:\n";
	cin.getline(name, 250);
	cout << "Enter the phrase which will trigger this command:\n";
	cin.getline(phrase, 250);
	
	profile.open(fileName.c_str( ), ios::app);
		profile << "<command name=\"" << name << "\" phrase=\"" << phrase << "\">\n";
	profile.close( );
}
 
Try adding

Code:
cin.ignore();

right before the first getline() and see what happens. It could be that you have a '\n' still in the buffer from somewhere else in the program like Souron said.
 
cin.ignore() did it. Hrm. Of course now I hit something else.

Same basic problem. When the !cin is triggered (pause is an int) it sticks in the loop spitting out the two lines and seeming to ignore the cin. Clearing the input buffer and the ignore function didn't do anything for this. :crazyeye:

Code:
if (menuCheck(menu)) {
		do {
			cout << "Pause in milliseconds: ";
			cin >> pause;
			if (!cin)
				cout << "Enter an integer value...\n";
		} while (!cin);
	}
 
When the failbit in cin is triggered by entering a non-integer value, you have to reset it. Try adding the code in blue:

Code:
if (menuCheck(menu)) 
{
     do 
     {
          [B][COLOR="Blue"]cin.clear();[/COLOR][/B]
          cout << "Pause in milliseconds: ";
          cin >> pause;
          if (!cin)
               cout << "Enter an integer value...\n";
     } while (!cin);
}
 
Arg.

If you do that, then the failbit won't trigger the while condition though. I don't think that's the problem though, since rewritten like this it does exactly the same thing. "fail" being a boolean.

Code:
if (menuCheck(menu)) {
  do {
    cout << "Delay in milliseconds: ";
    cin >> delay;
    if (!cin) {
      cout << "Enter an integer value...\n";
      fail = 1;
    }
    else
      fail = 0;
    cin.clear( );
  } while (fail);
}
 
I'm sorry, I made a mistake. Look at the code in my previous post again, I changed the order...

But if you did it the way it is in your last post you would have to reset the boolean to 0 at the beginning or it start an infinite loop.

Code:
if (menuCheck(menu)) {
  do {
    [COLOR="RoyalBlue"][B]fail = 0;[/B][/COLOR]
    cout << "Delay in milliseconds: ";
    cin >> delay;
    if (!cin) {
      cout << "Enter an integer value...\n";
      fail = 1;
    }
    cin.clear( );
  } while (fail);
}

But, since you're saying this doesn't solve it anyway, can we see more of your code (or maybe the whole code)?
 
Yeah, I realized that after a second ;)

I meant to add that I tried clearing it at the beginning of the loop, doesn't do any good.
 
hmmm that's wierd. I thought that would have worked.
 
Okay I think I have it - try this:

Code:
if (menuCheck(menu)) 
{
     do 
     {
          [COLOR="Blue"][B]
          cin.clear();
          cin.ignore();[/B][/COLOR]
          cout << "Pause in milliseconds: ";
          cin >> pause;
          if (!cin)
               cout << "Enter an integer value...\n";
     } while (!cin);
}

You need to clear the buffer if you want to go through the loop again or else cin will just accept the '\n' generated by pressing enter and move on.
 
The amazing thing is I managed to answer the question correctly without ever having worked with C++.

But that's only becouse I've had simmilar problems with C and clearing the input buffer was the solution.
 
Little different problem.

I was using Bloodshed Dev C++, and would use srand(time(0)); to set the seed for the rand function. Now I've been using the MS Visual C++ Express compiler, which doesn't recognize the time( ) function. I haven't found a way in the help to implement it, anybody have an idea?
 
Maybe there's a different function for obtaining the time in MSV C++? I don't really know because all I use is Dev-C++ (Visual is a waste of time and money everything you can do in Visual in Dev and in the process become a better programmer).
 
Why pay for it when they give it away? ;)

I think I just may keep using the DevCpp compiler with the MSV editor, 'cause I'm lazy and don't want to mess with learning the MSV compiler. I really can't stand DevCpp's editor though but don't feel like paying for UltraEdit.
 
Speedo said:
Why pay for it when they give it away? ;)

I think I just may keep using the DevCpp compiler with the MSV editor, 'cause I'm lazy and don't want to mess with learning the MSV compiler. I really can't stand DevCpp's editor though but don't feel like paying for UltraEdit.

I love Dev-C++'s editor!
 
Back
Top Bottom