samedi 30 décembre 2017

C++ code executes in VS 2017 but does not in VSCode

I have this simple C++ code. It executes correctly in Microsoft Visual Studio 2017.

// Author:    Herbert Schildt 
// Modified:  Qiang Hu
// File name: ~ftp/pub/class/cplusplus/Array/Array3.cpp
// Purpose:   Use sqrs[][] -- two dimentional integer
//        array to store integers from 1 to 10 and
//        their squares.  
//            Ask user for a number, then look up this
//        number in the array and then print out its
//            corresponding square.

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  // C++ allows for the initialization of arrays.  In the following,
  // we are initializing a 10x2 integer array. After initialization,
  // sqrs[0][0] = 1
  // sqrs[0][1] = 1
  // sqrs[1][0] = 2
  // sqrs[1][1] = 4 
  // and so on
vector<vector<int> > sqrs = { {1, 1}, 
                    {2, 4}, // The square of 2 is 4,and so on
                    {3, 9}, 
                    {4, 16}, 
                    {5, 25}, 
                    {6, 36}, 
                    {7, 49}, 
                    {8, 64}, 
                    {9, 81}, 
                    {10, 100} 
                  };    

  int i, j;

  cout << "Enter a number between 1 and 10: ";
  cin >> i;

  // look up i
  for(j = 0; j < 10; j++)
    if(sqrs[j][0] == i) break; // break from loop if i is found 
  cout << "The square of " << i << " is " ;
  cout << sqrs[j][1] << endl;

  return 0;
}

I tried to use VSCode under windows to compile the same code but the code does not execute. It gives me the following error.

Untitled-2.cpp: In function 'int main()':
Untitled-2.cpp:34:19: error: in C++98 'sqrs' must be initialized by constructor, not by '{...}'
                   };
                   ^
Untitled-2.cpp:34:19:   required from here
Untitled-2.cpp:34:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11

I guess that the compiler that VSCode is using is not C++11 but is C++98. If this is the case how can I change the compiler to C++11. (Here, I was looking for a solution but never find something useful.) If not, how to fix this problem?

Aucun commentaire:

Enregistrer un commentaire