Hi guys. Thanks for the help earlier. But I have a new problem now. Based on the code , type y to repeat and n to cancel the problem. How can I make a condition where if you type in other chars besides 'y' and 'n' a message will pop-up? I tried applying if statement but the program ended up looping my message over and over again.
#include <iostream>
usingnamespace std;
int main ()
{
int counter , fnum ;
char i;
for (;'y';)
{
{
cout<<"Please enter a positive integer: ";
cin>>fnum;
if (fnum > 0)
{
for (counter=fnum;counter>0;counter--)
{
if (fnum%counter==0)
cout<<counter<<endl;
}
}
elseif (fnum <=0)
{
cout<<fnum<<" is not a positive integer."<<endl;
}
cout<<endl<<"Would you like to see the divisors of another integer (y/n)?";
cin>>i;
}
if (i == 'n')
break;
}
return 0;
}
The looping can be a little more concise, consider a do-while:
1 2 3 4 5 6 7 8
char choice;
do
{
cout << "Would you like to go again (y/n) ? ";
cin >> choice;
cin.ignore(80, '\n'); // Make sure there is nothing in the buffer for next extraction
}
while (choice == 'y');