OK so most can guess I am a student just now learning programming and starting out with C++.

I have tried many compilers and IDE's and have settled on Eclipse for now. I do not want to start a What compiler/IDE do you use, but I am trying to figure out each one I try as much as I can.

So I noticed that an assignment I was working on would compile and run on Microsoft Visual C++ 6.0 but not Eclipse.

The program was to teach us how to throw an exception. When Eclipse would hit that line in the code it would quit the program, but Visual C++ would run the program the whole way through.

here is the code (Sorry about the length)

//Written by Jason Stabins
//March 19, 2008
//Chapter 16
//Programming Challange throw

#include<iostream>
#include<iomanip>
using namespace std;

//declare global size for name
const int NAME_SIZE = 30;

//Hold student info
struct Student {
    char Name[30];
    float GPA;
    int Major;
};

//Prototypes
Student StudentData(Student &S1);
Student ChangeData(Student &S2);
Student GetStudents(Student students[], int SIZE);
Student printStudents(Student students[], int SIZE);

int main() {
    Student S1, S2; //Two student structs
    
    //exceptions
	cout << "Enter the first students information and I will copy it into";
	cout << " the second.\n DO NOT ENTER ZERO FOR THE MAJOR!\n\n";

    try
    {
    S2 = StudentData(S1);
        
    cout << "Student 1 name:\t\t" << S1.Name << endl;
    cout << "Student 1 GPA:\t\t" << S1.GPA << endl;
    cout << "Student 1 Major:\t" << S1.Major << endl;
    cout << "--------------------\n";
    cout << "Student 2 name:\t\t" << S2.Name << endl;
    cout << "Student 2 GPA:\t\t" << S2.GPA << endl;
    cout << "Student 2 Major:\t" << S2.Major << endl;
    }
    catch (char *exceptionString)
    {
        cout << exceptionString;
    }

	cout << "\nNow enter the data for student 2.\n AGAIN ZERO FOR MAJOR IS NOT ALLOWED!\n";
	cout << "-------------------------------------\n";
    try
	{
		
    S2 = ChangeData(S2);  //Change s2 data
    
    cout << "Student 2 name:\t\t" << S2.Name << endl;
    cout << "Student 2 GPA:\t\t" << S2.GPA << endl;
    cout << "Student 2 Major:\t" << S2.Major << endl;
    }
	catch (char *exceptionString)
    {
        cout << exceptionString;
    }
    //declare array
    const int SIZE = 2;
    Student students[SIZE];
    
	cout << "\nWe will now work with two arrays.\n";

	try
	{
    students[SIZE] = GetStudents(students, SIZE);
	cout << "\nHere is the students information from the arrays\n";
	cout << "-------------------------------------------------\n";
	printStudents(students, SIZE);
    }
	catch (char *exceptionString)
    {
        cout << exceptionString;
    }


	return 0;
}

Student StudentData(Student &S1) {
    
    cout << "Please enter the students name:";
    cin.getline(S1.Name, NAME_SIZE);
    cout << "Enter the students GPA:";
    cin >> S1.GPA;
    cout << "Enter the students Major:";
    cin >> S1.Major;
    cin.ignore();
    
    if (S1.Major == 0)
    {
        throw "Bad Major!\n ";
    }
    else
        return S1;
}
Student ChangeData(Student &S2) {
    
    cout << "Please enter the students name:";
    cin.getline(S2.Name, 30);
    cout << "Enter the students GPA:";
    cin >> S2.GPA;
    cout << "Enter the students Major:";
    cin >> S2.Major;
    cin.ignore();
    if (S2.Major == 0)
    {
        throw "Bad Major!\n ";
    }
    else
        return S2;

}
Student GetStudents(Student students[], int SIZE) 
{
    for(int i = 0; i < SIZE; i++)
    {
        cout << "Enter the name for the #" << i + 1 << " array input:";
        cin.getline(students[i].Name, NAME_SIZE);
        cout << "Enter the GPA:";
        cin >> students[i].GPA;
        cout << "Enter the major:";
        cin >> students[i].Major;
        cin.ignore();
    }
	if (students[2].Major == 0 || students[1].Major == 0)
    {
        throw "Bad Major!\n ";
    }
    else
	return students[SIZE];
}
Student printStudents(Student students[], int SIZE)
{
    for(int i = 0; i < SIZE; i++)
    {
        cout << "Name:" << students[i].Name << "\t\tGPA:" << students[i].GPA;
        cout << "\t\tMajor:" << students[i].Major << endl;
    }
	return students[SIZE];
}

So first, am I using the technique correctly? is there a problem with the code itself?

Or is it a compiler/IDE problem?

The good news is my professor uses Microsoft Visual to grade our assignments so at least the program will run all the way through when she grades it.

Thanks for your help!
Jay

Recommended Answers

All 2 Replies

why is Student.Major and integer ? why not a string to that you can enter CS (computer science) or any other major ?

VC++ 2008 Express runs without errors too. But that doesn't mean there are no errors in the program. Actually there are several errors -- buffer overruns. For example line 73 and 138 are accessing non-existant array elements.

Students.major is what the assignment called for. Seemed silly to me too but thats what she wanted.

here is the assignment

This program will include error trapping with try and catch.

Put a throw in each function which gets user input and throw a string "Bad Major" if a Major of 0 is entered. The input functions are specified in 2, 4 and 7 below.

Create a global structure as follows:

struct Student

{

char Name[30];

float GPA;

int Major;

};

1. Now in main create 2 instances of that structure. Call them S1 and S2.

2. Create and call a function called StudentData:

S2 = StudentData( S1 );

The function receive as a parameter a reference to the structure( prototyping will handle this) and will return a reference to the structure. Use couts and cins for getting data from the user. (For testing puposes, change the data in S1 so that the GPA is 3.5 and the Major is 2.)

3. In main print the data stored in the structures S1 and S2 using cout.

4. Call a function called ChangeData with a pointer to S2 as the argument:

ChangeData( &S2 );

Change the data in S2 so that the GPA is 3.0 and the Major is 1. (Using these values for testing…)

5. Back in main print the data stored in the structure S2 using cout.

6. Now create an array of 2 structures in main. Call the array Students.

7. Create a function, GetStudents, which will receive the array and an int representing the count(2). In the function loop thru the data and get all three fields from the user using cin, cin.getline and cout statements. Organize like this: for (...........) { cout and cin.getline for name cout and cin for GPA cout and cin for Major cin.ignore(1); } The problem is that a cin for a numeric will leave the ENTER key in the keyboard buffer and that is OK with cin and other numbers but not with strings, thus we must remove it on our own.

8. Call the function GetStudents from main.

9. Create a function, PrintStudents, which will receive the same arguments as GetStudents. It will print out the array of students on 2 lines, 1 line per student.

I thought that I had the arrays set up correctly, looks like I got some more reading to do.

I think the point of the exercise was just an intro to try/catch.

If you enter 0 for major will the program crash?

Thanks again!
Jay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.