0

I'm trying to create a while loop to use with a menu in my program. It displays the menu initially, if I select option 1 it executes GenerateData(), and re-displays the menu, but if I try to select option 1 again the program terminates with exit code: 1 in the console. Any other option from the does nothing, then continuously prompts for imput.

#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;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
David Conrod
  • 1
  • 1
  • 1
  • 4
    Please do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) of the `LoadDataFromFile` function. – Some programmer dude Jun 28 '20 at 19:59
  • 1
    I also suggest you learn how to use an actual debugger to step through your code statement by statement, while monitoring variables and their values. That's the normal way to figure out problems like this. – Some programmer dude Jun 28 '20 at 20:01
  • 2
    Lastly I suggest you learn about the `switch` statement. And to not copy-paste common code like you do, when you can just call the `DisplayMenu` function *once* in the loop. – Some programmer dude Jun 28 '20 at 20:02
  • 1
    The problem appears to be with the `GenerateData()` function, something its doing doesn't want to be called twice, so that's probably the issue. – BEN1JEN Jun 28 '20 at 20:07
  • 1
    Your `main()` function should also return 0 because every value except for 0 is an error code (on a unix system), so that would also cause your program to exit with error code 1. – BEN1JEN Jun 28 '20 at 20:07
  • Also, `LoadDataFromFile` doesn't do anything because all the variables you're loading are local to that function. – BEN1JEN Jun 28 '20 at 20:08
  • @Some Programmer dude Do you have idea what my issue could be? The LoadDataFromFile function isn't complete, I was trying to get the menu working fist before I move on to other things. I'm pretty new to programming and this is my first time using C++ so I don't know what an actual debugger means and I have no idea how to use switch statements. I did just change the DisplayMenu call to be called once in the loop but the problem still remains the same as before, the only menu option that works properly is the first one, and even then it only works the first time. – David Conrod Jun 28 '20 at 20:09
  • I thought that main() returning 1 was strange, but that, as well as the GenerateData() function was provided code for the assignment so I left it. The prompt for this assignemnt said to not touch GenerateData() so I'll leave that but I can try returning 0 from main() – David Conrod Jun 28 '20 at 20:14
  • For example I have no idea why the DisplayData function wouldn't work, but for some reason it doesn't do anything at the moment, it doesn't even seem to execute the one line of code inside of the funtion. I'm at a real loss here – David Conrod Jun 28 '20 at 20:28
  • Quite honestly, if this really is your first time attempting to work with C++ then you're getting way ahead of yourself! Slow down, even take a few steps back, get a couple of [decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn C++ from the ground up first. – Some programmer dude Jun 29 '20 at 05:12
  • And as others have mentioned, something in the `GenerateData` data function probably isn't properly released the first time you call it, so the second time you call it then something will complain about being initialized multiple times. Considering that your claim about "exit value 1" it's very likely the `JNI_CreateJavaVM` call (it's the only place where you actually `exit` your program). It's time you take the comment `TO DO: error processing...` and do it. – Some programmer dude Jun 29 '20 at 05:15

0 Answers0