It goes through repeatedly on every option so far except when a the prime selection is selected.
It goes through the prime function and at the end returns back to the display function just like the rest.
//prime option
void prime()
{
//variables
int number;
bool isPrime = true;
cout << "P R I M E N U M B E R T E S T\n" << setw(26) << setfill('=')
<< "=\n" << setfill(' ') << "Enter int > 0: ";
cin >> number;
if ( number < 2 )
{
isPrime = false;
}
if ( number > 2 && ( number % 2 ) == 0 )
{
isPrime = false;
}
for(int i = 2; i < number; i++ )
{
if ( ( number % i ) == 0)
{
isPrime = false;
break;
}
}
if( isPrime == true )
{
cout << number << " is a prime number\n" << endl;
}else{
cout << number << " is NOT a prime number\n" << endl;
}
display();//Return to program start
}
Except it automatically crashes after. I followed debug & it went straight through the display function & never stopped at getline. Any ideas?
What calls prime() ? Is it optionDisplay()? Post it.
Though it would be best if you posted your whole program, or a compilable relevant subset that has the same problem, if there is a lot of code.
That's because cin >> leaves the end of line in input stream. This end of line is found by getline so it looks as if the input was skipped. In this situation getline reads an empty string and the program crashes when you try to access its non existent first character.
By the way, to use long strings you can write
1 2 3
cout << "Available options:\n\"
" 0. PRIME - Determine if a number is prime\n\"
" 1. SQUARE - Determine if a number is a perfect square\n\"
Now you can align the strings as you like.
Also, using recursion to repeat things is not a good idea. In most cases it won't harm you, but at times it may be hard to debug and is generally disliked. Note that in some cases use of recursion may cause a crash due to stack overflow. You could easily avoid recursion by wrapping lines 54-57 in a do while loop.