0

When i choose switch case 1 and entered the details , it's showing me ! Segmentation fault. I feel like it's because of the array input. but how to avoid that or is there anyway tackle the issue ? Question - Based on Problem 1 and 2 above, modify the program so that it will display a menu for the user to choose either to; Add new students Display student list Add new course Display Course Offered The user can choose to safely exit/terminate the program. Problem 4 Based on Problem 1 until 3 above, modify the program so that the user will be able to modify the name of the selected student.

#include <iostream>

#include <string>

using namespace std;

struct  Student{

    string name , id , nickname;



    };
struct  Course{

    string ccode,  cname, clecturer;



    };



int main() {
     int i=0, c=0, ti=0, tc=0;
     Student uniten[i];
     Course cuniten[c];

    cout << "\n \n" << endl;


int choice;
bool gameOn = true;
while (gameOn != false){
cout << "*******************************\n";
cout << " 1 - Add new students.\n";
cout << " 2 - Display student list.\n";
cout << " 3 - Add new course.\n";
cout << " 4 - Display course offered.\n";
cout << " 5 - Exit.\n";
cout << " Enter your choice and press return: ";

cin >> choice;

switch (choice)
{
case 1:
cout << "Add new students.\n";
i = i+1;
    cout << "\n \n" << endl;

    cout << "Student " << i << endl;

    cout << "Enter Student ID: ";

    cin >> (uniten[i].id);

    cout << "Enter their Name: ";

    cin >> uniten[i].name;

    cout << "Enter their Nickname: ";

    cin >> uniten[i].nickname;
cout << "\n \n" << endl;

break;
case 2:
cout << "Display Student List\n";

break;
case 3:
cout << "Add new course.\n";
c = c+1;

    cout << "\n \n" << endl;

    cout << "Course " << c << endl;

    cout << "Enter Course Code: ";

    cin >> (cuniten[c].ccode);

    cout << "Enter Course Name: ";

    cin >> cuniten[c].cname;

    cout << "Enter Lecturer Name: ";

    cin >> cuniten[c].clecturer;



break;

case 4:
cout << "Display Course List\n";
 for (tc=0; tc<c; tc++) {

    cout << "Course : " << cuniten[i].cname << " Course Code : " << cuniten[i].ccode << ", Lecturer name : " << cuniten[i].clecturer <<endl;

    cout << "\n \n" << endl;

    }
break;
case 5:
cout << "End of Program.\n";
gameOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}

}


}
Satz
  • 1
  • My answer to another question on SO might help with your problems. https://stackoverflow.com/a/58756962/434551. – R Sahu Nov 17 '19 at 06:02
  • `Student uniten[i]; Course cuniten[c]; ` could cause a major disaster for a couple reasons. The first is that C++ doesn't allow 0 length arrays. The second is that it doesn't allow arrays allocated at runtime with that syntax. You need a `std::vector` or pointer (though this isn't preferred) for dynamic arrays. –  Nov 17 '19 at 07:23

1 Answers1

1
int i=0, c=0, ti=0, tc=0;
     Student uniten[i];
     Course cuniten[c];

In the above code array has been created with size i=0 and c = 0 respectively.

case 1:
cout << "Add new students.\n";
i = i+1;
    cout << "\n \n" << endl;

    cout << "Student " << i << endl;

    cout << "Enter Student ID: ";

    cin >> (uniten[i].id);

    cout << "Enter their Name: ";

    cin >> uniten[i].name;

    cout << "Enter their Nickname: ";

    cin >> uniten[i].nickname;
cout << "\n \n" << endl;

In the switch case you are updating i=i+1 which is trying to access the index at uniten[1] which does not exist.

Somil Garg
  • 474
  • 4
  • 12
  • 1
    Even more worse: `int i = 0 /* or any other value */; Student uniten[i];` uses a [Variable Length Array](https://en.wikipedia.org/wiki/Variable-length_array) which is optional in C99 standard but not in C++. Some compilers support it in C++ as well (as proprietary extension). However, it shouldn't be used in C++. (A `std::vector` is the sufficient replacement.) And, an array with length 0 makes it even more more worse... – Scheff's Cat Nov 17 '19 at 06:55
  • Thank you , i got it ! But how to declare size based on the number of user input. I cannot get that part. – Satz Nov 17 '19 at 16:26
  • You should use std::vector instead of array – Somil Garg Nov 17 '19 at 18:15