I am currently stuck on how to differentiate integers from each line in a text file. The input file consists of 2 lines. The first line has two numbers, the second has only one number. I am trying to take the GCD of two numbers in the first line, and then take the single number of the second line and calculate the phi and provide a list of totatives for the number.
It isn't clear whether your problem is reading from file (which is what the title implies) or calculating phi(n), which is what you have written subsequently.
To read from file simply do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int num1, num2, n;
ifstream in( "input.txt" );
in >> num1 >> num2 >> n;
in.close();
cout << "Numbers are " << num1 << " " << num2 << " " << n << endl;
}
The first problem with your calculation of phi(n) is that you call Phi(n) in main() before inputting n.
If you have a working GCD routine (I haven't checked) then you could simply use this to calculate phi(n). Cycle through the integers i less than or equal to n and the totatives are those for which GCD(n,i) = 1: either store them in a vector or just output them as you go along. Phi(n) is the number of such totatives: count them as you find them.
There are probably much more efficient ways: if n is prime, for example, then Phi(n) would simply be n-1 without further calculation.
I think the introductory sentences from the OP are a dead giveaway that input.txt is the source of the data. The rest is just an indication that there is an attempt being made to take us for a ride.