9

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â
Christian Gardner
  • 237
  • 2
  • 3
  • 6

2 Answers2

12

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Please have a look at http://stackoverflow.com/questions/7124899/initializer-list-for-dynamic-arrays –  Feb 05 '14 at 14:21
  • Incomplete and buggy, even the latest builds still sometimes miss conformance on the standard when you try to push harder on c++11 new language features… Never trust the result of a single compiler to state that a code is well formed or not. – galop1n Feb 05 '14 at 14:22
  • @DieterLücking: Fair enough; I rarely use `new` directly, and only checked the syntax with one compiler. The answer should be more correct now. – Mike Seymour Feb 05 '14 at 14:26
  • `int *multi = new int{7,3,9,7,3,9,7,3};' is not what you expect: error: cannot convert ‘’ to ‘int’ in initialization –  Feb 05 '14 at 14:35
  • @DieterLücking: Indeed; which is why I corrected the answer to say "this form of initialisation still isn't valid" and suggested valid alternatives. – Mike Seymour Feb 05 '14 at 14:36
1

errors-:

1)there is an syntactical error in your 5th for loop which is you forgot to put ; after total+=b[N]

2)since your array multi is small and the value and size is predefined then initialize it with int multi[] = {7,3,9,7,3,9,7,3}; this way

suggestions to improve programme efficiency-: 1)instead of defining i variable 5 times for each for loop will give error in C/C++ so in order to avoid it you can declare i as a global variable

  • 1
    Please avoid using bold text for the code, try using backticks ` – Panwen Wang Jul 07 '20 at 18:07
  • Welcome Prakhar gupta! As @PanwenWang says, please consider using backtics instead of bold text for code. Also it will be useful to use markdown numbered lists to improve the formatting of your answer. – piraces Jul 07 '20 at 22:19