简体   繁体   中英

Program exits after user input after my switch case

I've been working on an assignment and after writing my code and running it I have noticed that my program exits after my inputting my first user input after my switch case.

I have asked around and found out that is was due to memory allocation on my struct/pointer and have tried initializing new on my int main but it still exits without running the whole program. I am a complete beginner on this and would ask for help.

My code is posted below, any help is appreciated, thanks a lot!

This is my declaration of struct & global variables as well as the function where my program exits

//struct declaration

struct Students {
    std::string givenName, lastName;
    int         studentType, NumSize, labUnits, lecUnits, totalUnits;
    char        paymentMode;
    long long   courseCode;
};
    
    //Students studs[100]; //global struct declaration
    
    //function prototype
    void
    infoStudent();
    
    void
    studentCode();
    
    void
    computation();
    
    //global variables
    bool inputerror;
    Students stud1;
    Students* studP;
    
    
   
void
infoStudent() {
    
    studP = new Students[size];   //allocates memory 
    delete [] studP;

    std::cout << "\nInput data for Student #" << ctr << ": " << std::endl;

    std::cout << "Student's Last Name: ";
    getline( std::cin, studP[ctr].lastName );

    std::cout << "Student's Given Name: ";
    getline( std::cin, studP[ctr].givenName );

    do {
        std::cout << "Student Number: ";
        std::cin >> studP[ctr].courseCode;
        inputerror = std::cin.fail();

        if ( inputerror || ( studP[ctr].courseCode < 2000000000 || studP[ctr].courseCode > 2999999999 ) ) {
            std::cout << std::endl << "Please enter a proper Student Number." << std::endl << std::endl;
            std::cin.clear();
            std::cin.ignore( 5, '\n' );
        }
    } while ( inputerror || ( studP[ctr].courseCode < 2000000000 || studP[ctr].courseCode > 2999999999 ) );

    do {
        do {
            std::cout << "Number of Laboratory Units: ";
            std::cin >> studP[ctr].labUnits;
            inputerror = std::cin.fail();

            if ( inputerror || ( studP[ctr].labUnits < 0 || studP[ctr].labUnits > 15 ) ) {
                std::cout << std::endl << "Please enter a proper amount of Laboratory Units." << std::endl << std::endl;
                std::cin.clear();
                std::cin.ignore( 5, '\n' );
            }
        } while ( inputerror || ( studP[ctr].labUnits < 0 || studP[ctr].labUnits > 15 ) );

        do {
            std::cout << "Number of Lecture Units: ";
            std::cin >> studP[ctr].lecUnits;
            inputerror = std::cin.fail();

            if ( inputerror || ( studP[ctr].lecUnits < 0 || studP[ctr].lecUnits > 15 ) ) {
                std::cout << std::endl << "Please enter a proper amount of Lecture Units." << std::endl << std::endl;
                std::cin.clear();
                std::cin.ignore( 5, '\n' );
            }
        } while ( inputerror || ( studP[ctr].lecUnits < 0 || studP[ctr].lecUnits > 15 ) );

        studP[ctr].totalUnits = studP[ctr].labUnits + studP[ctr].lecUnits;

        if ( studP[ctr].totalUnits <= 15 ) {
            std::cout << std::endl << "You have " << studP[ctr].totalUnits << " Total Units.\n";
        } else if ( studP[ctr].totalUnits > 15 ) {
            std::cout << std::endl
                      << "Please enter a proper amount of Laboratory and Lecture Units.\n(Must not exceed over 15 units.)\n"
                      << std::endl;
        }
    } while ( studP[ctr].totalUnits > 15 );

    do {
        std::cout << std::endl << "Mode of Payment ([F/f] Full or [I/i] Installment): ";
        std::cin >> (studP[ctr].paymentMode );;
        studP[ctr].paymentMode = std::tolower( studP[ctr].paymentMode );

        if ( studP[ctr].paymentMode == 'f' ) {
            std::cout << "Your chosen Mode of Payment is Full" << std::endl;
        } else if ( studP[ctr].paymentMode == 'i' ) {
            std::cout << "Your chosen Mode of Payment is Installment" << std::endl;
        }

        if ( studP[ctr].paymentMode != 'f' && studP[ctr].paymentMode != 'i' ) {
            std::cout << std::endl << "Please enter a proper mode of payment." << std::endl;
        }

    } while ( studP[ctr].paymentMode != 'f' && studP[ctr].paymentMode != 'i' );
}

You are allocating studP and then you immediately free the memory. After that you have a dangling pointer and then you are using this invalid pointer (all in function infoStudent ).

I have to mention that you have a switch-case and you call the same function with no arguments in every case, so this is pointless.

I should also mention, that you have several magic numbers in your code and you shouldn't use global variables that way. Also be sure to initialize all variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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