C++想回到上次离开的地方
C++ want to return to where left off
#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;
要控制程序的流程,您有多个选项。
如果你多次做事,你就会有for
和while
循环。如果要停止循环并退出,请使用 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
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 离开范围后如何保护局部变量的值?
- C++程序过早地离开我的 for 循环
- 将"Compass"与相机旋转隔离开GL
- 我的boost managed_shared_memory在离开作用域时似乎不会从内存空间中取消映射
- 即使在离开作用域后,如何访问在堆上分配的变量C++?
- 离开方法时重置变量值
- 程序在遍历所有值之前离开循环. 数组的
- 鼠标离开 GLFW窗口后触发 GLFW 鼠标回调?
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃
- 将局部变量保存到全局向量中,为什么离开局部范围后可以得到这些局部变量?
- c++ 指针在离开作用域后不会停留
- 在离开模式下唤醒窗户
- 分段错误发生在索引离开C++数组边界之后
- 离开功能后丢失一些数据
- 为什么我们可以使用空括号来初始化 std::function<void(Node*)>离开?
- C++ std::stack 内容在离开函数时不会自由
- 在相应的承诺离开范围后有效使用未来
- 空的唯一指针在离开作用域时调用析构函数
- Android编程 - 如何离开活页夹