Well, once again, hello. I'm trying to figure out why when i use 2 cin.getlines after eachother it skips one of them and I cant use it. Take a look if you will:

void Join() 
{
char* Username = new char[255];
char* Password = new char[255];

    ofstream DataBase("DataBase.txt", ios::app);
    cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
    cout << "Username: ";
    cin.getline(Username, 255);
    DataBase << Username << "\n";
    cout << "\nPassword: ";
     cin.getline(Password, 255);
    DataBase << Password << "\n\n";
    cout << "\nSaved To Database!\n";
    DataBase.close();
    
    delete [] Username;
    delete [] Password;
}

for some reason when I run the program and goto this function it will skip the first cin.getline and only let me type for the cin.getline that gets the Password. It's wierd because I ran a simple program similar to it, and it works fine:

#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main (int argc, char* argv[]) {
    char* string1 = new char[255];
    char* string2 = new char[255];
    
    cout << "String1: ";
    cin.getline(string1, 255);
    
    cout << "String2: ";
    cin.getline(string2, 255);
    
    cout << "\nString1 = " << string1 << endl;
    cout << "String2 = " << string2 << endl;
    
    delete[] string1;
    delete[] string2;
    
    getch();
    return 0;
}

This code doesnt skip any cin.getline()'s for some reason, could it not work in the other one because of the file i/o? Maybe its making it act wierd? Any help is appreciated, thanks in advance.

Before the call to Join(), is there another function that leaves unread input (such as a trailing newline) in cin ?

I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

char* Username = new char[255];
char* Password = new char[255];
char* User = new char[255];

void Join() 
{
    ofstream DataBase("DataBase.txt", ios::app);
    cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
    cout << "Username: ";
    cin.getline(Username, 255);
    DataBase << Username << "\n";
    cout << "\nPassword: ";
     cin.getline(Password, 255);
    DataBase << Password << "\n\n";
    cout << "\nSaved To Database!\n";
    DataBase.close();
    
    delete [] Username;
    delete [] Password;
}

void Login() {
     
     ifstream DataBase("DataBase.txt");
     cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
     cout << "Username: ";
     cin >> User;
     DataBase >> Username;
     if (Username==User) cout << "\nUsername Correct!";
     else cout << "\nUsername Incorrect!";
              
                        
     
     delete [] User;
     delete [] Username;
     delete [] Password;
     }
     
     void About() {
          }
      

int main(int argc, char *argv[])
{
    
    int option;
    
  cout << "DataBase v1.00 BETA!\n\n";
  cout << "1.Login\n";
  cout << "2.Sign-Up\n";
  cout << "3.About\n";
  cout << "4.Exit\n\n";
  cout << "Option(1-4): ";
  cin >> option;
  
  switch (option) {
         case 1:
              Login();
              break;
         case 2:
              Join();
              break;
         case 3:
              About();
              break;
         case 4:
              break;
         default:
                 cout << "Not An Option!";
                 break;
                 }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for replying.

How about in you use Login() where you use cin instead of getline?

cin >> User;

You'll have to use cin.ignore() to clean the input buffer, read this thread :)

I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

char* Username = new char[255];
char* Password = new char[255];
char* User = new char[255];

void Join() 
{
    ofstream DataBase("DataBase.txt", ios::app);
    cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
    cout << "Username: ";
    cin.getline(Username, 255);
    DataBase << Username << "\n";
    cout << "\nPassword: ";
     cin.getline(Password, 255);
    DataBase << Password << "\n\n";
    cout << "\nSaved To Database!\n";
    DataBase.close();
    
    delete [] Username;
    delete [] Password;
}

void Login() {
     
     ifstream DataBase("DataBase.txt");
     cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
     cout << "Username: ";
     cin >> User;
     DataBase >> Username;
     if (Username==User) cout << "\nUsername Correct!";
     else cout << "\nUsername Incorrect!";
              
                        
     
     delete [] User;
     delete [] Username;
     delete [] Password;
     }
     
     void About() {
          }
      

int main(int argc, char *argv[])
{
    
    int option;
    
  cout << "DataBase v1.00 BETA!\n\n";
  cout << "1.Login\n";
  cout << "2.Sign-Up\n";
  cout << "3.About\n";
  cout << "4.Exit\n\n";
  cout << "Option(1-4): ";
  cin >> option;
  
  switch (option) {
         case 1:
              Login();
              break;
         case 2:
              Join();
              break;
         case 3:
              About();
              break;
         case 4:
              break;
         default:
                 cout << "Not An Option!";
                 break;
                 }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for replying.

You could be facing the problem at either of these lines :

cin >> User;
cin >> option;

Most probably, they would leave a newline behind, in the input stream. Hence, the first getline() used after cin would just 'eat' that newline, and wont take any input.

You should better keep habit of using cin.ignore() to flush the input stream before using getline ( or even cin; i had faced this problem in that too..tho not so sure :))

cin.ignore();
cin.getline();

cin.ignore() also has overloaded versions. You should check them out. :)

Thanks i'll try cin.ignore(), and by the way, i don't think i'd want to just use cin, because when i do it doesnt allow spaces...

Thanks guys, it worked like a charm.. ^_^

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.