Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DeathChill

macrumors 68000
Original poster
Jul 15, 2005
1,663
90
I'm no C++ developer but I was wondering why this section of code is NOT executing properly:
int Login(char login_un[50], char login_pw[50], int user_choice, char username[50], char password[50]){
ifstream Settings("KCLP.cfg");
cout<<"Would you like to login or create a new account?"<<endl<<"1. Login\n"<<"2. Create Account"<<endl;
cin>>user_choice;
if(user_choice==1){
cout<<"Login:";
cin.getline (login_un, 50);
cin>>username;
Settings>>username;
while (strcmp(login_un, username) !=0){
cout<<"You've mistyped your login name, please retry.\nLogin:";
cin>>login_un;
}
cout<<"Password:";
cin.getline (login_pw, 50);
cin>>username;
Settings>>password;
while (strcmp(login_pw, password) !=0){
cout<<"You've mistyped your password, please retry.\nPassword:";
cin>>login_pw;
}
CommandLine(command,version,curr_dir,cmd,wrt=1);
}
else{
CreateUser(user_un,user_pw,user_drive,size);
}
}

What happens is the user types their username and it says wrong login (regardless of whether or not it is right). Once they type it again (rightly, of course) it works, and the same goes for password. The only way I can get it to not jump to the password section is to put the cin>>username underneath the cin.getline(). I don't understand why it is executing regardless of what the user types the first time o.o I'm no C++ developer so don't mock my horrible code.
 

rinseout

macrumors regular
Jan 20, 2004
160
0
The trouble is because the carriage return from the console input is "still there" when you call getline() after using operator>>. You need to eject it from the stream before you start using getline(). This will do what you want it to, but I am not suggesting that this is well-written.

Code:
#include <iostream>
#include <fstream>

using namespace std;

void Login();

int main() {
  Login();
  return 0;
}

void Login() {
  char login_un[50], login_pw[50], username[50], password[50]; 
  int user_choice;
  ifstream Settings("KCLP.cfg");
  cout<<"Would you like to login or create a new account?"<< endl
      <<"1. Login" << endl
      <<"2. Create Account"<< endl;
  cin >> user_choice;

  if(cin.peek() == '\n') cin.get();

  if(user_choice==1){
    Settings.getline(username, 50);
    while (strcmp(login_un, username) !=0){
      cout<<"Login: ";
      cin.getline(login_un, 50);
      if( strcmp(login_un, username) == 0 ) break;
      else
	cout<<"You've mistyped your login name, please retry." << endl;
    }

    Settings.getline(password, 50);
    while (strcmp(login_pw, password) !=0){
      cout << "Password: ";
      cin.getline( login_pw, 50 );
      if( strcmp(login_pw, password) ==0 ) break;
      else
	cout << "Incorrect password, please try again." << endl;
    }

    // CommandLine(command,version,curr_dir,cmd,wrt=1);
    cout << "Will execute CommandLine." << std::endl;
  }
  else{
    cout << "Will execute CreateUser." << endl;
    // CreateUser(user_un,user_pw,user_drive,size);
  }
  return;
}
 

makeme

macrumors member
Jul 16, 2005
48
0
Make it easy on yourself.

DeathChill said:
I'm no C++ developer so don't mock my horrible code.

Looks at code :eek:

Well, at least you admit it :)


If you're willing, I could teach you some things that could make that and future programs easier to write. For instance, you don't have to mess with char[], you can use string instead. Just include string and use it like this.

Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    cout << "What is your name?" << endl;
    cin >> name;
    
    if (name == "makeme")
        cout << "Hey, that's my name! What a wise guy." << endl;
    else
        cout << "Hello " << name << endl;
    
    return 0;
}

See how much easier that is. You can use it just as easily as you'd use an int only it's a string of chars. You can read in a string from a file the same way just use file >> string to get each word and file.getline(string) to get each line. C++ makes programs like yours easy if you take advantange of the standard streams and containers.

Have fun.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.