2016-12-07 3 views
0

私はメモリを割り当てる方法を学んでいます。このコードは、ユーザーにいくつかの値を入力してからそれらの値の合計を出力するように促すものとします。しかし、コードは値を出力するだけです。合計を出力しません。私はこのコードを実行するためにECLIPSEを使用しています。実行するたびに、「このコードにはエラーがあります。起動しますか?」というメッセージが表示されます。私はエラーを見つけることができません。「C++で「memory.exeを割り当てる出力ファイルを開くことができません:パーミッションが拒否されました」をデバッグするには?

私が実行するたびに得られるメッセージです。 PSファイル名がメモリを割り当てています。ここで

15:12:29 **** Incremental Build of configuration Debug for project allocating memory **** 
Info: Internal Builder is used for build 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\allocating memory.o" "..\\src\\allocating memory.cpp" 
g++ -o "allocating memory.exe" "src\\allocating memory.o" 
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file allocating memory.exe: Permission denied 
collect2.exe: error: ld returned 1 exit status 

code.thanks

#include <iostream> 
    using namespace std; 

    int main() { 
     int i, j, sum = 0 ; 
     int *p; 

     cout<<"how many numbers do you want to input"<<endl; 
       cin>> i; 

      p =new (nothrow)int [i]; 
      if (p== nullptr){ 

       cout<<"Sorry not enough memory to allocate for this program"<<endl;} 
      else { 
       for (j=0; j<i; j++){ 
       cout<<"enter the numbers then "<<endl; 
       cin>> p[j];} 
      // to print the numbers 

       cout << "the numbers you entered are"<<endl; 
        for (j=0; j<i;j++){ 
         cout << p[j]<< "," << endl; 
         sum +=p[j]; 
         cout << "the sum of all the inputs are"<< sum ; 
        } 

        delete[] p; 
      } 
     return 0; 
    } 

は更新*****です***ここ

はこの質問へのより良い試みである、それが動作します。私は、ユーザーからの入力に動的にメモリを割り当て、それらの入力をプリントアウトし、それらの合計を見つけようとしています。ここにコードがあります。

#include <iostream> 
using namespace std; 

int main() { 
    int i, j, sum = 0 ; 
    int *p; 

    cout<<"how many numbers do you want to input"<<endl; 
      cin>> i; 

     p =new (nothrow)int [i]; 
     if (p== nullptr){ 

      cout<<"Sorry not enough memory to allocate for this program"<<endl;} 
     else { 
      for (j=0; j<i; j++){ 
      cout<<"enter the numbers then "<<endl; 
      cin>> p[j];} 
     // to print the numbers 

      cout << "the numbers you entered are"<< endl; 
       for (j=0; j<i;j++){ 
        cout << p[j]<< ","; 
        sum = sum + p[j]; 
       } 
       cout << endl; 
      cout << "The sum of these numbers is " << sum<< endl; 



       delete[] p; 
     } 
    return 0; 
} 
+1

コンパイルエラーとは何ですか? – MordechayS

+3

C++コードの何が間違っているかを伝えるのに本当に良い*ツールは何ですか?コンパイラ。あなたはそれが間違っていたとあなたは何を言ったのですか? –

+2

'= +'演算子はありません。 – molbdnilo

答えて

2

+ =演算子の前後にタイプミスがあります。

sum =+ p[j]; 

それは次のようになります。

sum += p[j]; 
+0

ありがとうございます!私は変更を行います – TINA15

関連する問題