in the file, the first number describes the number of numbers, the written program does not work. the program is compiled, but there is no result

#include <fstream> #include <iostream> #include <cstring> using namespace std; int main() { ifstream inp; inp.open("input.txt"); int N; inp>>N; int mass[N]; for(int i=0; i<N; ++i) { inp >> mass[i]; cout << mass[i]; } inp.close(); return 0; } 

Closed due to the fact that off-topic participants freim , andreymal , PashaPash February 17 at 16:27 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - freim, andreymal, PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    What error gives? - Komdosh
  • Do you have a file? Show the file (the first 10 stock is enough, a million lines are not needed). - nick_n_a 1:56 pm
  • There is no verification that the file has opened normally. - HolyBlackCat 1:57 pm

2 answers 2

Most likely the problem is that the working directory of the program is different from the one in which the input file is located.

    It is difficult to give a clear answer, focusing on the few data from the question (your version, by the way, works for me).

    In general, here, try:

     #include <fstream> #include <iostream> using namespace std; int main() { ifstream inp; inp.open("input.txt", ios::in); int N, t; inp >> N; if(inp.is_open()) { for(int i=0; i<N; ++i) { inp >> t; cout << t << '\n'; } } else { cout << "error: couldn't open file" << '\n'; } inp.close(); return 0; } 

    input.txt:

    10 1 2 3 4 5 6 7 8 9 10