I have found out through the compiler that you cannot declare and initialize an array within a class header file. I was trying to figure out how I might go about getting a 5 by 14 array initialized as a private mamber within the class. I have included the header file and the class cpp file, neither are in full working form, but you should be able to get the idea about what i am trying to accomplish. thanks for taking a look.
rporter1 0 Newbie Poster
// vend cpp file
#include <iostream>
using std::cout
#include "vend.h"
Vend::Vend( int nv, int dv, int qv, int lv, int qtyv, int fsmv, int cstatev )
{
nickles = nv;
dimes = dv;
quarters = qv;
dollars = lv;
qty = qtyv;
cstate = cstatev;
fsm = fsmv;
}
void Vend::setNickle( int nv )
{
nickles = nv;
}
void Vend::setDime( int dv )
{
dimes = dv;
}
void Vend::setQuarter( int qv )
{
quarters = qv;
}
void Vend::setDollar( int lv )
{
dollars = lv
}
void Vend::setQty( int qtyv )
{
qty = qtyv;
}
void Vend::setCstate( int cstatev )
{
cstate = cstatev;
}
void Vend::setFsm( int fsmv )
{
fsm = fsmv;
}
int Vend::getNickle()
{
return nickles;
}
int Vend::getDime()
{
return dimes;
}
int Vend::getQuarter()
{
return quarters;
}
int Vend::getDollar()
{
return dollars;
}
int Vend::getQty()
{
return qty;
}
int Vend::getFsm()
{
return fsm;
}
int Vend::getCstate()
{
return cstate;
}
// show_items: display contents of vending machine items
void Vend::drink_amt()
{
cout << " Item Quantity is: " << qty << "\n";
}
int Vend::bank_amt()
{
cout << " Bank amount is: ";
return dollars * $dollar + quarters * $quarter + dimes * $dime + nickles * $nickle;
}
void Vend::accept_money( unsigned amt )
{
if (amt == $nickle || amt == $dime || amt == $quarter || amt == $dollar)
{
money_msg( "accepted $", amt );
deposit += amt;
}
}
void Vend::return_money( unsigned amt )
{
while (amt >= $dollar && dollars > 0)
{
money_msg( "returned $", $dollar, false );
cout << " (ka-ching)\n";
dollars--;
amt -= $dollar;
}
while (amt >= $quarter && quarters > 0)
{
money_msg( "returned $", $quarter, false );
cout << " (ka-ching)\n";
quarters--;
amt -= $quarter;
}
while (amt >= $dime && dimes > 0)
{
money_msg( "returned $", $dime, false );
cout << " (ka-ching)\n";
dimes--;
amt -= $dime;
}
while (amt >= $nickle && nickles > 0)
{
money_msg( "returned $", $nickle, false );
cout << " (ka-ching)\n";
nickles--;
amt -= $nickle;
}
}
// money_msg: display a monetary amount in dollars/cents format with a message
void Vend::money_msg( char *msg, unsigned amt, bool withnl )
{
cout << msg << fixed << setprecision(2) << amt/100.0;
if (withnl) cout << endl;
}
void vm_simulate( Vend &vm )
{
string input;
int choice;
while (true)
{
// display item menu screen
cout << "\n Porter's Vending Machine Simulation\n";
vm.show_items();
cout << " ===\n";
cout << " N - Insert a nickel\n";
cout << " D - Insert a dime\n";
cout << " Q - Insert a quarter\n";
cout << " L - Insert a dolar\n";
cout << " C - Cancel (coin return)\n";
cout << " S - Show Bank Amt and Drink Qty\n";
cout << " Z - Quit simulation\n";
vm.money_msg( "\nCredit: $", deposit );
cout << "Enter item selection number or command: ";
getline( cin, input );
// remove leading white space
while (isspace( input[0] ))
input.erase( 0, 1 ); // 1 char at a time
// convert user input to all uppercase
for (unsigned i = 0; i < input.length(); i++)
input[i] = toupper( input[i] );
// try to parse a number from user input
choice = atoi( input.c_str() );
// cout << "your input was: |" << input << "|\n";
// cout << "choice = " << n << endl;
if (input[0] == 'V')
else if (input[0] == 'Z') // quit
break;
else if (input[0] == 'S')
vm.drink_amt();
vm.bank_amt();
else if (input[0] == 'C') // cancel
vm.cancel();
else if (input[0] == 'N')
vm.accept_money( nickle );
else if (input[0] == 'D')
vm.accept_money( dime);
else if (input[0] == 'Q')
vm.accept_money( quarter );
else if (input[0] == 'L')
vm.accept_money( dollar );
} // while
} // vm_simulate()
//Header for vending machine
//
#ifndef VEND_H
#define VEND_H
enum State{STATE= 0, ZERO, FIVE, TEN, FIFTEEN, TWENTY, TWENTYFIVE, THIRTY, THIRTYFIVE, FOURTY, FOURTYFIVE, FIFTY, FIFTYFIVE, SIXTY, SIXTYFIVE};
enum Coin{NICKLE = 1, DIME, QUARTER, DOLLAR};
enum Money { $nickle = 5, $dime = 10, $quarter = 25, $dollar = 100 };
int deposit = 0;
class Vend {
public:
void setNickle( int );
int getNickle() const;
void setDime( int );
int getDime() const;
void setQuarter( int );
int getQuarter() const;
void setDollar( int );
int getDollar() const;
void setQty( int );
int getQty() const;
void setCstate( int );
int getCstate() const;
void setFsm( int );
int getFsm() const;
void state(int,int);
void vend_drink( int );
int bank_amt();
int drink_amt();
void cancel();
void accept_money( unsigned amt );
void money_msg( char, unsigned, bool );
private:
int nickles; // number of nickels,
int dimes; // dimes,
int quarters; // and quarters
int dollars; // and dollars
int qty;
int fsm[5][15] = {{'S',0,5,10,15,20,25,30,35,40,45,50,55,60,65},
{'N',5,10,15,20,25,30,35,40,45,50,55,60,65,65}, {'D',10,15,20,25,30,35,40,45,50,55,60,65,65,65},
{'Q',25,30,35,40,45,50,55,60,65,65,65,65,65,65,65},{'L',65,65,65,65,65,65,65,65,65,65,65,65,65,65}};
int cstate;
achyut 0 Newbie Poster
you cannot declare and initialize an array in header file.but u can crate as many as u want object of any class member in your .c++ file so using this u can create run time array. think on this
Tight_Coder_Ex 17 Posting Whiz in Training
All you have to do is move or more specifically code appropriately in Vend::Vend (default constructor) the values you want in fasm. This is how variables are initialized in a class, like int value [1][2] = {4, 8}; would be too code.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.