question about the implementation function for my project

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1158 Views - Last Post: 09 December 2014 - 05:06 PM Rate Topic: -----

#1 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

question about the implementation function for my project

Posted 07 December 2014 - 01:22 PM

my project is about loading the text file and record the information about selling chocolate and 3 types of chocolate there are 5 class we have to do, and when i look the first class I was confused
there are two implementation requirement that i do not understand

there are two varible in private class they are ordernumber and chocolate type

1.Order(int num, char tp);User-defined constructor. Assign the value of input arguments to the corresponding data fields, order number and ordered chocolate type.
2.void setOrderNumber(int num);
void setType(char tp); Modifier methods assign the value of input argument to the corresponding data field.

those function look like same thing in my understanding is to loading the text file and save the order number and chocolate type and Why they have "int num int to" in bracket? my thought the purpose the loading the num and tp in the text file.. can u guys explain to me ?

Is This A Good Question/Topic? 0
  • +

Replies To: question about the implementation function for my project

#2 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1865
  • View blog
  • Posts: 6,727
  • Joined: 19-February 09

Re: question about the implementation function for my project

Posted 07 December 2014 - 03:53 PM

There are similarities - the functions set member variables. The constructor can have an initialization list, this can be more efficient for initializing objects.

Constructor initialization lists
Was This Post Helpful? 1
  • +
  • -

#3 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 07 December 2014 - 04:11 PM

View Post#define, on 07 December 2014 - 03:53 PM, said:

There are similarities - the functions set member variables. The constructor can have an initialization list, this can be more efficient for initializing objects.

Constructor initialization lists

then why they have "int num and int tp" i dont get it in my opinion is just loading the text for save ordernumber and type then why they ask them in the function?
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1865
  • View blog
  • Posts: 6,727
  • Joined: 19-February 09

Re: question about the implementation function for my project

Posted 07 December 2014 - 04:50 PM

You read the values from the file into suitable variables, then create an Order. One way of reading the data is to read to the variables, create an Order object then assign (copy) it to an element of an array. Another way is to read the data to variables then use the modifier functions to update the Order object in the array.

To set the values in the you need the function parameters.

The best thing is to create a simple program

  int  order_number = 101;
  char order_type = 'A';

  Order order(order_number, order_type);

  cout << "Order=" << order.getOrderNumber() << ", " << order.getOrderType() << endl;

  order.setOrderType('G');



Was This Post Helpful? 0
  • +
  • -

#5 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 07 December 2014 - 05:11 PM

View Post#define, on 07 December 2014 - 04:50 PM, said:

You read the values from the file into suitable variables, then create an Order. One way of reading the data is to read to the variables, create an Order object then assign (copy) it to an element of an array. Another way is to read the data to variables then use the modifier functions to update the Order object in the array.

To set the values in the you need the function parameters.

The best thing is to create a simple program

  int  order_number = 101;
  char order_type = 'A';

  Order order(order_number, order_type);

  cout << "Order=" << order.getOrderNumber() << ", " << order.getOrderType() << endl;

  order.setOrderType('G');



i write my code like that then i stoped i dont k which function should I do the loading file
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;

ofstream input;

class Order
{
public:
    Order();
    ~Order();
    Order(int num, char tp);
    int getOrderNumber();
    char getType();
    void setOrderNumber(int num);
    void setType(char tp);
    void displayOrder(ofstream&outFile);
private:
    int orderNumber;
    char type;
};

Order::Order()
{
    orderNumber=0;
    type = 'N';
}

Order::Order(int num, char tp)
{
 num = orderNumber;
 tp = type;

}

void Order::setOrderNumber(int num)
{
    ofstream input;

}



main()
{



Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1865
  • View blog
  • Posts: 6,727
  • Joined: 19-February 09

Re: question about the implementation function for my project

Posted 07 December 2014 - 05:34 PM

Usually you are reading more than one Order from a file, so you would do that in main or another function. You might have another class that has a list of Orders, then it would be appropriate to have a read from file function as a member of that class.

In the Order class you could have a function that takes an istream as parameter and reads the data into the Order, but you have probably not been asked to do that.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: question about the implementation function for my project

Posted 07 December 2014 - 06:46 PM

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;


What language are you writing in?
Was This Post Helpful? 1
  • +
  • -

#8 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 07 December 2014 - 11:31 PM

View PostCTphpnwb, on 07 December 2014 - 06:46 PM, said:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;


What language are you writing in?

i am writing c++
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: question about the implementation function for my project

Posted 08 December 2014 - 07:26 AM

So what are you including C libraries for?
Was This Post Helpful? 0
  • +
  • -

#10 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 08 December 2014 - 05:36 PM

i am working on my coding I feel something wrong with my codeo I want to comput how many order in the text file then display it like "order number =1" "order number =2" so I did the loop in the main method it gives my total 14 order, then I want call my getOrdernumber function to display "order number =1", but it does not let me call the function and I am confused order(int num,char tp) use for what purpose ?
#include <iostream>
#include <fstream>
using namespace std;




class Order
{
public:
    Order();

    Order(int num, char tp);
    int getOrderNumber();
    char getType();
    void setOrderNumber(int num);
    void setType(char tp);
    void displayOrder(ofstream&outFile);
private:
    int orderNumber;
    char type;
};

Order::Order()
{
    orderNumber=0;
    type = 'N';

}

Order::Order(int num, char tp)
{
 num = orderNumber;
 tp = type;

}

void Order::setOrderNumber(int num)
{
    cout<<"Order number ="<<num;

}
int Order::getOrderNumber()
{
    return orderNumber;
}



main()
{

    int num =1;
    int sum =0;
    char tp;
    ifstream inFile;
    inFile.open("orders.dat");
   while(!inFile.eof())
   {
    inFile>>tp>>num;
    num=1;
    sum += num;


   }
   cout<<sum;
   for(int i=0; i<=(sum-1);i++)
   {
     Order number;
     number.getOrderNumber();
   }



inFile.close();

}
[/code\

[code]
#include <iostream>
#include <fstream>
using namespace std;




class Order
{
public:
    Order();

    Order(int num, char tp);
    int getOrderNumber();
    char getType();
    void setOrderNumber(int num);
    void setType(char tp);
    void displayOrder(ofstream&outFile);
private:
    int orderNumber;
    char type;
};

Order::Order()
{
    orderNumber=0;
    type = 'N';

}

Order::Order(int num, char tp)
{
 num = orderNumber;
 tp = type;

}

void Order::setOrderNumber(int num)
{
    cout<<"Order number ="<<num;

}
int Order::getOrderNumber()
{
    return orderNumber;
}



main()
{

    int num =1;
    int sum =0;
    char tp;
    ifstream inFile;
    inFile.open("orders.dat");
   while(!inFile.eof())
   {
    inFile>>tp>>num;
    num=1;
    sum += num;


   }
   cout<<sum;
   for(int i=0; i<=(sum-1);i++)
   {
     Order number;
     number.getOrderNumber();
   }



inFile.close();

}


Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1865
  • View blog
  • Posts: 6,727
  • Joined: 19-February 09

Re: question about the implementation function for my project

Posted 08 December 2014 - 06:14 PM

I think you need to store the order data, one possibility is a vector.

For a vector you will need the vector header.

#include <vector>




The data file has the format <order type> <order number> ?


int main()
{
  // create a list/collection of orders
  vector<Order> orders;

  ifstream inFile;
  inFile.open("orders.dat");

  if( !inFile) {
    cout << "Error opening file." << endl;
  }

  int  order_no;
  char order_type;

  cout << "reading orders" << endl;

  // read orders
  while( inFile >> order_type >> order_no )
  {
    // create order
    Order order(order_no, order_type);

    // add copy to list
    orders.push_back(order);
  }

  cout << "number of orders=" << orders.size() << endl;

  cout << "displaying orders" << endl;

  // there are various ways to get the data.
  for(int i=0; i<orders.size(); i++)
  {
    // access each Order
    cout << orders[i].getOrderNumber()
         << " "
         << orders[i].getType() << endl;
  }

  inFile.close();

  return 0;
}




vector

This post has been edited by #define: 08 December 2014 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

#12 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 08 December 2014 - 06:41 PM

View Post#define, on 08 December 2014 - 06:14 PM, said:

I think you need to store the order data, one possibility is a vector.

For a vector you will need the vector header.

#include <vector>




The data file has the format <order type> <order number> ?


int main()
{
  // create a list/collection of orders
  vector<Order> orders;

  ifstream inFile;
  inFile.open("orders.dat");

  if( !inFile) {
    cout << "Error opening file." << endl;
  }

  int  order_no;
  char order_type;

  cout << "reading orders" << endl;

  // read orders
  while( inFile >> order_type >> order_no )
  {
    // create order
    Order order(order_no, order_type);

    // add copy to list
    orders.push_back(order);
  }

  cout << "number of orders=" << orders.size() << endl;

  cout << "displaying orders" << endl;

  // there are various ways to get the data.
  for(int i=0; i<orders.size(); i++)
  {
    // access each Order
    cout << orders[i].getOrderNumber()
         << " "
         << orders[i].getType() << endl;
  }

  inFile.close();

  return 0;
}




vector

thank you for helping!
let me show you the ouput look like the order number and type is only part of my coding
the ouput is like

Order number = 1
Order type = Mint Chocolate
Ordered quantity = 25
Unit price = $1.25
Ordered Cost = $31.25

Order number = 3
Order type = Truffle Chocolate
Ordered quantity = 120
Unit price = $2.25
Ordered Cost = $270

Order number = 5
Order type = Mint Chocolate
Ordered quantity = 97
Unit price = $1.25
Ordered Cost = $121.25
Was This Post Helpful? 0
  • +
  • -

#13 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1865
  • View blog
  • Posts: 6,727
  • Joined: 19-February 09

Re: question about the implementation function for my project

Posted 08 December 2014 - 07:19 PM

Ok, I think you are saying, that you are printing the data in a satisfactory manner.
Was This Post Helpful? 0
  • +
  • -

#14 nerocheng321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-November 14

Re: question about the implementation function for my project

Posted 09 December 2014 - 03:01 PM

View Post#define, on 08 December 2014 - 07:19 PM, said:

Ok, I think you are saying, that you are printing the data in a satisfactory manner.


hello I am still working on my coding and I want show my order number from 1 to 14 and I write like that but it only how order number =1; if i push loop in the serOrdernumber() it will fix this howerver, I cant keep adding information under each ordernumber suck as price and chocolate type is any way to fix ?
#include <iostream>
#include <fstream>
#include <vestor>
using namespace std;




class Order
{
public:
    Order();

    Order(int num, char tp);
    int getOrderNumber();
    char getType();
    void setOrderNumber(int num);
    void setType(char tp);
    void displayOrder(ofstream&outFile);
private:
    int orderNumber;
    char type;

};

Order::Order()
{
    orderNumber=0;
    type = 'N';




}

Order::Order(int num, char tp)
{
 num = orderNumber;
 tp = type;

}

void Order::setOrderNumber(int num)
{


 cout<<"order number ="<<num<<"\n";




}
int Order::getOrderNumber()
{
    setOrderNumber(orderNumber);
}



main()
{
    int num;
  int sum;
    char tp;
    ifstream inFile;
    inFile.open("orders.dat");
   while(!inFile.eof())
   {
    inFile>>tp>>num;
    num=1;
    sum += num;
    }


for(int i=1; i<sum;i++)
{
    Order numb;
    numb.getOrderNumber();

}

   }






Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: question about the implementation function for my project

Posted 09 December 2014 - 04:19 PM

Look closely at this:
Order::Order(int num, char tp)
{
 num = orderNumber;
 tp = type;

}

What does the function do with num and tp?
void Order::setOrderNumber(int num)
{


 cout<<"order number ="<<num<<"\n";




}


What does this function do with num? Why all the white space?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2