0

Compiler-Code::Blocks Operating System(OS)-Windows

I wrote a very simple password masking program unlike the complicated ones on the internet.When i build and run my code it displays "Enter password" but does not allow me to input anything.

By password masking i mean that when i enter password then it is displayed as **** while being input just as it happens in E-mail.(one * for each character)

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
    string pass;

    cout << "Enter password";

    for (int i=0; i<100; i++)
    {
        char ch = getch();

        if (ch == 13)
            break;

        if (ch == 8)
        {
            if (pass.size())
            {
                cout << "\b \b";
                pass.pop_back();
            }
        }
        else
        {
            cout << "*";
            pass += ch;
        }
    }

    cout <<  "pass = " << pass << '\n';
}
suyashsingh234
  • 189
  • 1
  • 2
  • 12
  • No error using TDM-GCC 5.1.0 on Windows 7, standard Command Prompt. (What compiler/version are you using?) You might want to try `cin.sync_with_stdio(true)` and see if that makes a difference. (Conio stuff requires extra-weird wonkiness to work properly behind the scenes.) – Dúthomhas Nov 13 '17 at 05:29
  • 3
    *I wrote a very simple password masking program unlike the complicated ones on the internet.* -- I guess you see now why the ones on the internet are "complicated". Console I/O is not as straightforward as you thought it was. – PaulMcKenzie Nov 13 '17 at 05:43
  • Code::Blocks **is not** a compiler. It's an editor. – tambre Nov 13 '17 at 05:53
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Nov 13 '17 at 05:53
  • Additionally, please state your expected behaviour and the question. – tambre Nov 13 '17 at 05:55
  • @tambre OP has plainly stated expected behavior; I am also a bit dismayed that people always gripe about the lack of question marks. Further, I don't see how picking only on using namespace std helps OP with his actual problem. – Dúthomhas Nov 13 '17 at 06:15
  • @Duthomhas used "cin.sync_with_stdio(true)"(after string pass;) but still not working. – suyashsingh234 Nov 13 '17 at 15:19
  • @Duthomhas windows 10 and codeblocks 16.01. – suyashsingh234 Nov 13 '17 at 15:21
  • @suyashsingh234 -- Well take one of the "complicated" ones you mentioned, compile it, and run a program. Does the masking work? If it does, then investigate what those implementations are doing and what you're not doing. Otherwise all you're doing is hoping something works by throwing stuff on a wall and seeing what sticks. – PaulMcKenzie Nov 13 '17 at 16:20
  • @PaulMcKenzie But i am in school and the examiner would grill me for using complicated methods and give me 0. – suyashsingh234 Nov 13 '17 at 16:26
  • @suyashsingh234 -- No. What I'm asking you to do is take the complicated method, sees that it works and *do reasearch* on why it works and yours doesn't. Then fill in the gaps with your "simple" code with what's missing. Seriously, this is the first thing you should have done. Things like turning off the echo, replacing a typed in character with stars, etc. are things that you need to research how to do for you OS, and not come up with guesses. – PaulMcKenzie Nov 13 '17 at 16:28
  • i did it(before this question) but the complicated ones are completely different from what i do. :( – suyashsingh234 Nov 13 '17 at 16:31
  • @suyashsingh234 -- *the complicated ones are completely different from what i do.* - **Which is exactly my point** -- Thanks. Your method may not work at all, doomed for failure, and you may end up wasting your time on something that cannot work the way you wrote it. I would go back the teacher and get a solid answer -- will this approach work, yes or no, or am I wasting my time and you're just playing games. Again, there is a reason why those other approaches are "complicated". If it were so easy, why wouldn't those other approaches also make it easy? – PaulMcKenzie Nov 13 '17 at 16:33
  • code works now.What does pass.size() do here? – suyashsingh234 Nov 16 '17 at 13:18

1 Answers1

0

Duh. I found the simplest solution ever. settings>compiler>reset defaults :|

suyashsingh234
  • 189
  • 1
  • 2
  • 12