Thread: Try Catch

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    14

    Try Catch

    I am learning to use the Try Catch Throw but I am having a hard time getting all of the throws to work. I have to throw each of my functions with a string of Bad Major if a Major is 0. I can get only one of my functions to work. If anyone has a suggestions I would appreciate it.

    Thanks,
    R.

    Here is my code:

    Code:
    #include <iostream>
    #include<string>
    #include<iomanip>
    
    using namespace std;
    
    
    struct Student
    {
        char Name[30];
        float GPA;
        int Major;
    };
    
    
    Student StudentData(Student &s);
    void ChangeData(Student *s);
    void GetStudents(Student st[], short size);
    void PrintStudents(Student st[], short size);
    
    
    int main()
    {
        //#1 create 2 instances of that structure. Call them S1 and S2. 
        Student S1;
        Student S2;
    
        try
        {
        // S2 = StudentData( S1 ); //this is the call to the function
        S2 = StudentData(S1);
    
    
        //#3. print the data stored in the structures S1 and S2 using cout.
        cout << endl;
        cout << "Structor Student S2 info " << endl;
        cout << "Student Name:" << S2.Name << endl;
        cout << "Student GPA: " << S2.GPA << endl;
        cout << "Student Major: " << S2.Major << endl;
    
        cout << endl;
    
        cout << "Structor Student S1 info " << endl;
        cout << "Student Name: " << S1.Name << endl;
        cout << "Student GPA: " << S1.GPA << endl;
        cout << "Student Major: " << S1.Major << endl;
    
    
        cout << endl;
    
        
        //#4. Call a function named ChangeData with a pointer to S2 as the argument:
        //        ChangeData( &S2 ); //this is the call to the function
    
        ChangeData(&S2);
        
        
        //#5. Back in main print the data stored in the structure S2 using cout.
        cout <<"The student's name from ChangeData is: " << S2.Name << endl;
        cout <<"The student's GPA from ChangeData is: " << S2.GPA << endl;
        cout <<"The student's Major from ChangeData is: " << S2.Major << endl;
    
    
        cout << endl;
    
        //#6. Create an array of 2 structures in main. Call the array Students.
        Student Students[2];
    
        //#8. Call the function GetStudents from main.
        GetStudents(Students, 2);
    
    
        cout << endl;
    
        cout << "Here are the students from the GetStudents Function " << endl;
        cout << "====================================================" << endl;
        cout << "Number" << setw(20) << "Student Name" << setw(11) << "GPA" << setw(9) << "Major" << endl;
    
        //#9. Call the function PrintStudents
        PrintStudents(Students, 2);
        }
        catch(char *e)
        {
            cout << e << endl;
        }
       
        cout << endl;
        
    
        system("pause");
        return 0;
    }
    
    
    
    //#2. Create and call a function named StudentData:
    //   S2 = StudentData( S1 ); //this is the call to the function
    //The function receives 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 purposes, 
    //change the data in S1 so that the GPA is 3.5 and the Major is 2. 
    //Since you are to use cins for getting data from the user, you are the user 
    //and just enter these values. After the call to the function both S1 and S2 
    //will contain the same data.
    
    Student StudentData(Student &s)
    {
    
        if(s.Major == 0)
        {
            throw "Bad Major from StudentData function";
        }
        else
        {
        
        //Get the Student Name
        cout << "Enter the student name: ";
        cin.getline(s.Name, 31);
    
        //Get the Student GPA
        cout << "Enter the student GPA: ";
        cin >> s.GPA;
    
        //Get the Student Major
        cout << "Enter the student Major: "; 
        cin >> s.Major;
    
        cin.ignore();
        }
        
        return s;
        
    }
    
    //#4 . Call a function named ChangeData with a pointer to S2 as the argument:
    //     ChangeData( &S2 ); //this is the call to the function
    //Change the data in S2 so that the GPA is 3.0 and the Major is 1. 
    //(Using these values for testing…)
    void ChangeData(Student *s)
    {
    
        if(s->Major == 0)
        {
            throw "Bad Major from ChangeData function";
        }
        else
        {
        s->GPA = 3.0;
        s->Major = 1;
        }
    
    }
    
    //#7. Create a function, GetStudents, which will receive the array and an int 
    //representing the number of elements(2). In the function, loop through the data 
    //and get all three fields from the user using cin, cin.getline and cout statements. 
    //Organize like this: 
    //                  for (...........) 
    //                  { 
    //                        cout prompt to user 
    //                         cin.getline for name 
    //                         cout prompt to user 
    //                         cin for GPA
    //                         cout promp to user
    //                         cin for Major 
    //                          cin.ignore(1); 
    //                      }
    //The problem is that a cin for a numeric value 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. cin.ignore should handle this for us.
    void GetStudents(Student st[], short size)
    {
    
        for(int cnt = 0; cnt < size; cnt++)
        {
            
            if(st[cnt].Major == 0)
            {
            throw "Bad Major from ChangeData function";
            }
            else
            {
    
            cout << "Enter the student's name " << endl;
            cin.getline(st[cnt].Name, 30);
    
            cout << "Enter the student's GPA " << endl;
            cin >> st[cnt].GPA;
    
            cout << "Enter the student's Major " << endl;
            cin >> st[cnt].Major;
            }
    
            cin.ignore();
    
        }
    
    }
    
    
    //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.
    void PrintStudents(Student st[], short size)
    {
        for(int cnt = 0; cnt < size; cnt++)
        {
            cout <<"#" << (cnt + 1) << setw(25) << st[cnt].Name << setw(10) << st[cnt].GPA << setw(7) << st[cnt].Major << endl;
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Instead of throwing a raw char pointer, you should use the predefined exception types like std::exception and its derivatives.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    Thank you Elkvis for your quick reply. I am not sure about the std:: exception and its derivatives are. All I have been shown is what I have in my code. If you can expand on what you are taking about I would appreciate it.

    Thanks.
    R.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try googling for "std exception".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    Thanks everyone. I finally got my code to work. I had the throw in the wrong area of the functions.
    Thanks,
    R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. catch()
    By Tool in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2010, 06:42 AM
  2. try & catch
    By kawafis44 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2008, 04:04 PM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. try/catch
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2005, 12:44 AM
  5. how can I use try catch?
    By Tonyukuk in forum C# Programming
    Replies: 1
    Last Post: 05-09-2003, 12:02 AM