繁体   English   中英

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

[英]Start a .cpp file from another .cpp file

我在 google 和 dadduckgo 上都试过了。 我只是放弃了,来找你问一个问题。

我正在研究一个学习 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 Menu\n";
        cout << "Please make your selection\n";
        cout << "   1 - Generate password\n";
        cout << "   2 - View Saved Passwords\n";
        cout << "   3 - Quit\n";
        cout << "Selection = ";
        cin >> choice;

        switch (choice) {
        case 1:
            break;
        case 2:
            cout << "????\n";
            break;
        case 3:
            cout << "Goodbye!";
            break;
        default:
            cout << endl << endl << "Main Menu\n";
            cout << "Please make your selection\n";
            cout << "   1 - Start game\n";
            cout << "   2 - Options\n";
            cout << "   3 - Quit\n";
            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;
}

我只是想不通在有人请求密码生成器后如何让 Nudle 启动 PasGen

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

如果它已经存在,请管理员不要关闭我的线程。 我几乎所有的胎面,仍然无法理解

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

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

#include "PasGen.cpp"

然后我必须包含 cstdlib,因为您使用的是 EXIT_SUCCESS

#include <cstdlib>

最后我可以在你的情况下调用 function 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 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM