1

Im trying to teach myself C++ and this is a program I am working on at the moment but I'm having a problem with the password loop. Even why I type in the correct username and password it just keeps asking me for the username again and again instead of going to the void menu funciton

#include <iostream> #include <stdio.h> #include <string> #include <fstream> using namespace std; void menu(int argc, const char * argv[], char text[2000]); string encryptDecrypt(string toEncrypt) { char key = 'K'; //Any char will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key; return output; } void menu(int argc, const char * argv[], char text[2000]) { system("color 0A"); //ifstream my_input_file; ofstream my_output_file; my_output_file.open("output_data.txt"); cout<<"Please enter your text: "; //cin>>text; //cin.ignore(); //cin.getline(text, sizeof text); cin.getline(text,2000); //cout<<"You entered: "<< text <<"\n"; //string encrypted = encryptDecrypt("kylewbanks.com"); string encrypted = encryptDecrypt(text); cout << "Encrypted:" << encrypted << "\n"; string decrypted = encryptDecrypt(encrypted); cout << "Decrypted:" << decrypted << "\n"; my_output_file << "Your encrypted text is: " << encrypted; my_output_file.close(); cin.get(); } int main() { string username; string password; do { cout << "username: "; getline(std::cin, username); if (username == "John") { std::cout << "password: "; getline(std::cin, password); if (password != "1234") { cout << "invalid password. try again." << std::endl; } else if (password == "1234"){ void menu(int argc, const char * argv[], char text[2000]); } } else { std::cout << "invalid username. try again." << std::endl; } } while (password != "1234"); return 1; } 
9
  • 5
    void menu(int argc, const char * argv[], char text[2000]); does not call a function. Commented Aug 26, 2014 at 14:53
  • 1
    I don't think function calls work the way you think they work. Commented Aug 26, 2014 at 14:53
  • 2
    Is the password cherry or 1234? Commented Aug 26, 2014 at 14:54
  • Sorry about that the password is 1234. I fixed that, now it just closes the program when I get the password and username correct. Commented Aug 26, 2014 at 15:00
  • @Sean if you fixed that, provide updated code. Commented Aug 26, 2014 at 15:01

3 Answers 3

4

If the password is equal to "cherry" then you do nothing that is you simply locally declare function menu

else if (password == "cherry"){ void menu(int argc, const char * argv[], char text[2000]); } 

This statement

 void menu(int argc, const char * argv[], char text[2000]); 

is not a function call. It is a function declaration.

However if you will enter "1234" then the loop ends because its condition is

} while (password != "1234"); 

EDIT: I see you updated your post and substituted statement

else if (password == "cherry"){ 

for

else if (password == "1234"){ 

Nevertheless in essence nothing was changed. After the new statement there is still a function declaration.

Sign up to request clarification or add additional context in comments.

Comments

1

First, don't use using namespace std;. Just don't. It's bad practice, regardless if the others don't point it out to you. Second, seriously read up on how functions are called, made, and fed arguments, especially if you want to copy someone else's code. Even if you correct your loop, if you don't know which arguments to pass to your given menu function, it's all for zilch.

Now that that's out of the way, see the following dumbed down version of the code.

#include <iostream> #include <string> void menu() { std::cout << "I am inside the menu!" << std::endl; } int main() { std::string username; std::string password; std::string access = "cherry"; do { std::cout << "Username: "; std::getline(std::cin, username); if (username != "John") { std::cout << "Invalid username. Please try again.\n"; continue; } std::cout << "Password: "; std::getline(std::cin, password); if (password != access){ std::cout << "Incorrect password. Resetting access procedure.\n"; continue; } } while (password != access); menu(); std::cin.get(); return 1; } 

See how I don't need to put menu inside the do-while loop. Granted, this means that as long as the username and password are incorrect, it's not going to reach that part. How to exit it is your exercise.

Screenshot:

enter image description here

Hope this helps.

Comments

1
#include <iostream> #include <stdio.h> #include <string> #include <fstream> using namespace std; //the point of using namespace std is to avoid writing std:: every time string encryptDecrypt(string toEncrypt) { char key = 'K'; //Any char will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key; return output; } // better use the string class rather than an array of chars. Made it a local variable. void menu(int argc, const char * argv[]) { system("color 0A"); //ifstream my_input_file; ofstream my_output_file; my_output_file.open("output_data.txt"); cout << "Please enter your text: "; string text; cin >> text; string encrypted = encryptDecrypt(text); cout << "Encrypted:" << encrypted << "\n"; string decrypted = encryptDecrypt(encrypted); cout << "Decrypted:" << decrypted << "\n"; my_output_file << "Your encrypted text is: " << encrypted; my_output_file.close(); } int main(int argc, const char * argv[]) { string username; string password; //changed the do while to be while(password != 1234) { cout << "username: "; cin >> username; if (username == "John") { cout << "password: "; cin >> password; //swapped comparisons to avoid 1 comparison and make the code clearer if (password == "cherry") menu(argc, argv); //it didn't make sense giving text as an argument //because you then do a cin >> text in menu. else cout << "invalid password. try again." << endl; } else cout << "invalid username. try again." << endl; } 

4 Comments

Code without explanations doesn't help!
@anatolyg I just have updated and commented most important changes. Sean, please, take a look to last changes.
This code might work, but it's not good code. Kindly consider editing it and providing explanations and doing correct revisions. Working answer is not the same as a good answer. Your inconsistent use of std::endl and endl alone is bad practice. Would give a +1 if it was cleaned up.
@Nanashi Sorry, the std::endl was simply a forgotten std:: to clean up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.