不一致。单独使用std::endl和endl是一种不好的做法。如果它被清理掉,会得到+1。@Nanashi很抱歉,std::endl只是一个被遗忘的std::来清理。没有解释的代码没有帮助!@anatolyg我刚刚更新并注释了最重要的更改。Sean,请看一下最后的更改。这段代码可能有问题工作,但这不是好代码。请考虑编辑它并提供解释并做正确的修改。,c++,loops,C++,Loops" /> 不一致。单独使用std::endl和endl是一种不好的做法。如果它被清理掉,会得到+1。@Nanashi很抱歉,std::endl只是一个被遗忘的std::来清理。没有解释的代码没有帮助!@anatolyg我刚刚更新并注释了最重要的更改。Sean,请看一下最后的更改。这段代码可能有问题工作,但这不是好代码。请考虑编辑它并提供解释并做正确的修改。,c++,loops,C++,Loops" />

如何留下一个循环 我试图教自己C++,这是我现在正在做的一个程序,但是我的密码循环有问题。即使我输入正确的用户名和密码,它也会一次又一次地问我用户名,而不是去“无效”菜单功能 #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; } #包括 #包括 #包括 #包括 使用名称空间std; 无效菜单(int-argc,const-char*argv[],char-text[2000]); 字符串加密解密(字符串到加密){ char key='K';//任何字符都可以使用 字符串输出=加密; for(int i=0;i与好答案< /代码>不一致。单独使用std::endl和endl是一种不好的做法。如果它被清理掉,会得到+1。@Nanashi很抱歉,std::endl只是一个被遗忘的std::来清理。没有解释的代码没有帮助!@anatolyg我刚刚更新并注释了最重要的更改。Sean,请看一下最后的更改。这段代码可能有问题工作,但这不是好代码。请考虑编辑它并提供解释并做正确的修改。

如何留下一个循环 我试图教自己C++,这是我现在正在做的一个程序,但是我的密码循环有问题。即使我输入正确的用户名和密码,它也会一次又一次地问我用户名,而不是去“无效”菜单功能 #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; } #包括 #包括 #包括 #包括 使用名称空间std; 无效菜单(int-argc,const-char*argv[],char-text[2000]); 字符串加密解密(字符串到加密){ char key='K';//任何字符都可以使用 字符串输出=加密; for(int i=0;i与好答案< /代码>不一致。单独使用std::endl和endl是一种不好的做法。如果它被清理掉,会得到+1。@Nanashi很抱歉,std::endl只是一个被遗忘的std::来清理。没有解释的代码没有帮助!@anatolyg我刚刚更新并注释了最重要的更改。Sean,请看一下最后的更改。这段代码可能有问题工作,但这不是好代码。请考虑编辑它并提供解释并做正确的修改。,c++,loops,C++,Loops,如果密码等于“cherry”,那么您只需在本地声明函数即可 菜单 此声明 void menu(int argc, const char * argv[], char text[2000]); else if (password == "cherry"){ 不是函数调用。它是函数声明 #include <iostream> #include <stdio.h> #include <string> #include <fstream>

如果密码等于“cherry”,那么您只需在本地声明函数即可 菜单

此声明

     void menu(int argc, const char * argv[], char text[2000]);
else if (password == "cherry"){
不是函数调用。它是函数声明

#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;
}
但是,如果输入“1234”,则循环结束,因为其条件为

} while (password != "1234");   
编辑:我看到你更新了你的帖子和替换声明

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


尽管如此,本质上什么都没有改变。在新语句之后仍然有一个函数声明。

如果密码等于“cherry”,那么您什么也不做,只是在本地声明函数
#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;
}
菜单

此声明

     void menu(int argc, const char * argv[], char text[2000]);
else if (password == "cherry"){
不是函数调用。它是函数声明

#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;
}
但是,如果输入“1234”,则循环结束,因为其条件为

} while (password != "1234");   
编辑:我看到你更新了你的帖子和替换声明

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

然而,本质上什么都没有改变。在新语句之后仍然有一个函数声明。

\include
#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;
}
#包括 #包括 #包括 使用namespace std;//使用namespace std的目的是避免每次都写入std:: 字符串加密解密(字符串到加密){ char key='K';//任何字符都可以使用 字符串输出=加密; for(int i=0;i文本; 字符串加密=加密解密(文本); 不能包含 #包括 #包括 #包括 使用namespace std;//使用namespace std的目的是避免每次都写入std:: 字符串加密解密(字符串到加密){ char key='K';//任何字符都可以使用 字符串输出=加密; for(int i=0;i文本; 字符串加密=加密解密(文本);
cout首先,不要使用命名空间std;
使用
。不要这样做。这是一种不好的做法,不管其他人是否向您指出。其次,认真阅读函数是如何调用、生成和提供参数的,特别是如果您想复制其他人的代码。即使您更正了循环,如果您不知道要传递哪些参数,也不会o您给定的
菜单
功能,一切都是免费的

现在,这已经不是问题了,请参阅下面的代码简化版本

#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;
}
#包括
#包括
无效菜单()
{

std::cout首先,不要使用命名空间std;
使用
。不要这样做。这是一种不好的做法,不管其他人是否向您指出。其次,认真阅读函数是如何调用、生成和提供参数的,特别是如果您想复制其他人的代码。即使您更正了循环,如果您不知道哪些参数需要p看看你给定的
菜单
功能,这都是免费的

现在,这已经不是问题了,请参阅下面的代码简化版本

#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;
}
#包括
#包括
无效菜单()
{

std::cout
void菜单(int-argc,const-char*argv[],char-text[2000]);
不调用函数。我认为函数调用的工作方式与您认为的不一样。密码是
cherry
还是
1234
?很抱歉,密码是1234。我已经解决了这个问题,现在当我得到正确的密码和用户名时,它只会关闭程序。@Sean如果你解决了这个问题,请提供更新的代码。
void menu(int argc,const char*argv[],char text[2000]);
不调用函数。我认为函数调用的工作方式与您认为的不一样。密码是
cherry
还是
1234
?很抱歉,密码是1234。我已经解决了这个问题,现在当我得到正确的密码和用户名时,它只会关闭程序。@Sean如果你解决了这个问题,请提供更新的代码。代码没有解释“离子没有帮助!”Antulyg我刚刚更新并评论了最重要的变化。请肖恩,看一下最后的变化。这个代码可能会工作,但这不是好的代码。请考虑编辑它并提供解释并进行正确的修订。<代码>工作答案< /代码>与<代码>好答案< /代码>不一致。单独使用
std::endl
endl
是一种不好的做法。如果它被清理掉,会得到+1。@Nanashi很抱歉,std::endl只是一个被遗忘的std::来清理。没有解释的代码没有帮助!@anatolyg我刚刚更新并注释了最重要的更改。Sean,请看一下最后的更改。这段代码可能有问题工作,但这不是好代码。请考虑编辑它并提供解释并做正确的修改。