I have searched this up and tried multiple launch.json and tasks.json configurations and nothing seems to work. The program that I wrote in c++ works when I read from console, so the issue isn't with the original code. I have tried with multiple programs and never have I been able to get it to read from a file. I was hoping you could help. This is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
],
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
And this is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\CodeBlocks\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\CodeBlocks\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
For example, a file "bac.in" contains the numbers 1 7 2 1 1 2 1 7 2.
When running this program, the console shows 0 instead of 144:
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("bac.in");
int a[10];
int main()
{
int S=0, i;
while ( fin >> i )
{
if ( a[i] == 0 )
{
a[i]= 1;
S += 10;
}
S += i*i;
}
cout << S;
return 0;
}