Thread: This one simple thing I cant do for my assignment need help

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    This one simple thing I cant do for my assignment need help

    Hello I have an assignment that I pretty much finished heres some of the output is as follows:


    Property Type: RNT
    Cost: 1500 dollors
    Contains: 2 bedrooms
    Days On Market: 20 days
    Address: 560 Rumproast road

    Converting RNT into a RNT..........
    ERROR this property is alreay a rental


    I want it to number the property type. for example I want it to say "property type 1: RNT, property type 2: RNT. ect.

    I cant figure out how to do that without it displaying a garbage number. Heres my source code:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #define DATA 10
    
    
    
    class properties
    {
       private:
           //char *p_pointer[10];
    	   char type[DATA];
    	   //char RNT[10];
    	   int price;
    	   int bedrooms;
    	   int days_on_market;
    	   char address[50];
    	  
      
    	   
    	 
       public:
    	  properties() {}
    	  properties(char typ[], int pri, int bedrm, int days_on_m, char addr[]);
          char* get_type();
    	  char* get_address();
    	  int get_price();
    	  int get_days_on_market();
    	  void convert_to_rental();
    	  void print_listing();
    	  void set_property(char typ[], int pri, int bedrm, int days_on_m,char addr[]);
         
    };
    
      
    //-------------------------Main()-------------------------------------------
    int main()
    {
    	using std::cin;
    	using std::cout;
    	using std::endl;
        properties prop1("RNT", 1500, 2, 20, "560 Rumproast road");
        properties prop2("CND", 2000, 4, 60, "203 lumix vile");
    	prop1.print_listing();
    	prop1.convert_to_rental();
    	prop2.print_listing();
    	prop2.convert_to_rental();
    
        cin.get();
    	return 0;
    }
    
    //----------- Method definitions of class "properties"--------------
    
    properties::properties(char typ[], int pri, int bedrm,int days_on_m, char addr[])
    {
       strcpy(type, typ);
       price= pri;
       bedrooms = bedrm;
       days_on_market = days_on_m;
       strcpy(address, addr);
    
    
    }
    
    int properties::get_price()
    {
    	return price;
    }
    
    char* properties::get_type()
    {
       return type;
    }
    
    char* properties::get_address()
    {
       return address;
    }
    
    void properties::convert_to_rental()
    { 
       using std::cout;
       using std::endl;
       cout<<"Converting"<<" "<<type<<" "<<"into a rental......"<<endl;
       
       if (strcmp(type, "RNT") == 0)
       {
          cout<<"ERROR this property is already a rental"<<endl<<endl;
          return;
       }
       if (strcmp(type, "RNT") != 0)
       {
    	   price /= 300;
    	   days_on_market = 1;
    	   strcpy(type, "RNT");
       }
        
       cout<<"This property is now a:"<<" " <<type<<endl;
       cout<<"Cost:"<<" "<<price<<" "<<"dollors"<<endl;
       cout<<"Contains:"<<" "<<bedrooms<<" "<<"bedrooms"<<endl;
       cout<<"Days On Market:"<<" "<<days_on_market<<" "<<"days"<<endl;
       cout<<"Address:"<<" "<<address<<endl<<endl;
    }
    
    int properties::get_days_on_market()
    {
       return days_on_market;
    }
    
    void properties::print_listing()
    {
       using std::endl;
       using std::cout;
    
       
       cout<<"Property Type:"<<" " <<type<<endl;
       cout<<"Cost:"<<" "<<price<<" "<<"dollors"<<endl;
       cout<<"Contains:"<<" "<<bedrooms<<" "<<"bedrooms"<<endl;
       cout<<"Days On Market:"<<" "<<days_on_market<<" "<<"days"<<endl;
       cout<<"Address:"<<" "<<address<<endl<<endl;
    
    }
    
    void properties::set_property(char typ[], int pri, int bedrm, int days_on_m, char addr[])
    {
       strcpy(type, typ);
       price= pri;
       bedrooms = bedrm;
       days_on_market = days_on_m;
       strcpy(address, addr);
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    This is why it's generally a good idea to separate implementation and user interface. If you don't want to alter your design, you would need a private static variable which you would increment indefinitely. Still not very clean.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    23
    you mean.....having the user input how many properties and then try to figure out something from there??

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Desolation you're really confusing the guy.

    No, he's talking about your code layout, but forget about that for now... first, so I don't have to compile your code myself, give me an example of what you're getting as output and an example of what you want to get... also, if you don't mind, could you put each example output in it's own code tags?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    23
    yes to be honest I was confused lol. Remember I may not be bare bottom new but im still really new. I consider this my rough assignment copy Ok ill post my acutal output for you in tags and my wanted output also.


    Heres my actual real output:

    Code:
    Property Type: RNT
    Cost: 1500 dollors
    Contains: 2 bedrooms
    Days On Market: 20 days
    Address: 560 Rumproast road
    
    Converting RNT into a rental......
    ERROR this property is already a rental
    
    Property Type: CND
    Cost: 2000 dollors
    Contains: 4 bedrooms
    Days On Market: 60 days
    Address: 203 lumix vile
    
    Converting CND into a rental......
    This property is now a: RNT
    Cost: 6 dollors
    Contains: 4 bedrooms
    Days On Market: 1 days
    Address: 203 lumix vile
    Now heres what I want it to do:

    Code:
    Property Type 1 : RNT
    Cost: 1500 dollors
    Contains: 2 bedrooms
    Days On Market: 20 days
    Address: 560 Rumproast road
    
    Converting RNT into a rental......
    ERROR this property is already a rental
    
    Property Type 2: CND
    Cost: 2000 dollors
    Contains: 4 bedrooms
    Days On Market: 60 days
    Address: 203 lumix vile
    
    Converting CND into a rental......
    This property is now a: RNT
    Cost: 6 dollors
    Contains: 4 bedrooms
    Days On Market: 1 days
    Address: 203 lumix vile
    I will add 2 more properties later. But I basically thats all im trying to do. My teacher wants me to include get() methods but now that I look at my program it doesnt seem to need them. But I guess i'll leave them there because they are required.

    Thanks

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    So for each property type you want a cooresponding number. There are several ways you can do that. The easiest method is likely a method that you can't use for this assignment and that's to use a std::map. For you, the simplest way (and I mean simplest in that it's the lowest level) is to just make a function that returns an int that cooresponds to the type. You can do this with if statements or a switch statement... all you need is to compare a string and return the cooresponding int, then what your output statement should effectively look like is.
    Code:
    cout << "Property Type " << getTypeNum(type) << " : " << type << endl;
    Unless, I misunderstood about whether the number cooresponds to the type or if it's just a counter of each property.
    Last edited by SlyMaelstrom; 09-27-2006 at 09:24 PM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could add a member variable that holds an ID and add a different id every time you create a new one in main, but that would put the onus on the calling code in main to figure out what Id to put.

    Another option is to do that but add a static member variable like Desolation suggested that increments each time and initializes the ID variable in the constructor.

    Another option is to have a function somewhere that knows all the type strings and can return a different ID for each, then you can pass that function your string and it will return the proper ID. This would also be a little less flexible since it would have to know all possible Property types.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Ok I guess I'm not very good at explaining things but there are a few things I'd like to point out. First you include <sstream> yet you never use anything located in this file. Second, you are using strcmp() and such other functions wich are located in <cstring>. You've been lucky that for some reason it was automatically included by your compiler but to be on the safe side, when you use functions on C-style strings like strcmp, strcpy and such, include <cstring>. Third, you put lots of 'using' directives. I suggest you to either put each directive only once, and at the top of the file, after all the includes. Better, each time you use something inside the std namespace, you can access it via std:: . It's a bit longer to write, but it prevents you to fall into some mess that could cause weird compiler bugs. It's something you can easily find when you're used to the language but it's mucho harder when you are struggling with it. Last but not least, if have lots of operations to do on strings, I would suggest you to use C++ style strings which are located in <string> (not <string.h> nor <cstring>). They provide a lot of very useful functions -- like the equality operator -- that will save you lots of time.

    In short...

    Good:
    Code:
    #include <string>
    #include <iostream>
    
    bool isStrEqual(std::string a, std::string b) {
        return a == b;
    }
    
    // Display the result via the if operator.  I like things concise but it basically
    // means "if Yay and yay  are equal, output equal otherwise output non-equal".
    std::cout << "Yay and yay are " << (isStrEqual("Yay", "yay") ? "equal" : "non-equal");
    Edit: Don't comment on the utility of the function, I just wanted to show how to write some code using the suggestions I gave you. The code is absolutely pointless, I know.
    Last edited by Desolation; 09-27-2006 at 09:39 PM.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Good explanation for the most part, Desolation, but what's with the ridiculously unnecessary function wrapper around a perfectly good operator? :/
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Read my edit -.- I actually edited before you posted. I felt it coming XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Simple thing
    By Doink in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 05:20 AM
  3. Simple Sorts Confuse ME =/
    By otchster in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 02:02 PM
  4. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  5. Simple project becomes very annoying.
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2002, 11:42 PM