如何在 C++ 中返回到上一步
How to go back to previous step in c++
我是一个C++初学者,我正在制作一个工具,我可以在其中访问所有自制工具。但问题是,如果我按下某事并返回,我无法选择其他选项。 例如,如果我选择"3"并返回,我无法访问"1"或"2"有人知道解决方案吗?我想用乘数步来做到这一点。 谢谢!
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
using std::cout;
using std::string;
using std::cin;
int main()
{
string username;
string password;
string secondchoice;
string passwd="",user;
string firstchoice;
cout << "www.mytools.comnnWelcome to MyTools!";
getchar();
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
cin >> firstchoice;
system("CLS");
firstchoice = " ";
while (firstchoice != "q")
{
cin >> firstchoice;
if (firstchoice == "1")
{
system("cls");
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword") {
system("cls");
cout << "You are succesfully logged in as "; cout << username; cout << "!";
}
else {
cout << "username or password incorrect! press enter to try again. ";
string enter;
getch();
system("cls");
}
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword")
{
system("cls");
cout << "You are succesfylly loggen in as "; cout << username; cout << "!";
}
else {
cout << "You have been blocked from the server for to many triesn";
return 0;
}
}
if (firstchoice == "2") {
system("cls");
cout << "some stuff";
getch();
system("cls");
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
cin >> firstchoice;
system("CLS");
}
if (firstchoice == "3") {
system("cls");
cout << "some stuff";
getch();
system("cls");
cout << "welcome to MyTools!";
getch();
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
cin >> firstchoice;
system("CLS"); }
}
return 0;
}
您有一个字符串首选。因此,您可以每隔一段时间更改为 if 并创建一个 while 循环while (firstchoice != "q")
并将cin>> firstchoice;
放在该 while 循环中。
firstchoice = " ";
while (firstchoice != "q")
{
std::cin >> firstchoice;
if (firstchoice == "1")
{
...
}
}
这对我有用:
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
using std::cout;
using std::string;
using std::cin;
int main()
{
string username;
string password;
string secondchoice;
string passwd = "", user;
string firstchoice;
cout << "www.mytools.comnnWelcome to MyTools!";
getchar();
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
cin >> firstchoice;
system("CLS");
firstchoice = " ";
while (firstchoice != "q")
{
cin >> firstchoice;
if (firstchoice == "1")
{
system("cls");
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword") {
system("cls");
cout << "You are succesfully logged in as "; cout << username; cout << "!";
}
else {
cout << "username or password incorrect! press enter to try again. ";
string enter;
system("cls");
}
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword")
{
system("cls");
cout << "You are succesfylly loggen in as "; cout << username; cout << "!";
}
else {
cout << "You have been blocked from the server for to many triesn";
return 0;
}
}
if (firstchoice == "2") {
system("cls");
cout << "some stuff";
system("cls");
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
}
if (firstchoice == "3") {
system("cls");
cout << "some stuff";
system("cls");
cout << "welcome to MyTools!";
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
}
}
return 0;
}
看起来您正在填充 firstchoice 两次 - 一次在 while 循环的开头,一次在选项 2 和 3 结束时提示用户。你只需要一开始的那个。我建议像所示那样分解您的代码。
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <ctime>
using std::cout;
using std::string;
using std::cin;
string getChoice() {
cout << "nlogin [1]";
cout << "nabout us [2]";
cout << "ncontact [3]";
cout << "nMake your choice: ";
string choice;
cin >> choice;
return choice;
}
int main() {
string username;
string password;
string secondchoice;
string passwd = "", user;
string firstchoice;
cout << "www.mytools.comnnWelcome to MyTools!";
getchar();
system("CLS");
firstchoice = " ";
while (firstchoice != "q")
{
firstchoice = getChoice();
if (firstchoice == "1")
{
system("cls");
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword") {
system("cls");
cout << "You are succesfully logged in as "; cout << username; cout << "!";
}
else {
cout << "username or password incorrect! press enter to try again. ";
string enter;
system("cls");
}
cout << "username: ";
cin >> username;
cout << "password: ";
cin >> password;
if (username == "myusername" && password == "mypassword")
{
system("cls");
cout << "You are succesfylly loggen in as "; cout << username; cout << "!";
}
else {
cout << "You have been blocked from the server for to many triesn";
return 0;
}
} else if (firstchoice == "2") {
system("cls");
cout << "some stuff";
system("cls");
} else if (firstchoice == "3") {
system("cls");
cout << "some stuff";
system("cls");
cout << "welcome to MyTools!";
}
}
return 0;
}
相关文章:
- 维护unordered_map但同时每一步都需要最低的映射值
- 如何在最后一步使用CryptDecrypt解决NTE_BAD_DATA
- 如何在 C++ 中返回到上一步
- 如何一步一步地正确解释此代码?(编程新手)
- 通过GL_FLOAT,GL_UNSIGNED_INT一步
- 将隐式促销添加到复杂类,最后一步
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 检查下一步是检查员
- 改进malloc()算法的下一步是什么
- 如何在c++骰子游戏中存储第一个值,并在下一步进行比较
- 如何一步一步地遍历目录树
- 有没有一个调试器可以让我在代码中后退一步
- 若在下一步中对象被破坏,为什么不自动移动呢
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- 一步一步寻找选择排序大θ符号的过程
- 两步到一步的初始化和错误处理
- 带有下一步/后退按钮的Qt对话框/小部件
- 无法理解此代码的最后一步
- 只有当我在 c++ 中按 Enter 键(线程)时,代码如何进入下一步
- 离散傅立叶变换C++-下一步要做什么