I made this program that tells the user if the number that was inputted is prime or composite. At the end of a program I would normally create a char and then fill it but I wanted to try to do something more efficient. I wanted to use cin.get() but when I try to use it the program crashes. Here's the code:
#include <iostream>
int main()
{
usingnamespace std;
int input, test, ans = 0, i = 2;
bool loop;
cout << "Enter a number to be checked as a prime or composite number\n";
cin >> input;
while (i != input)
{
test = (input%i);
if (input == 1)
{
ans = 1;
break;
}
if (test == 0)
{
ans = 0;
break;
}
else
{
ans = 1;
}
i++;
}
if (ans == 0)
{
cout << endl << input << " is a Composite Number\n";
}
else
{
cout << endl << input << " is a Prime Number\n";
}
cout << "\nPress ENTER to quit\n";
cin.get();
return 0;
}
The code itself works but I posted the entire thing just in case. Thanks in advance.
Is my name really an l-value? *checks*
Yep. It is. Uh-oh. for(; Albatross > 0; Albatross--);
Aside from that, 2 is the initial value of i. If you make an exception to your algorithm, making it output that 2 is a prime number if 2 is entered, it would be alright.
Also, since it's more or less fixed, we could optimize it a bit. You only need to go through sqrt(input) (the plus one is for safety) iterations to determine if the number is prime or not. For larger prime numbers, this is one helluva time saver. You could change the program around a bit to deal with that...
Why two cin.get() s? Not 100% sure. I'd have to dig around in the source files for that.
However, it seems that (this may be wrong) cin.get() tries to get a character from the stream, and indeed there is a newline to fetch that hasn't been gotten from the stream yet. When the next cin.get() comes, there's nothing to fetch, so it waits for the user to give it something to fetch.
I changed my code to handle two now. Probably not in the most efficient way but here's the snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int input, test, ans = 0, i = 1;
bool loop;
cout << "Enter a number to be checked as a prime or composite number\n";
cin >> input;
while (i != input)
{
if (input == 1 || input == 2)
{
ans = 1;
break;
}
++i;
test = (input%i);
However, it seems that (this may be wrong) cin.get() tries to get a character from the stream, and indeed there is a newline to fetch that hasn't been gotten from the stream yet. When the next cin.get() comes, there's nothing to fetch, so it waits for the user to give it something to fetch.
#include <iostream>
int main()
{
usingnamespace std;
int input, oneortwo = 0, test, ans = 0, i = 2;
cout << "Enter a number to be checked as a prime or composite number\n";
cin >> input;
if (input == 1 || input == 2)
{
ans = 1;
oneortwo = 1;
}
while (i != input)
{
if (oneortwo == 1)
break;
test = (input%i);
cout << "Test: " << test << "\t\ti: " << i << endl;
i++;
if (test == 0)
{
ans = 0;
break;
}
else
{
ans = 1;
}
}
if (ans == 0)
{
cout << endl << input << " is a Composite Number\n";
}
else
{
cout << endl << input << " is a Prime Number\n";
}
cout << "\nPress ENTER to quit\n";
cin.get();
cin.get();
return 0;
}
If there are anymore please let me know.
EDIT:
The line:
cout << "Test: " << test << "\t\ti: " << i << endl
is purely for debugging purposes. Feel free to take it out.