cin.ignore() not working

Hello.
I am attempting to write a little program that will allow a password to be printed out if you know the first password (the date) here's the code i have.
#include <iostream>
#include <cstring>
#include <time.h>
using namespace std;

int main(){
char password[8];
char guess[8];
cin >> guess[8];
cin.ignore(8,'\n');
_strdate( password);
if(strcmp (password,guess) != 0){
cout << "The password is aucn3siv5ntos93mnt768dnshc8823";
}
cin.get();
return 0;
}
However for reasons i cannot figure out the cin.ignore doesn't seem to be working, Anyone know what might be causing this?

Thanks.

EDIT: Got it working with the italicized change.
Last edited on
Couldn't you just use cin.ignore() by itself?
It is because you are mixxing formatted and unformatted input.
http://www.cplusplus.com/forum/articles/6046/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <ctime>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
  {
  // Get time as string YYYYMMDD, eg: 20090117
  string password( 8 );
  strftime(
    const_cast <char*> (password.c_str()),
    password.length(),
    "%Y%m%d",
    localtime( time( NULL ) )
    );

  // Can the user access the stored passwords?
  string guess;
  cout << "Password? " << flush;
  getline( cin, guess );

  if (guess == password)
    {
    // Print the password(s)
    cout << "Password1 = parangaricutirimicuaro" << endl;
    cout << "Password2 = antidisestablishmentarianism" << endl;
    }
  else
    {
    // Complain to the user
    cerr << "Incorrect password." << endl;
    }

  #if defined(__WIN32__)
    // Only PAUSE on Windows...
    cout << "Press ENTER to continue..." << flush;
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  #endif

  // Return success or failure
  return (guess != password);
  }

Hope this helps.
Last edited on
Topic archived. No new replies allowed.