C++想回到上次离开的地方

C++ want to return to where left off

本文关键字:离开 C++      更新时间:2023-10-16
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int password;
    system("cls");
    cout <<"Login"<< endl;
    cout <<"Enter password to continue"<< endl;
    cin >> password;
    cin.ignore().get();
          if( password == 1111)
          {
              system("cls");
              cout <<"Access Granted"<< endl;
              system("PAUSE");
              system("cls");
              return main();
          }
          //Want to make if( password == 1111) return to main(), but start    where it left off
          //I want it to start at cout <<"Files:'<< endl; 
                  cout <<"Files:"<< endl;
                  cout <<"n E n N n G n S n"<< endl;
                  cout <<"Choose a file"<< endl;
                  string file;
                  cin >> file;
                  cin.ignore().get();
                                 if(file == "E" || file == "e")
                                 {
                                         system("cls");
                                         cout <<"E:"<< endl;
                                         cout <<"Age:"<< endl;
                                         cout <<"Grade:"<< endl;
                                         cout <<"Eye color:"<< endl;
                                         cout <<"Hair color:"<< endl; 
                                         system("Pause");     
                                 }
                                 else if(file == "N" || file == "n")
                                 {
                                      system("cls");
                                      cout <<"N:"<< endl;
                                      cout <<"Age:"<< endl;
                                      cout <<"Grade:"<< endl;
                                      cout <<"Eye color:"<< endl;
                                      cout <<"Hair color:"<< endl;
                                      system("Pause");
                                 }        
                                 else if(file == "G" || file == "g")
                                 {
                                      system("cls");
                                      cout <<"G:"<< endl;
                                      cout <<"Age:"<< endl;
                                      cout <<"Eye color:"<< endl;
                                      cout <<"Hair color:"<< endl;
                                      system("Pause");         
                                 }        
                                 else if(file == "S" || file == "s")
                                 {
                                      system("cls");
                                      cout <<"S:"<< endl;
                                      cout <<"Age:"<< endl;
                                      cout <<"Eye color:"<< endl;
                                      cout <<"Hair color:"<< endl;
                                      system("Pause");         
                                 }        
          else
          {
              system("cls");
              cout<<"Access Denied!"<< endl;
              system("PAUSE");
              return 0;    
          }
    return 0;
}

无法弄清楚如何继续主要功能离开的地方。我一直在自学如何编码,所以我不知道这是否可能。阅读我在代码中留下的评论,以更好地理解我在说什么。感谢

如果删除此行:

return main();

然后计算机将继续按照写入的顺序执行指令 - 特别是下一个指令将被cout <<"Files:"<< endl;

.

要控制程序的流程,您有多个选项。

如果你多次做事,你就会有forwhile循环。如果要停止循环并退出,请使用 break .如果要跳到下一个小版本,请使用 continue

如果你想简单地跳到程序中的不同位置,你可以使用goto但它是一种代码异味。通常最好避免使用它,因为它往往会损害可读性。

如果你想做一个可以打断的任务,你可以使用void function。如果您希望函数停止其正在执行的操作并继续main,请使用 return


但是,在您的特定示例中,您似乎不太了解 if 语句的工作原理。如果if条件为真,则执行大括号 ({ ... }( 中的任何内容。它会自动在右大括号后继续执行。您不需要显式返回到主。

你在那里实际做的(错误地(是编写一个递归函数(一个在满足条件之前调用自己的函数(。

您似乎严重误解了C++程序中的程序流。 return main()将对 main 进行新的调用,因此用户将再次看到"登录"提示。当对 main 的调用结束时,由于您使用了关键字 return ,它将退出上一次对 main 的调用。这些都不是你想要的。C++标准也明确禁止从另一个函数调用 main。

我还注意到,在缩进功能的长块之后,您有一个 else 语句,看起来像是要if (password == 1111)的 else .

缩进对C++编译器没有任何意义,它纯粹是为了人类可读性。

我认为您要实现的目标更像是这样的:

if (password == 1111)
{
    std::cout << "Access grantedn";
}
else
{
    std::cout << "Access deniedn";
    return 0;
}
std::cout << "Files:n";

在这里,如果用户键入 1111,则执行第一个复合代码块,并在 if/else 块结束后恢复执行,即下一条指令是打印文件。

如果用户键入其他内容,则执行else块。它以 return 0 结束,退出函数并将值 0 返回给调用方。

#include <iostream>
#include <string>
int main()
{
    std::cout << "Login:n";
    std::string password;
    std::getline(std::cin, password);
    if (password == "1111")
        std::cout << "That's correct.n";
    else {
        std::cout << "Access denied.n";
        return 0;
    }
    std::cout << "Files:n";
    // your code here
}

演示:http://ideone.com/DSYAG4