Can someone please explain why the cin.getline instruction doesnt work when the program goes a second time around .It is completely ignored.
Code:
//// output the factors of a chosen number//////
#include<iostream>
using namespace std;
void OutputFactors(void);
int GetValidNum(void);
int GoAgain(void);
int main(void)
{
OutputFactors();
return(0);
}
void OutputFactors(void)
{
for(;;)
{
int num = GetValidNum();
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
{
cout << i << '\t';
}
}
cout << '\n';
if(0 == GoAgain())
{
break;
}
}
}
int GetValidNum(void)
{
const int MAX = 128;
char chNum[128] = {0};
int check = 0;
do
{
check = 0;
cout << "Enter a number and I will output its factors.\n";
cin.getline(chNum, MAX, '\n');
for(int i = 0; chNum[i] != '\0'; i++)
{
if(chNum[i] < '0' || chNum[i] > '9')
{
check = 1;
}
}
if(check == 1)
{
cout << "That is not a valid number.\n"
<< "Please try again.\n";
}
}while(check == 1 || chNum[0] == '\0');
int num = atoi(chNum);
return(num);
}
int GoAgain(void)
{
char goAgain = '\0';
int check = 0;
cout << "Would you like another go.\n";
cin >> goAgain;
if(goAgain == 'y' || goAgain == 'Y')
{
check = 1;
}
return(check);
}
Thanks Mark S.