Mar 14, 2013 at 7:09am Mar 14, 2013 at 7:09am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
using namespace std;
class Vending{
private :
string item[4];
double coin;
public :
Vending();
void showItemMenu();
void insertCoin();
void buyItem();
};
vending.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include "vending.h"
Vending::Vending() {
coin = 0.00;
item[0] = "Coffee" ;
item[1] = "Snack" ;
item[2] = "Cans of soda" ;
item[3] = "hot drinks" ;
item[4] = "ice cream" ;
}
void Vending::showItemMenu(){
cout << "\tWelcome to Lim's Vending Machine" << endl
<< "--------------------------------------------------" << endl;
for ( int i = 0 ; i < 5 ; i++ ){
cout << ( i+1 ) << ". " << item[i] << endl;
}
}
void Vending::insertCoin(){
cout << "Please insert RM 5 for each item : RM" ;
double money = 0.00;
double moremoney = 0.00;
double tmp = 0.00;
cin >> money;
if ( money < 5 && money > 0 ){
do {
tmp = 5 - money;
cout << "Please insert more RM" << tmp << " !" << endl
<< "Insert more money : RM" ;
cin >> moremoney;
money+= moremoney;
}while ( money < 5 && money > 0);
if ( money > 5 ){
tmp = 0.00;
tmp = money - 5;
cout << "Here is your change : RM" << tmp << endl;
buyItem();
}
else
buyItem();
}
else if ( money > 5 ){
tmp = money - 5;
cout << "Here is your change : RM" << tmp << endl;
buyItem();
}
}
void Vending::buyItem(){
cout << endl;
cout << "Choose item : " ;
int itemChoice = 0;
cin >> itemChoice;
if ( itemChoice < 1 || itemChoice > 5 ){
do {
cout <<"Invalid input ! Please re-enter" ;
cin >> itemChoice;
}while (itemChoice < 1 || itemChoice > 5 );
}
switch ( itemChoice ){
case 1:
cout << "Here is your Coffee" << endl
<< "Thanks you ! Please come again !" << endl << endl;
break ;
case 2:
cout << "Here is your Snack" << endl
<< "Thanks you ! Please come again !" << endl;
break ;
case 3:
cout << "Here is your Soda" << endl
<< "Thanks you ! Please come again !" << endl;
break ;
case 4:
cout << "Here is your hot Drinks" << endl
<< "Thanks you ! Please come again !" << endl;
break ;
case 5:
cout << "Here is your ice cream" << endl
<< "Thanks you ! Please come again !" << endl;
break ;
default :
cout << "Invalid input!" ;
}
}
vending.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
* File: main.cpp
* Author: Lim
*
* Created on March 12, 2013, 1:19 PM
*/
#include "vending.h"
int main( ) {
Vending theVending;
theVending.showItemMenu();
// theVending.insertCoin();
system( "pause" );
return 0;
}
main.cpp
why i cannot create third variable inside my private data
if i create my program will stop run.
else i cannot run.
and why i cannot delete the
virtual constructor and the another constructor ?
if i delete them my program will stop working window pop out ! my lecturer also cannot solve the program !
Last edited on Mar 14, 2013 at 8:30am Mar 14, 2013 at 8:30am UTC
Mar 14, 2013 at 8:12am Mar 14, 2013 at 8:12am UTC
item[4] = "ice cream" ;
You accessing out of bounds here. Your item array declared as char item[4][20];
so you can hold 4 c-strings here with indexes 0, 1, 2 and 3.
Mar 14, 2013 at 8:30am Mar 14, 2013 at 8:30am UTC
@miniioaa.change it to string.
my default program are string. paste wrong here. but can't work also
Mar 14, 2013 at 8:41am Mar 14, 2013 at 8:41am UTC
That will not change fact that you are accessing out of bounds. Even if it is string item[4];
largest index is 3: item[3]
item[4]
will access random memory and can corrupt it (write a garbage to another member for example)
Last edited on Mar 14, 2013 at 8:41am Mar 14, 2013 at 8:41am UTC