mercredi 6 mai 2015

Taking "exit" as user input in C++

I'm currently trying to write code that takes user input as strings, and then convert them to integers if I need to. If the user decides to enter exit then the program should move on to calling a function. Here is what I have so far:

void printArray(string string_array[], int size){
    for (int i = 0; i < size; i++){
        cout << string_array[i];
    }

}
void a_func(){
    string string_array[10];
    string user_input;

    while (user_input != "exit"){
        cout << "Please enter a number between 0 - 100: ";
        cin >> user_input;
        if (stoi(user_input) < 0 || stoi(user_input) > 100){
            cout << "Error, please re-enter the number between 0 - 100: ";
            cin >> user_input;
        }
        else if (user_input == "exit"){
            printArray(string_array);
        int array_index = stoi(user_input) / 10;
        string_array[array_index] = "*";

However, as I'm testing the program, the console is aborting the program if I enter exit. Is there a way for me to enter exit and then the program calls printArray?

Aucun commentaire:

Enregistrer un commentaire