have no idea what to make the class return if a test fails

i have two ciphers written,both of which require a key.
now there is a catch here
the key,and the number of letters in the alphabet( either 26,30,40)
must be coprime.
now testing isn't the problem,the problem is i have no idea what to make them do if the test fails.
i want this code to be easily put in a gui,so asking to enter a keyword again is a no.
thought about returning an empty string,but that wouldn't be very good,cause how would one differ these two cases:
one where the keyword is wrong
and the other where the user just entered an empty string.
the way i have it setup now is i have a isKeywordCorrect();
function that just returns true or false.
but this imo is quite bad since you have to enter a keyword then call this.
and how would somebody using the library of ciphers even know that he is supposed to call it...
I have asked quite a few questions here in the past few days,and that's because this is my first project of this size.So i'm really confused about a lot of things.
Last edited on
Throw an exception. You cannot ignore it, and it delegates responsibility to handle error to code using this class/function.
hmm
okay
i looked at what try catch things are and have a couple of questions.

after I try the code,what do i actually make the catch block do,apart from printing to console(i dont want to do that),i tried returning 0 but that gives some message that isn't what i want it to be.
basically i need to throw the exception if 2 numbers aren't coprime
can you show me an exmple on how i would do this?
what do i actually make the catch block do
Anything you need to do in case of an error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdexcept>
//...
std::string cypher(std::string original, int key)
{
    if(not is_coprime(key, alphabet.size()))
        throw std::invalid_argument("Key and alphabet size should be coprime");
    //...
}	

//Terminating the program
try {
    //...
    input = cypher(input, key);
    //...
} catch(std::invalid_argument& e) {
    std::cerr << e.what();
    std::exit(EXIT_FAILURE);
}

//Letting user reenter key (in dialog mode)
do {
    try {
        std::cin >> key;
        run_again = false;
        //...
        input = cypher(input, key);
        //...
    } catch(std::invalid_argument& e) {
        std::cerr << e.what() << '\n';
        run_again = true;
        std::cout << "Please, reenter key: "
    }
} while(run_again);

//Display dialog window:
void onButtonClick
{
    //...
    try {
        //...
        encode(input_field.text(), key.value()); // call cypher inside
    } catch(std::invalid_argument& e) {
        messageBox("Key and alphabet size should be coprime\nPlease reenter key");
        setFocus(key);
    }
}
oh
that works
thank you !
i don't need to use try in the function?
Last edited on
i don't need to use try in the function?
You need to use try ... catch block where you know how to handle exception.
alright
i get it now.
thank you for your help
Topic archived. No new replies allowed.