-1

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.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    `if (cin.fail)` This case will never be true. If the read failed the loop will not be entered. If it was though, why would a read failure lead to printing that there's a prime number? `else if (num2 = num)` assigns the value of `num` to `num2`, it doesn't compare them. Use `==` for that. – Retired Ninja Apr 01 '22 at 17:24
  • Off-topic: You could perform a simple check before reading the file: `if (num1 > 2) && ((num1 % 2) == 1) { /* Look up number in file */}` This will save you reading the file for 50% of the numbers since numbers greater than 2 are odd. – Thomas Matthews Apr 01 '22 at 17:31
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Apr 01 '22 at 17:36
  • Remember `if (num2 = num) {` is an assignment. It sets num2 to the value of num then checks if the value is not zero. – drescherjm Apr 01 '22 at 20:46

1 Answers1

2

My recommendation here would be to:

  • Read the whole file into a vector of numbers.
  • If you are not sure the file contents are sorted, sort the vector.
  • For each number input by the user, binary search it in the vector of numbers.

I.e. make use of the STL data and algorithms that you have handy.

The code below is an example, 1) using an istringstream instead of an ifstream, and 2) checking a few random numbers instead of user input.

[Demo]

#include <algorithm>  // binary_search
#include <fmt/ranges.h>
#include <iterator>  // istream_iterator
#include <sstream>  // istringstream
#include <vector>

int main()
{
    std::istringstream iss{"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"};
    std::vector<int> ps{std::istream_iterator<int>{iss}, {}};
    std::ranges::sort(ps);
    fmt::print("{}\n\n", ps);
    for (auto&& n : std::vector<int>{1, 2, 3, 4, 5}) {
        if (std::binary_search(std::cbegin(ps), std::cend(ps), n)) {
            fmt::print("{} is a prime number\n", n);
        } else {
            fmt::print("{} is not a prime number\n", n);
        }
    }
}

// Outputs:
//
//   [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]
//   
//   1 is not a prime number
//   2 is a prime number
//   3 is a prime number
//   4 is not a prime number
//   5 is a prime number
rturrado
  • 7,699
  • 6
  • 42
  • 62