-1

I'm trying to read and write some files, but i get "ERROR C1001, internal error happened in compiler" every time i try to std::cout something to my output.out file.

Why ?

(i used _CRT_SECURE_NO_WARNINGS in preprocesssor definition to be able to use freopen())

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <deque>
#include <array>
#include <vector>

freopen("C:\\somepath\\input.in", "r", stdin);
freopen("C:\\somepath\\output.out", "w", stdout);

int t;
std::cin >> t;
std::cout << t << std::endl;
for (int i = 0; i < t; i++)
{
    int n;
    std::cin >> n;
    std::cout << t << std::endl;
    std::vector <int> x, h;
    x.resize(n);
    h.resize(n);
    for(int j=0;j<n;j++)
    {
        std::cin >> x[j] >> h[j];
        std::cout<< x[j] << h[j] << std::endl;
    }
}

EDIT : as Nighteen said, i had some errors in my code (n++, no vector resizing, now corrected) But the bug, still there in some way : The code is compiling in this state, but as soon as i try put cout a string, the same probleme appears like adding <<" " in my std::cout<< x[j] << h[j] << std::endl;

in the std::cout<< x[j] <<" "<< h[j] << std::endl;
NanBlanc
  • 127
  • 1
  • 12

2 Answers2

1

The code compiles fine in MSVC 15.5.2 assuming you put the code in a main block. Proof here.

Even though the compilation is OK, the code seems not to work out as it should. First of all, you can't std::cin >> x[j] >> h[j];. Create a temporary variable to store the input and then push it back into the vector:

int input1, input2;
std::cin >> input1 >> input2;

x.push_back(input1);
h.push_back(input2);

You should also note that this loop for (int j = 0; j < n; n++) never ends. The variable j should be increasing, not n. Whatever you are trying to achieve, this does not seem to be the way.

Nighteen
  • 731
  • 1
  • 7
  • 16
0

std::cout is a std::ostream used for outputting to the console. For outputting to files, used the classes std::ifstream and std::ofstream. Use the following syntax:

std::ifstream [ifstream_name](constructor parameters);
[ifstream_name] >> [container to store file contents];

std::ofstream [ofstream name](constructor parameters);
[ofstream_name] << [contents to write to file];
me'
  • 494
  • 3
  • 14