从另一个.cpp文件启动.cpp文件

Start a .cpp file from another .cpp file

本文关键字:文件 cpp 启动 另一个      更新时间:2023-10-16

我已经在谷歌和Duckduckgo上尝试了所有内容。 我只是放弃了,来找你问一个问题。

我正在做一个学习CPP的项目,遇到了一个问题。我有 2 个文件。

一个叫:Nudle.cpp(主要的(

// Nudle.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <fstream>
#include <iostream>
#include "PasGen.cpp"
#include "PasGen.h"
using namespace std;
int main(int argc, char* argv[])
{
int choice;
do {
cout << "NUDLE Menun";
cout << "Please make your selectionn";
cout << "   1 - Generate passwordn";
cout << "   2 - View Saved Passwordsn";
cout << "   3 - Quitn";
cout << "Selection = ";
cin >> choice;
switch (choice) {
case 1:
break;
case 2:
cout << "????n";
break;
case 3:
cout << "Goodbye!";
break;
default:
cout << endl << endl << "Main Menun";
cout << "Please make your selectionn";
cout << "   1 - Start gamen";
cout << "   2 - Optionsn";
cout << "   3 - Quitn";
cout << "Selection = ";
cin >> choice;
}
} while (choice != 3);
return EXIT_SUCCESS;

还有一个叫做PasGen.cpp(密码生成器(

#include<iostream>
#include<cstdlib> 
#include<ctime> 
using namespace std;
static const char alphnum[] = "0123456789" "!@#$%^&*" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
int strLen = sizeof(alphnum) - 1;
char GenRand()
{
return alphnum[rand() % strLen];
}
int PassGen()
{
int n, c = 0, s = 0;
srand(time(0));
cout << "Enter the length of the password required:";
cin >> n;
cout << n << endl;
cout << "Your Password is:";
N:
char C;
string D;
for (int z = 0; z < n; z++)
{
C = GenRand();
D += C;
if (isdigit(C))
{
c++;
}
if (C == '!' || C == '@' || C == '$' || C == '%' || C == '^' || C == '&' || C == '*' || C == '#')
{
s++;
}
}
if (n > 2 && (s == 0 || c == 0))
{
goto N;
}
cout << D;
}

我只是不知道如何在一个人请求密码生成器

我希望它将输出显示到同一控制台,而不是打开一个新控制台。

管理员请不要关闭我的线程,如果它已经存在。 我几乎都是踩踏,仍然无法理解

您的主.cpp文件中存在一些问题。最大的一个是您将 cpp 文件包含在另一个 cpp 文件中。这通常应该如何工作,分别编译两个 cpp 文件,然后将它们链接在一起以创建最终输出。

所以我注释掉了以下内容(通过添加前导//(

#include "PasGen.cpp"

然后我必须包含 cstdlib,因为您正在使用EXIT_SUCCESS

#include <cstdlib>

最后,我可以在您的案例中调用该函数 1

case 1:
PassGen();
break;

在所有这些更改之后,我可以分别编译这两个文件并将它们链接在一起

g++ -c main.cpp
g++ -c PasGen.cpp
g++ main.o PasGen.o

然后运行它

$ ./a.exe
NUDLE Menu
Please make your selection
1 - Generate password
2 - View Saved Passwords
3 - Quit
Selection = 1
Enter the length of the password required:10
10
Your Password is:N0sJ%YphnFNUDLE Menu

您也可以同时编译这两个文件

g++ main.cpp PasGen.cpp 

这最终会做同样的事情,但最好从一开始就直接拥有概念。