簡體   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