vendredi 3 avril 2020

How to implement exceptions

void getInitials() {
string result;
cout << "Please Enter your name: ";
string name;
getline(cin, name);
int nameSize = name.size();



result += toupper(name.at(0)); //(name.at(0) + ('A' + 'a')) if the letter is already uppercase then it changes the ascii value
for (int i = 0; i < nameSize; i++) {
    /*
    try{
        if (name.empty()) {
            throw "Invalid argument/statement!";
        }
    }
    */
    if (!name.empty()) { //checks if empty 
        if (name[i] == ' ') { //if name is == to a space then it proccess to i + 1 to get the Initials 
            result += toupper((name[i + 1]));
        }
    }
}
cout << "Initials: "<< result;

}

This program gets the initials of names. Everything works fine I just wanted to know how to implement exceptions into it correctly and see if you guys can critique my code to make it better.

Aucun commentaire:

Enregistrer un commentaire