Quack
Quack

Reputation: 133

Stopping If statement from running entire code at once

I'm creating a password system for an encryption program.

For this i moved my main chunk of code inside an if statement, however now, instead of reading the code line by line, it simply prints it all at once. (e.g. when it comes to std::cout << "Enter here: "; std::getline(std::cin, userin); the user cant input anything, it just prints the next line and keeps going.

int main(){

    std::string userin;

    std::string pass;

    int attempts = 3;

    std::cout << "            ENCRYPTER" << std::endl;
    std::cout << " " << std::endl;



    while (attempts > 0){

    std::cout << "Please Enter the Password" << std::endl;

    if(attempts > 1){
    std::cout << "You have " << attempts << " attempts remaining." << std::endl;
    } 
    
    else {
        std::cout << "You have " << attempts << " attempt remaining." << std::endl;

    }

    std::cin >> pass;
    std::cout << " " << std::endl;

    if(pass == "12345"){


    std::cout << greetingFunc() << std::endl;
    std::cout << " " << std::endl;
    std::cout << "NOTE: Use only LETTERS. No numbers or grammar." << std::endl;
    std::cout << " " << std::endl;
    std::cout << "Please enter the message you wish to encrypt." << std::endl;
    std::cout << "Enter here: "; std::getline(std::cin, userin);
    std::cout << " " << std::endl;
    std::cout << "Encrypted Message: " << encryptFunc(userin) << std::endl;
    std::cout << " " << std::endl;
    std::cout << "Highlight it and right click to copy." << std::endl;
    system("pause");

    } else {

        std::cout << "Incorrect password. Please try again." << std::endl;
        attempts--;
    }

    }

    return 0;

}

Here is what is received in the console after the password is correctly entered.

Welcome to the Code Encrypter

NOTE: Use only LETTERS. No numbers or grammar.

Please enter the message you wish to encrypt.
Enter here:
Encrypted Message:  

Highlight it and right click to copy.
Press any key to continue . . . 

See how it just prints everything without running the functions or allowing the user to input their data.

How can i get it to read each line and stop to let the user input the info, and then proceed to call the encryption function? Thanks.

Upvotes: 0

Views: 100

Answers (3)

h0tst3w
h0tst3w

Reputation: 114

It looks like you're having issues with the input buffer. This is a very common issue when running command line/terminal programs.

When the cin >> pass is called, the stream buffer may have inputs remaining (namely the '\n' from pressing enter). You can do a few things, but the easiest thing is to call cin.get() after the cin >> pass.

You could also try creating a helper function or parsing the input using a while loop.

Upvotes: 0

ArashV
ArashV

Reputation: 84

Just add std::getchar(); after std::cin >> pass

The problem is that when you enter pass and click enter, this enter keyboard will be considered as your next input. std::getchar(); receives the effect of enter keyboard. Now you can enter userin.

Note: Use std::getchar() only after you get a string by std::cin. If you use std::getline(std::cin, pass); instead of std::cin >> pass;, you don't have to worry about the final '\n' because getline() doesn't let this character interrupt the next input.

Upvotes: 1

kzlca
kzlca

Reputation: 441

Using stream operators with getline(cin, variable) is always a problem because some data remains in the input buffer. So you should remove everything still in there then read your input. I.e.

while(getline(cin, userin))
    if (userin != "")
        break;

so this should works:

int main(){

    std::string userin;

    std::string pass;

    int attempts = 3;

    std::cout << "            ENCRYPTER" << std::endl;
    std::cout << " " << std::endl;



    while (attempts > 0){

    std::cout << "Please Enter the Password" << std::endl;

    if(attempts > 1){
    std::cout << "You have " << attempts << " attempts remaining." << std::endl;
    } 
    
    else {
        std::cout << "You have " << attempts << " attempt remaining." << std::endl;

    }

    std::cin >> pass;
    std::cout << " " << std::endl;

    if(pass == "12345"){


    std::cout << greetingFunc() << std::endl;
    std::cout << " " << std::endl;
    std::cout << "NOTE: Use only LETTERS. No numbers or grammar." << std::endl;
    std::cout << " " << std::endl;
    std::cout << "Please enter the message you wish to encrypt." << std::endl;
    std::cout << "Enter here: "; 
    while (std::getline(std::cin, userin))
        if (userin != "")
            break;
    std::cout << " " << std::endl;
    std::cout << "Encrypted Message: " << encryptFunc(userin) << std::endl;
    std::cout << " " << std::endl;
    std::cout << "Highlight it and right click to copy." << std::endl;
    system("pause");

    } else {

        std::cout << "Incorrect password. Please try again." << std::endl;
        attempts--;
    }

    }

    return 0;

}

Upvotes: 0

Related Questions