0

我正在尝试创建一个 while 循环以在我的程序中与菜单一起使用。它最初显示菜单,如果我选择选项 1,它会执行 GenerateData(),并重新显示菜单,但如果我再次尝试选择选项 1,程序将以退出代码终止:控制台中的 1。中的任何其他选项都不执行任何操作,然后不断提示输入。

#include <iostream>
#include <jni.h>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void GenerateData()               //DO NOT TOUCH CODE IN THIS METHOD
{
     JavaVM *jvm;                      // Pointer to the JVM (Java Virtual Machine)
     JNIEnv *env;                      // Pointer to native interface
     //================== prepare loading of Java VM ============================
     JavaVMInitArgs vm_args;                        // Initialization arguments
     JavaVMOption* options = new JavaVMOption[1];   // JVM invocation options
     options[0].optionString = (char*) "-Djava.class.path=";   // where to find java .class
     vm_args.version = JNI_VERSION_1_6;             // minimum Java version
     vm_args.nOptions = 1;                          // number of options
     vm_args.options = options;
     vm_args.ignoreUnrecognized = false;     // invalid options make the JVM init fail
                                                                      //=============== load and 
initialize Java VM and JNI interface =============
     jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);  // YES !!
     delete options;    // we then no longer need the initialisation options.
     if (rc != JNI_OK) {
            // TO DO: error processing...
            cin.get();
            exit(EXIT_FAILURE);
     }
     //=============== Display JVM version =======================================
     cout << "JVM load succeeded: Version ";
     jint ver = env->GetVersion();
     cout << ((ver >> 16) & 0x0f) << "." << (ver & 0x0f) << endl;

     jclass cls2 = env->FindClass("ZooFileWriter");  // try to find the class
     if (cls2 == nullptr) {
            cerr << "ERROR: class not found !";
     }
     else {                                  // if class found, continue
            cout << "Class MyTest found" << endl;
            jmethodID mid = env->GetStaticMethodID(cls2, "createZooFile", "()V");  // find method
            if (mid == nullptr)
                   cerr << "ERROR: method void createZooFile() not found !" << endl;
            else {
                   env->CallStaticVoidMethod(cls2, mid);                      // call method
                   cout << endl;
            }
     }


     jvm->DestroyJavaVM();
     cin.get();
}

void AddAnimal()
{
     /*
            TODO: Write proper code to add an animal to your vector (or array)
     */
}


void RemoveAnimal()
{
     /*
            TODO: Write proper code to remove an animal from your vector (or array. Remmber to re- 
   allocate proper size if using array)
     */
}

void LoadDataFromFile()
{
     /*
            TODO: Write proper code to load data from input file (generated using JNI) into 
vector/array.
     */
    vector<vector<string> > anmlData;


    string trackID;
    string anmlName;
    string anmlType;
    string anmlSubType;
    int numEggs;
    int isNursing;
    ifstream zooData;

    while (!zooData.eof())
    {
        zooData.open("zoodata.txt");
        zooData >> trackID;
        zooData >> anmlName;
        zooData >> anmlType;
        zooData >> anmlSubType;
        zooData >> numEggs;
        zooData >> isNursing;
        zooData.close();
    }

}

void SaveDataToFile()
{
     /*
            TODO: Write proper code to store vector/array to file.
     */
}

void DisplayMenu()
{
    cout << "************************" << endl;
    cout << "****Make a Selection****" << endl;
    cout << "*                      *" << endl;
    cout << "* 1) Generate Data     *" << endl;
    cout << "* 2) Load Data         *" << endl;
    cout << "* 3) Display Data      *" << endl;
    cout << "* 4) Add Record        *" << endl;
    cout << "* 5) Delete Record     *" << endl;
    cout << "* 6) Save Data         *" << endl;
    cout << "* 0) End Session       *" << endl;
    cout << "*                      *" << endl;
    cout << "************************" << endl;
}


void DisplayData()
{

}
int main()
{
    int userInput = -1;
    DisplayMenu();

    while (userInput != 0)
    {
        cin >> userInput;
        if (userInput == 1)
        {
            GenerateData();
            DisplayMenu();
        }
        if (userInput == 2)
        {
            LoadDataFromFile();
            DisplayMenu();
        }
        if (userInput == 3)
        {
            DisplayData();
            DisplayMenu();
        }
        if (userInput == 4)
        {
            AddAnimal();
            DisplayMenu();
        }
        if (userInput == 5)
        {
            RemoveAnimal();
            DisplayMenu();
        }
        if (userInput == 6)
        {
            SaveDataToFile();
            DisplayMenu();
        }
    }
    cout << "done";
    return 1;
}
4

0 回答 0