@ixabellol (ず す)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

コアダンプの原因を知りたいです

Q&A

Closed

解決したいこと

学校の課題をやっているのですが、実行するとコアダンプになりました。
解決方法を教えて下さい。

発生している問題・エラー

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
中止 (コアダンプ)

該当するソースコード

    1  #include<iostream>
     2  #include<string>
     3  #include<cstring>
     4  using namespace std;  //名前空間
     5
     6  class People{
     7  public:
     8    string name;      //名前
     9    string address;   //住所
    10    int age;          //年齢
    11  public:
    12    People(string newname, string newaddress, int newage){ //constractor
    13      name = newname;
    14      address = newaddress;
    15      age = newage;
    16    }
    17    virtual void ShowInformation(){};  //仮想関数の宣言
    18    virtual ~People(){}                //仮想デストラクタ
    19  };
    20
    21  class Student: public People{
    22  public:
    23    string stID;          //学生ID
    24    int eng, math, jan;   //生徒個人の英語、数学、国語の成績
    25    int sum, avg;         //     総得点、平均点
    26  public:
    27    Student(string newstID,int neweng,int newmath,int newjan,string name,string address,int age):People(name,address,age){
    28      stID = newstID;
    29      eng  = neweng;
    30      math = newmath;
    31      jan  = newjan;
    32    }
    33    void ShowInformation(){
    34      cout << stID << "\t\t" << eng <<
    35        "\t\t" << math << "\t\t" << jan <<endl;
    36    }
    37  };
    38
    39  class Teacher: private People{
    40  protected:
    41    string tcID, dep;                            //教員ID、所属学科
    42    double avg_eng, avg_math, avg_jan, avg_sum;  //各科目の平均点、全員総得点の平均値
    43  public:
    44    void ShowInformation();
    45    void print();
    46  };
    47
    48
    49
    50
    51  int main(){
    52    int i,eng,math,jan,age;
    53    int judge;
    54    string passInput;
    55    string password = 0000;
    56    string stID, tcID,name,address;
    57
    58    Student stu[10]={
    59      Student(stID, eng, math, jan, name, address, age),
    60      Student(stID, eng, math, jan, name, address, age),
    61      Student(stID, eng, math, jan, name, address, age),
    62      Student(stID, eng, math, jan, name, address, age),
    63      Student(stID, eng, math, jan, name, address, age),
    64      Student(stID, eng, math, jan, name, address, age),
    65      Student(stID, eng, math, jan, name, address, age),
    66      Student(stID, eng, math, jan, name, address, age),
    67      Student(stID, eng, math, jan, name, address, age),
    68      Student(stID, eng, math, jan, name, address, age)
    69    };
    70
    71    //この辺に配列の初期値入力 生徒だと未定義の配列が呼び出される?
    72
    73    cout << "あなたは先生ですか?学生ですか?" <<endl
    74         <<  "数字で入力してください" <<endl << "1,先生 2.学生" <<endl;
    75    cin >> judge;
    76
    77    if(judge = 1){
    78      cout<< "パスワードを入力してください" <<endl;
    79      cin >> passInput;
    80      if(passInput ==password){
    81        cout << "点数の登録または修正をします。" <<endl;
    82
    83        for(i=0; i<10; i++){
    84          cout << "学生ID" << stu[i].stID << "のデータを入力";
    85          cin  >> stu[i].stID;
    86          cout << "英語の点数>>";
    87          cin  >> stu[i].eng;
    88          cout << "数学の点数>>";
    89          cin  >> stu[i].math;
    90          cout << "国語の点数>>";
    91          cin  >> stu[i].jan;
    92        }
    93      }else{
    94        cout << "パスワードが違います";
    95      }
    96    }else if(judge = 2){
    97      cout<< "学生IDを入力してください"<<endl;
    98
    99      //未実装
   100
   101    }else{
   102      cout<< "error!1,2のいずれで入力ください"<<endl;
   103    }
   104
   105    /*
   106    for(i=0; i<10; i++){
   107    cout <<stu[i].stID <<"\t" <<stu[i].eng <<"\t" <<stu[i].math <<"\t" <<stu[i].jan<<endl;
   108    }
   109    */
   110
   111    return 0;
   112  }
0 likes

2Answer

string password = 0000;

文字列は両側を"で囲まないと駄目。

string password = "0000";
1Like

Comments

  1. @ixabellol

    Questioner

    無事解決しました。ありがとうございます!
int i,eng,math,jan,age;
...
string stID, tcID,name,address;

Student stu[10] = {
  Student(stID, eng, math, jan, name, address, age),
  ...
}

何となくですが、未初期化のローカル変数使ってるからとか
int型については、ゴミ数字が入るだけで済むでしょうが、string型はそうはいかなそう

0Like

Your answer might help someone💌