well i wrote the following code for practice. I think i'm getting an understanding of cin.ignore cin >> and etc etc etc...
But I still have a few questions.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <limits>
//#include <math.h>
#include <conio.h>
//#include <windows.h>
using namespace std;
bool cont = true;
bool iseven(int integer) { if ( (integer % 2) == 0) return true; else return false; }
int squarerootroof(int radicand)
{
int n = 0;
while( (n*n) <= radicand)
++n;
if (--n*n == radicand)
return n;
else return n+1;
}
bool isprime(int integer)
{
int numerator = 2, divisor = 2;
if (integer == 0 || integer == 1)
return false;
if (integer == 2)
return true;
if (iseven(integer))
return false;
//while(++numerator <= (sqrt(integer)))
while(++numerator <= (squarerootroof(integer)))
if (integer % numerator == 0)
++divisor;
if (divisor >= 3)
return false;
else return true;
}
int primecount(int number)
{
if (number == 0 || number == 1) return 0;
if (number == 2) return 1;
int copy = number;
while (copy >= 0)
{
if (isprime(copy))
return primecount(--copy) + 1;
else copy--;
}
}
int main()
{
int count = 0;
while(cont && ++count)
{
if (count >= 2) cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
int number; char more;
redonumber:
cout << "Enter a number for prime info. \n";
cout << '>';
cin >> number;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
goto redonumber;
}
for (int i = 0; i <= number; ++i)
{
if (isprime(i))
cout << i << " is prime. \n";
}cout << endl;
cout << "There are " << primecount(number) << " primes from 0 to " << number << ".\n";
redoredo:
cout << "Continue? Y/N \n";
more = getch();
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
goto redoredo;
}
if (more == 'Y' || more == 'y')
cont = true;
else if (more == 'N' || more == 'n')
cont = false;
else goto redoredo;
}
cout << flush;
cout << "You have reached the line of code stating that you have reached the end of the program. \n" << endl;
return 0;
}
|
Ok now for my questions.
For the most part it works fine, but I don't like the way line 59 looks. But were I to comment it out, it would catch invalid input that doesn't start with an int, but invalid input that starts with an int, would leave junk in the stream upon the second iteration of the loop. so then I came up with line 60, but with line 60 uncommented when the program starts there is nothing in the stream at all so cin.ignore waits for user input... which is not what I want. so I had to come up with this stupid count variable which ensure that line 60 is not executed on the first time through the loop.
What is the ideal way to handle this?
My second question: Because I use getch on line 80, and storing it into a char, do I even need to have a cin.ignore clause for it, it seems to work ok were I to comment out line 84. Because I don't think its possible for cin to even go into a failstate, or is it?
Any other suggestions would be appreciated.
Thanks!
Oh yeah...
what does std::numeric_limits<std::streamsize>::max() do exactly anyway?
I like to know what all the code I use does, and I don't know what it does.
Is there a simpler way to just
"ignore everything in the buffer"?