配列を使った入力

このQ&Aのポイント
  • 配列を使った入力のプログラムでエラーが発生しています。
  • `test[b][a] = c;`の部分が問題の原因です。
  • 配列の使い方が間違っている可能性があります。エラーの理由を教えてください。
回答を見る
  • ベストアンサー

配列を使った入力

配列を使った入力 下記プログラムで sample9.4.cpp: function 内の `int main()': sample9.4.cpp:18: no match for `std::istream& << int&' operator というエラーがでてしまいます。 たぶん test[b][a] = c; という部分が悪いと思うのですが、 なぜ悪いのか分かりません。 配列の使い方が間違っているのでしょうか。 どなたか教えて頂けると嬉しいです。 ーー #include <iostream> using namespace std; int main() { int a,b,c,sub,num; cout << "Please input the number of subjects\n"; cin >> sub; cout << "Please input the number of people\n"; cin >> num; int test[b][a]; for(int b=0; b<sub; b++){ for(int a=0; a<num; a++){ cout << "Please input the point of the " << a+1 << "th people of the " << b+1 << "th subject\n"; cin << c; test[b][a] = c; } } for(b=0; b<sub; b++){ for(int a=0; a<num; a++){ cout << "The point of the " << a+1 << "th people of the " << b+1 << "th subject is " << test[a][b] << '\n'; } } return 0; }

質問者が選んだベストアンサー

  • ベストアンサー
  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.2

C++では > int test[b][a]; のように、添字に変数を使っての配列宣言はできません。 あと、配列の大きさが入っている変数は、sub、 numですよね? 動的に配列を確保するには、new演算子を使用します。 2次元配列の場合、1度に int **test=new int[sub][num] ; とすることはできず、第1の添字用の領域を int **test=new int*[sub] ; で確保して、 for( b=0;b<sub;++b) { test[b]= new int[num] ; } と各test[b]に対して確保します。 使い終わったら、delete[]で解放します。その際は、確保とは逆の順番で行います。 for( b=0;b<sub;++b) { detele[] int[num] ; } delete[] test あるいは、std::vector等、配列に相当するクラスを使用します。 ちなみに、そのエラーはここではなく、 > cin << c; でcinに対して << を使っているためのものです。 std::istream型への参照 << int型への参照 という演算に該当するものが無い、とちゃんとエラーメッセージに書いてあります。

ishigamin
質問者

お礼

親切にご回答頂き、誠にありがとうございます。 下記質問者の方の回答のお礼にあるように、無事動きました。 new演算子というものを使わなくても動いたのですが、 新しい知恵を授けていただきありがとうございます。

その他の回答 (1)

  • azkurw
  • ベストアンサー率33% (41/124)
回答No.1

>test[b][a] = c; という部分が悪いと思うのですが 一つ上の行(Line:17)の 「<<」 の向きが逆では? あと、2次元配列のtestの宣言について。(Line:13) a,bを使っていますが、それ以前に、a,bが初期化されていませんよ。 要素数が不確定だから動かないと思う。

ishigamin
質問者

お礼

ご回答頂き、誠にありがとうございます。 おっしゃるとおり、<< の向きが逆でした。 また、testの宣言でa,bでなくsubとnumを使ったら無事うまく動きました。

関連するQ&A

  • 配列のプログラムですが

    #include <iostream> using namespace std; int main() { int a[100],b=-9999; int i=0,j; do { cout << "整数値を入力してください\n"; cin >> a[i]; b += a[i]; i++; }while( a[i-1] != 9999); cout << b << '\n'; for(j=0;j<i-1;j=j+1) cout << a[j] * 3 << '\n'; return 0; } このプログラムってどんな計算をしてるんですか?誰か分かる人いますか 整数値を入れてくださいって出るだけなんですが

  • ややこしいコード

    #include<iostream> using namespace std; int main() { const int num = 5; int test[num]; cout << num << "人の点数を入力して下さい。\n"; for (int i = 0; i < num; i++) { cin >> test[i]; } for (int s= 0; s < num - 1; s++) { for (int t = s + 1; t < num; t++) { if (test[t] > test[s]) { int tmp = test[t]; test[t] = test[s]; test[s] = tmp; } } } for (int j = 0; j < num; j++) { cout << j + 1 << "番目の人の点数は" << test[j] << "です。\n"; } return 0; } 配列を並べ替える(ソート)する練習コードなんですが、 ちょっとややこしいので解りやすく教えて欲しいです。 因みに点数は、 22,80,57,60,50の順でお願いします。

  • 点数の大きい順に出力する

    #include <iostream> using namespace std; int main() { const int num = 5; int test[num]; cout << num << "人の点数を入力して下さい。\n"; for(int i=0; i<num; i++){ cin >> test[i]; } } } } for(int j=0; j<num; j++){ cout << j+1 << "番目の人の点数は" << test[j]<< "です。\n"; } return 0; } ---------- の for(int s=0; s<num-1; s++){ for(int t=s+1; t<num; t++){ if(test[t] > test[s]){ int tmp = test[t]; test[t] = test[s]; test[s] = tmp; の部分が理解できません。 s<num-1 は 0<4; ということでしょうか? 値が5つ入力されその値が if(test[t] > test[s]){ にどの様に挿入されるのかが 解りません。  

  • 設定した値が意図せぬ値に

    POJ 3176の問題です。 http://poj.org/problem?id=3176 #include <iostream> #include <algorithm> #define MAX 100 using namespace std; int main() { int n; cin >> n; int line[n-1][MAX]; int num[n-1][MAX]; cin >> line[0][0]; if(n==0) { cout << 0 <<endl; return 0; } else if(n==1) { cout << line[0][0] << endl; return 0; } for (int i =1;i < n;i++) { for (int j=0;j < i+1;j++) { int x; cin >> x; line[i][j]= x; } } for (int k= 0 ; k<n;k++) { num[n-1][k]= line [n-1][k]; } for (int k= n-2; k > 0 ; k--) { for (int l=0 ; l<k+1; l++) { num[k][l] = max (num[k+1][l],num[k+1][l+1]) + line[k][l]; } } num[0][0] = max(num[1][0],num[1][1]) + line[0][0]; cout << line[0][0] <<" "<<num[0][0]<<endl; return 0; } 入力 4 3 1 3 1 2 3 1 3 4 5 出力 1 12 最後に出力でline[0][0]をするようにしているのはバグチェックのためです。 ここで僕がわからないのはどうしてline[0][0]が3で宣言し、ほかでいじっていないにも関わらず、最後に1になっているのかということです。 どなたかわかる方がいらっしゃったらよろしくお願いします。

  • 配列での終了条件

    教えて頂きたいことは、 MAX=5として整数を入力して配列を作りたいのですが、 入力が5個以下でも0をインプットすると入力が終わる ようにするにはどうしたらいいのでしょうか? int list[MAX]; cout << "Please insert integers (0 to end)"; for (int i=0, i < MAX, i++) cin >> list[i]; これでは、5個の整数の入力が終わるまでループして しまいます・・・

  • linuxでリンクを貼るには

    linuxでリンクを貼るには 本に書いてある通りにプログラムをつくったのですが、 うまくコンパイルできません。 本で書いてあるのはwindowsのvisual C++で、 僕の使っている環境はlinuxのg++です。 コンパイルした結果を以下に示します。 -- ファイル名:myfunc.h //declaration of the max function int max(int x, int y); -- ファイル名:myfunc.cpp //a definition of the max function int max(int x, int y) { if(x > y) return x; else return y; } -- ファイル名:sample10.5.cpp #include <iostream> #include "myfunc.h" using namespace std; int main() { int num1, num2, ans; cout << "Please input the 1st integer.\n"; cin >> num1; cout << "Please input the 2nd integer.\n"; cin >> num2; ans = max(num1, num2); cout << "The maximum value is " << ans << '\n'; return 0; } -- bash-2.05b$ g++ myfunc.cpp /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start': : undefined reference to `main' collect2: ld はステータス 1 で終了しました -- bash-2.05b$ g++ sample10.5.cpp /tmp/ccWJNOkX.o(.text+0x6c): In function `main': : undefined reference to `max(int, int)' collect2: ld はステータス 1 で終了しました

  • icpcの過去問

    問題→ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1147&lang=jp 自分の回答→ #include <iostream> using namespace std; int numbers[105]; int order(int n,int num){ if(num==0){numbers[0]=n;} else{ for(int i=0;i<num;i++){ if((numbers[num-(i+1)])>n){//cout << "here1"; numbers[num-i]=numbers[num-(i+1)];//cout << "here2"; numbers[num-(i+1)]=n;//cout << "here3"; } } } return 0; }//昇順に並べる int main(){ int n; while(cin>>n,n){ int count,answer; for(int i=0;i<n;i++){int Numb; cin >> Numb;if(i==2){cout << "here";} order(Numb,i); count++; }//昇順に要素が並んだ for(int i=1;i<count-1;i++){ answer += numbers[i]; } answer = answer/(count-2); cout << answer << endl; } } 実行結果→ 3 7 6 5 Bus error となってしまいます。最後の入力の時に、cinされずにbuserrorになるのですがなぜなのでしょうか。

  • ところどころ理屈の解らない記述

    #include<iostream> using namespace std; int main() { const int num = 5; //変数numを5の値で初期化、constを使うから値の変更はない int test[num]; //int型の配列testを初期化 cout << num << "人の点数を入力してください。\n"; //変数numを出力して点数入力を促す for (int i = 0; i < num; i++) { //int型のiを0で初期化 numよりiが小さい場合 iを1増やす cin >> test[i]; //上のfor文によって配列の点数を5回入力 } for (int s = 0; s < num - 1; s++) {  for (int t = s + 1; t < num; t++) { if (test[t] > test[s]) { int tmp = test[t]; test[t] = test[s]; test[s] = tmp; } } } for (int j = 0; j < num; j++) { cout << j + 1 << "番目の人の点数は" << test[j] << "です。\n"; } return 0; } 解るところは、コメントしてあります。 それ以外で解らないところがあるので、(特にnumの後ろにつく-1の意味が解らない) どうかご回答お願いします。

  • C++のプログラムが走りません。

    <<条件は以下です。>> データメンバ x:xの座標(ただし 0-1000とする) y:yの座標(ただし 0-1000とする) z:zの座標(ただし 0-1000とする) メンバ関数 void setX(int a): xの座標を設計する(範囲外の値は、error messageを出力する) void setY(int b): Yの座標を設計する(範囲外の値は、error messageを出力する) void setZ(int c) Zの座標を設計する(範囲外の値は、error messageを出力する) int getX(): Xの座標値を得る int getY(): Yの座標値を得る int getZ(): Zの座標値を得る << 不明な点は、 if文で、どう戻るかがわからないことと、 最後の結果表示の部分です。 以下が、作成したプログラムです。ネットで検索して類似しているプログラムを参考にしたんですが。 よろしくお願いします。>> #include <iostream> using namespace std; class Zahyou{ private: int getX();//Xの座標値を得る int getY();//Yの座標値を得る int getZ();//Zの座標値を得る int a,b,c; public: void setX(int a);//Xの座標を設計する void setY(int b);//Yの座標を設計する void setZ(int c);//Zの座標を設計する void show(); }; void Zahyou::show(){ cout << "The result is: (" << a << "," << b << "," << c << ")"; } void Zahyou::setX(int a){ cout << " Please Input X " << "\n "; cin >> a ; if(a < 0 && a > 1000){ cout << "The value of X is a mistake, Please input X again! " << " \n "; } } void Zahyou::setY(int b){ cout << "Please Input Y " << "\n "; cin >> b ; if(b < 0 && b > 1000){ cout << "The value of y is a mistake, Please input X again! " << " \n "; cin >> b ; } } void Zahyou::setZ(int c){ cout << " Please Input Z " << "\n "; cin >> c ; if(c < 0 && c > 1000){ cout << "The value of Z is a mistake, Please input X again! " << " \n "; cin >> c ; } } int main(){ Zahyou zahyou1; zahyou1.getX(); zahyou1.getY(); zahyou1.getZ(); zahyou1.show(); return 0; }

  • 合計値を求める関数

    #include<iostream> using namespace std; //sum関数の定義 int sum(int x, int y) { return x + y;  } int main() { int num1, num2, ans; cout << "1番目の整数を入力して下さい。\n"; cin >> num1; cout << "2番目の整数値を入力して下さい。\n"; cin >> num2; ans = sum(num1, num2); cout << "合計は" << ans << "です。\n"; return 0; }  ここのreturn x+y;の所の合計値を戻り値として返す処理の仕組みを解りやすく教えて欲しいです、戻り値はちょっと解りづらいです、よろしくお願いします。