Reputation: 552
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;
}
Upvotes: 1
Views: 359
Reputation: 4773
#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;
}
Upvotes: 1
Reputation: 14169
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:
Hope this helps.
Upvotes: 1
Reputation: 310930
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.
Upvotes: 4