Note that there are some explanatory texts on larger screens.

plurals
  1. POConfusion about correct design of a program C++
    primarykey
    data
    text
    <p>I made a small program that generates primes and lets the user check a number and see if it's a prime or not. Problem is, I'm not sure how to properly design it. This is the program:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; typedef unsigned long long bigint; std::vector&lt;bool&gt; sieve(size_t size) { std::vector&lt;bool&gt; primelist(size); primelist[0] = false; primelist[1] = false; for (bigint i = 2; i &lt; size; ++i) { primelist[i] = true; } for (bigint i = 2; i * i &lt; size; ++i) { if (primelist[i]) { for (bigint j = i; j * i &lt; size; ++j) primelist[i*j] = false; } } return primelist; } int main() { bigint range; bigint number; std::vector&lt;bool&gt; primes; std::cout &lt;&lt; "Enter range: " &lt;&lt; std::endl; std::cin &gt;&gt; range; primes = sieve(range); while (1) { std::cout &lt;&lt; "Enter number to check: " &lt;&lt; std::endl; std::cin &gt;&gt; number; if (primes[number]) std::cout &lt;&lt; "Prime" &lt;&lt; std::endl; else std::cout &lt;&lt; "Not prime" &lt;&lt; std::endl; } return 0; } </code></pre> <p>The basic flow I want to achieve is: Input range, /handle input/, input number to check, /handle input/</p> <p>I also want to give the user an option to change the range at any given time, by writing a command like "change range <em>number</em>"</p> <p>I have a few problems with this:</p> <p>I want the program to be under control if the user inputs a range bigger than unsigned long long, and if the user basically exceeds any limit(like for example if the range he input was 100 then if he checks for 101) an exception will be caught. I know this needs to be implemented using try/catch/throw, but I have no idea how to do that while keeping the option to change the range and without making my code spaghetti code.</p> <p>Also, I want the errors to be of enum type(I read that enums are good for exceptions), something like</p> <pre><code>enum errors { OUT_OF_RANGE = 1, //Out of the range specified by the user INCORRECT_VALUE, //If user input "one" instead of 1 RANGE_SIGNED, //If user inputs a signed value for range NUM_LIMITS //Number exceeds unsigned long long }; </code></pre> <p>I have no idea how to use exception handling, not to mention using it with enums. How the hell do I keep this program safe and running, while keeping away from spaghetti code?</p> <p>I am extremely confused. If someone could help me design this program correctly and maintain readability and efficiency, it will really improve my future program designs.</p> <p>Thanks for reading! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload