Hi I am trying to validate user input from a text file. When attempting to validate, I don't get the desired result.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
// start of program
int num1, num2;
int main() {
std::cout << "Welcome to my program" << '\n';
ifstream inFile;
int num, i;
// includes the prime.txt file
inFile.open("prime.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
cout << "PRIME NUMBER" << endl << "enter yout prime numbers" << endl;
cout << "Enter your first prime number" << endl;
cin >> num1;
cout << "Enter your second prime number" << endl;
cin >> num2;
// displays file information and fucntions to determine if a prime number
// was entered
while (inFile >> num) {
if (cin.fail) {
std::cout << num1 << " is a prime number" << '\n';
}
else if (num2 = num) {
std::cout << num2 << " is a prime number" << '\n';
}
else if (num2 != num) {
std::cout << "Second number is not a prime number" << '\n';
}
std::cout << num1 << " and " << num2 << " are prime numbers" << '\n';
}
inFile.close();
return 0;
}
prime.txt
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
I would like the program to fail if none of the input were equal to any of the numbers in the .txt file or display the inputs if it were true.