Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extended initializer lists only available with

I'm very new to C++ and I'm having trouble reading my errors I was able to eliminate most of them but I'm down to a few and I'm request help on them please.

Here is the program

#include <string>
#include <iostream>
using namespace std;
int main(){
 int *bN = new int[9];
 string bankNum;
 int *number = new int[9];
 int total, remain;
 int *multi = new int{7,3,9,7,3,9,7,3};
 cout<<"Please enter the bank number located at the bottom of the check"<<endl;
 cin>>bankNum;
 for(int i = 0; i < 8; i++){
  bN[i]= (bankNum[i]-48);
 }
 for(int i = 0; i < 8;i++){
  cout<<bN[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  cout<<multi[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  bN[i] = bN[i] * multi[i];
  cout<< bN[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  total += bN[i]
  cout<<total;
 }
 cout<<endl;
 remain = total % 10;
 if(remain == (bankNum[9] - 48)){
  cout<<"The Number is valad"<<endl;
  cout<<remain<<endl;
 }
}

and the errors

wm018@cs:~$ c++ bankNum.cpp
bankNum.cpp: In function âint main()â:
bankNum.cpp:9:19: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
bankNum.cpp:9:38: error: cannot convert â<brace-enclosed initializer list>â to âintâ in initialization
bankNum.cpp:30:3: error: expected â;â before âcoutâ
like image 780
Christian Gardner Avatar asked Feb 05 '14 14:02

Christian Gardner


1 Answers

This style of initialisation, using braces:

int *multi = new int{7,3,9,7,3,9,7,3};

was introduced to the language in 2011. Older compilers don't support it; some newer ones (like yours) only support it if you tell them; for your compiler:

c++ -std=c++0x bankNum.cpp

However, this form of initialisation still isn't valid for arrays created with new. Since it's small and only used locally, you could declare a local array; this doesn't need C++11 support:

int multi[] = {7,3,9,7,3,9,7,3};

This also has the advantage of fixing the memory leak - if you use new to allocate memory, then you should free it with delete when you've finished with it.

If you did need dynamic allocation, you should use std::vector to allocate and free the memory for you:

std::vector<int> multi {7,3,9,7,3,9,7,3};

Beware that your version of GCC is quite old, and has incomplete support for C++11.

like image 57
Mike Seymour Avatar answered Oct 17 '22 02:10

Mike Seymour