Jump to content

C++ Help...

Sebastian Kurpiel

What's up guys, this is my first class in C++ and I have ran into some errors that I can't seem to debug.  If anyone has time could may you give it a look. Thanks in advance. 

order.h (Professor made this)


#ifndef Order_h
#define Order_h

class Order {

private:
    char * symbol;
    char side; //B for Bid A for Ask
    double price; //decimal points
    int quantity; //full number
    char message_type;//A for Add, M for Modify, D for Remove, C for Clear, will explain later
    int order_id; //full integer
public:
    Order(); //default constructor, default private variables to respective defaults, 0's for nums, NULL for char arrays, NULL terminator for char
    Order(char * s, char bs, double p, int q, char mp, int oid); //assign variables with parameters given by the user
    ~Order(); //if there is any variables that are made dynamically release there memory look at rectangle_example_dynamic.cp for assistance
    bool set_symbol(char * s); //look at Examples/Week2/rectangle_example_dynamic.cpp for assistance
    bool set_side(char s); //set to new side
    bool set_orderid(int oid);//set to new orderid
    bool set_message_type(char mp); //set to new message_type
    bool set_quantity(int q); //make sure if quantity is <= 0 print a statement saying 'Can Not be less or equal to 0' return false otherwise set it return true
    bool set_price(double p); //make sure if price is <= 0.0 print a statement saying Can not be less or equal to 0.0 return false other set it return true
    char * get_symbol(); //return said var
    char get_side(); //return said var
    double get_price(); //return said var
    int get_quantity(); //return said var
    void write_console(); //make sure ALL variables are present, meaning they are not 0, 0.0, NULL, or \0 print a statment that looks like example
                          //MsgType: A Symbol: AAPL Side: B Quantity: 200 Price: 238.90 OrderId: 123345
                          //else print not all data points are available can't print!
    char get_message_type(); //return said variable
    char get_order_id(); //return said variable
}
#endif 

order.cpp(I wrote this)


#include <iostream>
#include "order.h"
using namespace std;
Order()::Order(char * s, char bs, double p, int q, char mp, int oid);
{
    * symbol = *s;//symbol
    side = bs;// asumming this is the bid/asking price
    price = p;//price
    quantity = q;//quantity
    message_type = mp;//prof will explain later
    order_id = oid;//the order number
}
Order::~Order()
{
    cout << "Destroying Order" << endl;
    if (symbol != 0) {
        symbol = 0;
    }
    if (side != 0) {
        side = 0;
    }
    if (price != 0) {
        price = 0;
    }
    if (quantity != 0) {
        quantity = 0;
    }
    if (message_type != 0){
        message_type = 0;
    }
    if (order_id != NULL) {
        delete order_id;
    }
}
char Order::get_symbol();//accessor
{
    return symbol;
}
bool Order::set_symbol(char * s);//mutator
{
    if (*s != NULL) {
        symbol=*s;
        return true;
    }
    return false;
}
char Order::get_side()//accessor
{
    return side;
}
bool Order::set_side(char bs)//mutator
{
    if (bs != NULL) {
        side=bs;
        return true;
    }
    return false;
}
double Order::get_price()//accessor
{
    return price;
}
bool Order::set_price(double p)//mutator
{
    if (p != NULL) {
        price = p;
        return true;
    }
    return false;
}
int Order::get_quantity()//accessor
{
    return quantity;
}
bool Order::set_quantity(int q)//mutator
{
    if (q != NULL) {
        quantity = q;
        return true;
    }
    return false;
}
char Order::get_message_type()//accessor
{
    return message_type;
}
bool Order::set_message_type(char mp)//mutator
{
    if (mp != NULL) {
        message_type = mp;
        return true;
    }
    return false;
}
int Order::get_order_id()//accessor
{
    return order_id;
}
bool Order::set_orderid(int oid)//mutator
{
    if (oid != NULL) {
        order_id = oid;
        return true;
    }
    return false;
}

main.cpp(wrote this aswell)


#include <iostream>
#include "order.h"


int main(){
    Order order_one = new Order("APPL", 'A', 100.99, 4, , 12345);
    order_one.write_console();
    Order *two= new order_two("TC", 'B', 59.97, 100, 12346)
    order_two.write_console();
    delete two;
 //Create a static Order object called order_one with the default constructor
 //Call order_one write_console() function, should print out the not valid statement of write console
 //Create a dynamic (*) Order object called order_two with second constructor, fill in with valid parameters
 //Call order_two write console function, should print out the Order information
 //take order_two and set_price to 94.88 and set_symbol to be DIA
 //call order_two write console function, should show the change
 //delete your dynamic order object
 return 0;
}

Error when running the main.1.PNGError when running the order.cpp

2.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

The biggest problem is the missing semi colon at the end of the class Order definition in order.h, after that's fixed most of the errors will go away and the rest will be a lot easier to figure out.

 

Next, in order.cpp, the functions shouldn't have a semi colon at the end like in the line

char Order::get_symbol();//accessor

A few of the functions in order.cpp don't match their declaration in order.h (char vs char* and int vs char etc.)

Order()::Order(char * s, char bs, double p, int q, char mp, int oid);

Has an extra set of () after the first Order that shouldn't be there.

 

order_id is an int so you shouldn't be deleting it.

 

Those should fix like...half of the problems.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, I start with this. There is a lot of small errors here and there that break the entire program. I see you are using visual studio, look what the red lines are saying when you hover over them.
I will mention a few.
You have on a lot of your functions in Order.cpp written them like this: var functionName(); { function code } //You have to remove the ";".

And you'r professor have forgotten an ";" on the end of his "class order{};"

You have not made, and there for not reset the variables to the default values like he asked you for in the default constructor. The default constructor is always just ClassName::ClassName(" /*Nothing here*/"){} //No parameters.
There are also a lot of NotPointer = pointer problems. Where my best suggestion is adding some "*" and "&" on the variables until it works :P Ps. There are also some pointer fuctions (Is that what they are called?) also. Like: var * functionName(); That need to be var *className::functionName(){} 
I hope I helped somewhat.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Fix const correctness all over.

 

"symbol" is a char pointer with no memory allocated to it to store your symbol. Also, you cannot copy a string like that, use strcpy or std::string.

 

Use enums for "side" and "message_type".

 

In the destructor you are nullifying the "symbol" pointer, not the contents. Once you allocate memory for "symbol" properly in the constructor, you should free it with "delete" in the destructor.

 

"delete order_id" tries to delete a automatic variable.

 

"get_symbol()" does not match the prototype (char vs char*)

 

"set_symbol()" has the same problem as constructor, you cannot copy a string like that. You need to free any memory allocated to "symbol", reallocate enough memory for the new string and copy it using strcpy or use std::string.

 

"Order order_one" is a automatic variable, you cannot assign dynamically allocated new Order to it"
Use "Order order_one("APPL", ...etc"

 

"order_to.write_console()" order_two is a Order pointer, members are accessed with "->" for pointer types.
"Order_two->write_console();"

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

5 hours ago, Unimportant said:

 

 

 

13 hours ago, ollod said:

 

 

 
 

 

14 hours ago, fizzlesticks said:

 

 

Thanks guys, I  really appreciate the help, C++ is a new language to me and I haven't grasped it yet. BTW the sample output should be

"Not all data points are available can't print! MsgType: A Symbol: AAPL Side: B Quantity: 200 Price: 238.90 OrderId: 123345
MsgType: A Symbol: DIA Side: B Quantity: 200 Price: 94.88 OrderId: 123345 "

Where would I add the "not all data points..." part?

I'm now receiving 15 errors in my order.cpp

What does expected a declaration mean?

For the expression must be a pointer to complete object type, I add the * and still receive an error.

The 4th error I don't understand, there's a ";"...

2.PNG


 #include <iostream>
#include "order.h"
using namespace std;
Order::Order(char * s, char bs, double p, int q, char mp, int oid);{
    *symbol = *s;//symbol
    side = bs;// asumming this is the bid/asking price
    price = p;//price
    quantity = q;//quantity
    message_type = mp;//prof will explain later
    order_id = oid;//the order number
    int oid = 0
}
Order::~Order(){
    cout<< "Destroying Order" << endl;
    if (side != 0) {
        side = 0;
    }
    if (price != 0) {
        price = 0;
    }
    if (quantity != 0) {
        quantity = 0;
    }
    if (message_type != 0) {
        message_type = 0;
    }
    if (order_id != NULL) {
        order_id = 0;
    }
    if (*symbol != 0) {
        delete *symbol;
    }
}
char* Order::get_symbol()//accessor
{
    return symbol;
}
bool Order::set_symbol(char * s)//mutator
{
    if (*s != NULL) {
        *symbol = *s;
        return true;
    }
    return false;
}
char Order::get_side()//accessor
{
    return side;
}
bool Order::set_side(char bs)//mutator
{
    if (bs != NULL) {
        side = bs;
        return true;
    }
    return false;
}
double Order::get_price()//accessor
{
    return price;
}
bool Order::set_price(double p)//mutator
{
    if (p != NULL) {
        price = p;
        return true;
    }
    return false;
}
int Order::get_quantity()//accessor
{
    return quantity;
}
bool Order::set_quantity(int q)//mutator
{
    if (q != NULL) {
        quantity = q;
        return true;
    }
    return false;
}
char Order::get_message_type()//accessor
{
    return message_type;
}
bool Order::set_message_type(char mp)//mutator
{
    if (mp != NULL) {
        message_type = mp;
        return true;
    }
    return false;
}
int Order::get_order_id()//accessor
{
    return order_id;
}
bool Order::set_orderid(int oid)//mutator
{
    if (oid != NULL) {
        order_id = oid;
        return true;
    }
    return false;
}

main.cpp


#include <iostream>
#include "order.h"


int main() {
    Order order_one("APPL", 'A', 100.99, 4, , 12345);
    order_one.write_console();
    Order *two = new order_two("TC", 'B', 59.97, 100, 12346)
        order_two->write_console();
    delete two;
    //Create a static Order object called order_one with the default constructor
    //Call order_one write_console() function, should print out the not valid statement of write console
    //Create a dynamic (*) Order object called order_two with second constructor, fill in with valid parameters
    //Call order_two write console function, should print out the Order information
    //take order_two and set_price to 94.88 and set_symbol to be DIA
    //call order_two write console function, should show the change
    //delete your dynamic order object
    return 0;
}

3.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Order::Order(char * s, char bs, double p, int q, char mp, int oid);{ ...etc...

Remove the ';'

 

Your constructor requires 6 arguments but you only pass 5 on both occasions where you construct a object int main.

 

Order *two = new order_two("TC", 'B', 59.97, 100, 12346)
  
should be:

Order *order_two = new Order(...etc...

Altough there is no reason to dynamically allocate there, you could just use a automatic variable like you did with order_one.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Unimportant said:

Altough there is no reason to dynamically allocate there, you could just use a automatic variable like you did with order_one.

 

 

 
1

I didn't notice that part about the main, the first order is suppose to write that there is an error in the because there is no 6th value. Where would I place my error code so that it prints, "Not all data points are available can't print!  " and it prints the output when corrected like this " MsgType: A Symbol: AAPL Side: B Quantity: 200 Price: 238.90 OrderId: 123345 "My professor asked to create the second object as a dynamic object. I'm now receiving these errors with the changes to the main that I did:

2.PNG 


#include <iostream>
#include "order.h"


int main() {
    Order order_one("APPL", 'A', 100.99, 4, , 12345);
    order_one.write_console();
    Order *order_two = new Order("TC", 'B', 59.97, 100, "A", 12346)
    order_two ->write_console();
    delete order_two;
    //Create a static Order object called order_one with the default constructor
    //Call order_one write_console() function, should print out the not valid statement of write console
    //Create a dynamic (*) Order object called order_two with second constructor, fill in with valid parameters
    //Call order_two write console function, should print out the Order information
    //take order_two and set_price to 94.88 and set_symbol to be DIA
    //call order_two write console function, should show the change
    //delete your dynamic order object
    return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

57 minutes ago, Sebastian Kurpiel said:

I didn't notice that part about the main, the first order is suppose to write that there is an error in the because there is no 6th value. Where would I place my error code so that it prints, "Not all data points are available can't print! 

 

You can't. C++ requires you to adhere to the signature of a function. The function expects 6 arguments of the correct type and nothing else will be accepted.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, HeadsUpHigh said:

Ahem, not to sound like an asshole, but you are using an IDE with some of the most advanced debugging. Did you even read the error logs ?

 

http://prntscr.com/cifier

 

I do read them, but this is my first homework assignment with C++ and I'm confused why it's asking me to put a semicolon on my main part though 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Unimportant said:

You can't. C++ requires you to adhere to the signature of a function. The function expects 6 arguments of the correct type and nothing else will be accepted.

 

So then how would I go about completing this part that my professor asked for?

//Call order_one write_console() function, should print out the not valid statement of write console 

Would C++ print that as soon as my other codes are gone?

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Sebastian Kurpiel said:

I do read them, but this is my first homework assignment with C++ and I'm confused why it's asking me to put a semicolon on my main part though 

I think the main issue is that you are not passing the correct number of arguments to the constructor and some of the errors you get are confused because of that. Try fixing that and see what errors you will get.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Unimportant said:

You can't. C++ requires you to adhere to the signature of a function. The function expects 6 arguments of the correct type and nothing else will be accepted.

 

 

Just now, HeadsUpHigh said:

I think the main issue is that you are not passing the correct number of arguments to the constructor and some of the errors you get are confused because of that. Try fixing that and see what errors you will get.

 

I added a 6th item to the first order, but my professor wants it so Call order_one write_console() function, \ prints out the not valid statement of write console. That's why I'm leaving a spot blank 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Sebastian Kurpiel said:

 

I added a 6th item to the first order, but my professor wants it so Call order_one write_console() function, \ prints out the not valid statement of write console. That's why I'm leaving a spot blank 

You can't leave a blank spot the C++ semantics don't allow that afaik. If the constructor takes 6, you have to provide 6. I can't remember, it has been a while but I think this applies to all function, right ?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×