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

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




Passing values

 
Reply to this topicStart new topic

Passing values

jrice528
5 Dec, 2007 - 08:37 PM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
Ok... This is my assignment, its fairly easy. I need to make a class for a retail store. storing only 3 items in the class using arrays.

How would I pass a value x into getDiscription() to d[] and have it print the next item. have it print d[0] then increase by one and print d[1].

Does that make sense at all?? Its really hard for me to explain it. I think i need to pass by reference but I am not sure how to do that. I will post the output.

RetailItem.cpp
CODE
#include <cstdlib>
#include <iostream>
#include <string>
#include "RetailItem.h"

using namespace std;


RetailItem::RetailItem()
{
}

double RetailItem::getPrice()
{
     p[0] = 59.95;
     p[1] = 34.95;
     p[2] = 24.95;
     return p[];
    
}

string RetailItem::getDescription()
{
       d[0] = "Jacket";
       d[1] = "Jeans";
       d[2] = "Shirt";
      
       return d[];
}


double RetailItem::getUnitsOnHand()
{
       oh[0] = 12;
       oh[1] = 40;
       oh[2] = 20;
      
       return oh[];
}

RetailItem.h
CODE
#include <cstdlib>
#include <iostream>
#include <string>
#include "RetailItem.h"

using namespace std;


RetailItem::RetailItem()
{
}

double RetailItem::getPrice()
{
     p[0] = 59.95;
     p[1] = 34.95;
     p[2] = 24.95;
     return p[];
    
}

string RetailItem::getDescription()
{
       d[0] = "Jacket";
       d[1] = "Jeans";
       d[2] = "Shirt";
      
       return d[];
}


double RetailItem::getUnitsOnHand()
{
       oh[0] = 12;
       oh[1] = 40;
       oh[2] = 20;
      
       return oh[];
}





Main.cpp
CODE
#include <cstdlib>
#include <iostream>
#include "RetailItem.h"
#include <iomanip>
using namespace std;
const unsigned W = 20;

int main(int argc, char *argv[])
{
    RetailItem r;

cout<<setw(W)<<"Description"<<setw(W)<<"Units on Hand"<<setw(W)<<"Price"<<endl;
cout<<endl;
cout<<"Item #1";
cout<<setw(10)<<r.getDescription();
cout<<setw(15)<<r.getUnitsOnHand();
cout<<setw(25)<<r.getPrice()<<endl;
cout<<"Item #2"<<endl;
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



OUTPUT
CODE
    Description    Units On Hand    Unit Price ($)
Item #1    Jacket                          12                                    59.95
Item #2    Jeans                          40                                    34.95
Item #3    Shirt                          20                                    24.95

User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Passing Values
5 Dec, 2007 - 10:02 PM
Post #2

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 995



Thanked: 4 times
Dream Kudos: 375
My Contributions
Your indexing is wrong, better read a good book on C/C++ about arrays and character. Or go to

http://www.cplusplus.com/doc/tutorial/ntcs.html

What I can give you now is an example by the use of vectors instead of arrays.

Vectors know its size, so you don't need to worry about overflow at runtime.
CODE
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void getDescription()
{
       vector <string> des;
      
       // store your clothes in vector
       des.push_back("Jacket");
       des.push_back("Jeans");
       des.push_back("Shirt" );
    
    // Printing
    for (int i=0; i<des.size(); i++)
    {
        cout << "Description :" << des.at(i) << endl;
    }
    
}

int main()

{
    
    getDescription();
    
    return 0;
    
}
I hope that helps.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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