Basic C++ help!

Discussion in 'Mac Programming' started by DeathChill, Jul 27, 2005.

  1. DeathChill macrumors 68000

    Jul 15, 2005
    I'm no C++ developer but I was wondering why this section of code is NOT executing properly:
    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.
  2. rinseout macrumors regular

    Jan 20, 2004
    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.

    #include <iostream>
    #include <fstream>
    using namespace std;
    void Login();
    int main() {
      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();
        Settings.getline(username, 50);
        while (strcmp(login_un, username) !=0){
          cout<<"Login: ";
          cin.getline(login_un, 50);
          if( strcmp(login_un, username) == 0 ) break;
    	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;
    	cout << "Incorrect password, please try again." << endl;
        // CommandLine(command,version,curr_dir,cmd,wrt=1);
        cout << "Will execute CommandLine." << std::endl;
        cout << "Will execute CreateUser." << endl;
        // CreateUser(user_un,user_pw,user_drive,size);
  3. makeme macrumors member

    Jul 16, 2005
    Make it easy on yourself.

    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.

    #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;
            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.

Share This Page