c++ - not reading file line by line -


so need read n numbers file , print on screen prime. code determine if number prime working after checks number doesn't want check other n numbers.

#include <iostream> #include <fstream> #include <conio.h>  using namespace std;   int main() {  int n = 0;  unsigned int x = 0;  cout << "n= "; cin >> n;  ifstream f("numbers.txt");  (int = 1; <= n; i++) {     int p = 1;     f >> x;          if (x % 2 == 0)         {             if (x == 2)                 cout << x << endl;         }         else         {             (int = 3; <= x / 2; i++)             {                 if (x%i == 0)                     p = 0;             }             if (p == 1)                 cout << x << endl;         }         cout << "i= " << << "x= " << x<<endl; }    f.close(); _getch(); return 0; } 

the file

53 34 65 234 756 342 988 997 1 2 97 234 87 234 867 37 234 

and test of output file since can't post images

53 i= 1x= 53 i= 2x= 34 i= 3x= 65 i= 4x= 234 i= 5x= 756 i= 6x= 342 i= 7x= 988 

you should read file way :

std::ifstream f("your_file_name.txt"); unsigned long int x; while(f >> x)  // 0 when value can't read (= @ end of file)   if(is_prime(x))     std::cout << x << std::endl; 

note it's not needed close file @ end of scope. destructor of f job.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -