C++ Question(please help :) )?

Hi i write this program for my class assignment, I dont know why but when im choosing Lease the car. in output function im getting some crazy numbers.

here is my code.

#include <iostream>

using namespace std;

enum type{lease,purchase};

union paytype

{

float monthly;

int full;

};

struct car

{

char manufacturer[20];

int year;

type tpy;

paytype money;

};

void assignValues(car [],int);

void displayValues(car [],int);

int main()

{ int total;

cout<<"Enter the number of cars in stock :";

cin>>total;

car *cart;

cart = new car [total];

assignValues(cart,total);

displayValues(cart,total);

system("pause");

return 0;

}

void assignValues(car c[],int total)

{

char choice;

for(int count=0;count<total;count++,c++)

{

cout<<"Enter the make for car "<<count<<": ";

cin.ignore();

cin.getline(c->manufacturer,20);

cout<<"Enter the year: ";

cin>>c->year;

cout<<"Lease (l or L) or Purchase (p or P): ";

cin>>choice;

if(choice=='l' || choice=='L')

c->tpy=lease;

else

c->tpy=purchase;

if(c->tpy==lease)

{

cout<<"Enter monthly payment: ";

cin>>c->money.monthly;

cout<<"Check price"<<c->money.monthly<<endl;

}

else if(c->tpy=purchase)

{

cout<<"Enter the purchase price: ";

cin>>c->money.full;

}

cout<<endl;

}

}

void displayValues(car c[],int total)

{for(int count=0;count<total;count++)

{

cout<<"Make: "<<c->manufacturer<<endl;

cout<<"Year: "<<c->year<<endl;

if(c->tpy=lease)

cout<<"Leased at $"<< c[count].money.monthly<<endl;

else

cout<<"Purchased at $"<< c[count].money.full<<endl;

cout<<endl;

}

}

Thank you.

Update:

in Union, when i put int instead of float program works just fine.how to fix it ?

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    Change this line

    if (c->tpy = lease)

    to

    if (c->tpy == lease)

    You should also be setting

    c->money.full = 0;

    for leased and

    c->money.monthly = 0.0f;

    for purchase.

    Have fun.

    • Login to reply the answers
Still have questions? Get your answers by asking now.