Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,702 people online right now. Registration is fast and FREE... Join Now!




Using TRY BLOCKs PROBLEM HELP!

 
Reply to this topicStart new topic

Using TRY BLOCKs PROBLEM HELP!

bortiz0823
14 Mar, 2007 - 05:59 AM
Post #1

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 21


My Contributions
Hello, I want to know if somebody can help me in my code. My code is about create a class named RealState that has data members to hold the price, bedrooms and baths of a house. Write a main() function that instanties a RealState object, allows the user to enter the data and display the data members. The user should recieve an appropiate thrown eror message if negative values are entered for any of the data members.
My problem is when i used the TRY Block to compare and throw the values. All comments or sugerents are welcome. Thanks all!!!!
CODE
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

class RealState
{
private:
    double HousePrice;
    int BedRooms;        
    int Baths;
    
public:
     int dataEntry();
     void showdata();
};
int RealState::dataEntry()
{

    cout<<"PLEASE ENTER A PRICE OF A HOUSE: ";
    cin>>HousePrice;
    cout<<"NOW, ENTER THE NUMBER OF BEDTROOMS: ";
    cin>>BedRooms;
    cout<<"FINALLY, ENTER HOW MANY BATHS ARE: ";
    cin>>Baths;
    if (HousePrice<0 || BedRooms<0 || Baths<0)
        throw("*****!!INVALID ENTRY!!*****");
    return(HousePrice);//I dont know If this is correct(3return values)
    return(BedRooms);
    return(Baths);
}
void RealState::showdata()
{
    cout<<"\n********************"<<endl;
    cout<<"HOUSE PRICE: "<<HousePrice<<endl;
    cout<<"# OF BEDROOMS: "<<BedRooms<<endl;
    cout<<"# OF BATHS: "<<Baths<<endl;
    cin.get();
}

void main()
{
    int value;
    RealState HouseValue;
    HouseValue.dataEntry();
    
    
    try
    {
    value=RealState::dataEntry();//Here is wher I try to call a function to examined.
    }
    catch(const char msg[])
    {
        cout<<"^^^FATAL ERROR^^^"<<endl;
        cout<<msg<<endl;
    }
    HouseValue.showdata();
    cin.get();
}


User is offlineProfile CardPM
+Quote Post

msg555
RE: Using TRY BLOCKs PROBLEM HELP!
14 Mar, 2007 - 06:29 AM
Post #2

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 2 times
Dream Kudos: 25
My Contributions
I don't know a whole lot about using try blocks, but I don't see how you can throw a string array. It seems to me that that you would need to have a copy constructor available in the class you are throwing to throw it, I may be wrong. Try throwing a character pointer or a string instead. (like I said, I may be completly wrong)
User is offlineProfile CardPM
+Quote Post

horace
RE: Using TRY BLOCKs PROBLEM HELP!
14 Mar, 2007 - 08:18 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
after fixing a problem in main() your try {} catch works ok, e.g.
CODE

int main()
{
    int value;
    RealState HouseValue;
  //  HouseValue.dataEntry();   ** put in try block    
    try
    {
  //  value=RealState::dataEntry();// you need an object to call this
    value=HouseValue.dataEntry();// for example
    }
    catch(const char msg[])
    {
        cout<<"^^^FATAL ERROR^^^"<<endl;
        cout<<msg<<endl;
        system("pause");
    }
    HouseValue.showdata();
    cin.get();
}

when I run it with faulty input I get
.

********************
HOUSE PRICE: 50000
# OF BEDROOMS: 4
# OF BATHS: -3
^^^FATAL ERROR^^^
*****!!INVALID ENTRY!!*****
Press any key to continue . .

This post has been edited by horace: 14 Mar, 2007 - 08:19 AM
User is offlineProfile CardPM
+Quote Post

bortiz0823
RE: Using TRY BLOCKs PROBLEM HELP!
14 Mar, 2007 - 08:39 AM
Post #4

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 21


My Contributions
Ok Thanks a lot. I resolve my problem. I post it to others person w the same problem can resolve it. Thanks
CODE
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

class RealState
{
private:
    double HousePrice;
    int BedRooms;        
    int Baths;
    
public:
     int dataEntry();
     void showdata();
};
int RealState::dataEntry()
{

    cout<<"PLEASE ENTER A PRICE OF A HOUSE: ";
    cin>>HousePrice;
    cout<<"NOW, ENTER THE NUMBER OF BEDTROOMS: ";
    cin>>BedRooms;
    cout<<"FINALLY, ENTER HOW MANY BATHS ARE: ";
    cin>>Baths;
    if (HousePrice<0 || BedRooms<0 || Baths<0)
        throw("*****!!INVALID ENTRY!!*****");
    return(HousePrice);
    return(BedRooms);
    return(Baths);
}
void RealState::showdata()
{
    cout<<"\n***************************"<<endl;
    cout<<"HOUSE PRICE: "<<HousePrice<<endl;
    cout<<"# OF BEDROOMS: "<<BedRooms<<endl;
    cout<<"# OF BATHS: "<<Baths<<endl;
    cin.get();

}

void main()
{
    int value;
    RealState HouseValue;
    try
    {
    value= HouseValue.dataEntry();
    }
    catch(const char msg[])
    {
        cout<<"***************************"<<endl;
        cout<<"\n********FATAL ERROR********"<<endl;
        cout<<msg<<endl;
    }
    HouseValue.showdata();
    cin.get();
    cin.get();
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month