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