For homework this week, I have been tasked with taking input from a file and taking the greatest common divisor recursively from the numbers that were inputted. I've run into an issue though. I know that I have to use cin.peek() when it comes to reading where the end of the line is, but I'm having trouble with it. How would I change the code so the lines are displayed separately?
There are 25 numbers in the file, but I'll just display the first couple lines just to get an idea of what I am talking about. I have also attached my code below and any help is greatly appreciated. I am not looking for a straight answer. I just need to be pointed in the right direction.
Thanks!
**Text File:
5 7
12 15
80 40 100
Code:
int gcd(int a, int b);
int gcd(int a, int b)
{
if (b != 0)
return gcd(b, a % b);
else
return a;
}
int main()
{
ifstream input("A1.txt");
int a = 0;
int b = 0;
int i = 0;
int j = 0;
int gcdArr[25];
if (input.is_open())
{
for (i = 0; i < 25; i++)
{
input >> gcdArr[i];
}
}
i = 0;
for (j = 0; j < 25; j++)
{
while ((cin.peek()) != '\n')
{
cout << gcd(gcdArr[i], gcdArr[i+1]) << endl;
}
i++;
}
_getch();
return 0;
}