如何在 C++ 中返回到上一步

How to go back to previous step in c++

本文关键字:一步 C++ 返回      更新时间:2023-10-16

我是一个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;
}