using constructors and arrays

Hello everybody,
I'm getting crazy with this errors I'm getting
I'm trying to built a program for a soda machine
Its probably something wrong in the way I'm using the constructors
I'm not so familiar with the subject yet
and I'm trying to initialize an array of structures in my class
and I'm getting this messages:

error: expected primary-expression before '{' token
error: expected `;' before '{' token


Here is the code for the class:
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
#include<string>
using namespace std;
struct DrinkInfo
{   string drinkName;
    float  drinkPrice;
    int    drinkQuantity;
};
#ifndef DRINK_H
#define DRINK_H
class DrinkMachine
{
    private:
    DrinkInfo drinkArray[5];
    void inputMoney(short);
    void dailyreport();

    public:
    DrinkMachine();
    void displayChoises();
    void buyDrink(short);
    ~DrinkMachine();
};
#endif
DrinkMachine::DrinkMachine()
{  drinkArray[5] = {DrinkInfo("Cola", 0.75,20),DrinkInfo("Root beer",0.75,20),
        DrinkInfo("Orange Soda",0.75,20),DrinkInfo("Grape Soda",0.75,20),
        DrinkInfo("Bottled Water",0.75,20)};
}
void DrinkMachine::displayChoises()
{
    cout << "\tDRINK      " << "PRICE" << endl;
    for(int index = 0;index < 5;index++)
    {
    cout << "\t" << (index+1) <<"."<< setw(10) << drinkArray[index].drinkName << "$"
         << drinkArray[index].drinkPrice << endl;
    }
}

//buyDrink() function definiton
//calls the input money if the soda is avaliable
//otherwise displays a sorry message
void DrinkMachine::buyDrink(short choise)
{
    if(drinkArray[choise-1].drinkQuantity > 0)
    inputMoney(choise);

    else
    cout << "The specific soda has run out.We are sorry!\n";
}

void DrinkMachine::inputMoney(short sodaType)
{
    float moneyEntered = 0;
    float totalMoney = 0;
    cout << "PRICE $" << drinkArray[sodaType -1].drinkPrice;


        while(totalMoney <  drinkArray[sodaType -1].drinkPrice)
        {
            cout << "Please insert money or press 0 to esape.\n";
            cin  >> moneyEntered;
            totalMoney += moneyEntered;

            if(totalMoney == drinkArray[sodaType -1].drinkPrice)
           {
               cout << "Here is your drink.Thank You!" << endl;
               break;
           }
            else if(totalMoney > drinkArray[sodaType -1].drinkPrice)
            {    float change = totalMoney - drinkArray[sodaType -1].drinkPrice;

                 cout << "You change is " << change << endl;
                 cout << "Here is your drink.Thank you!" << endl;
                 break;
            }
            if(moneyEntered == 0)
            {
                cout << "Purchase never fiished!\n";
                cout << "Here is your $" << totalMoney << " back.\n";
                break;
           }
}

}

and here is the code for main
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
#include<iostream>
#include<iomanip>
#include<string>
#include"DrinkMachine.h"
using namespace std;


int main()
{

      DrinkMachine buyObject;
        buyObject.displayChoises();

        char yesNo;
        cout << "Would you like a drink?(y/n)\n";
        cin  >> yesNo;
        if(yesNo == 'Y' || yesNo == 'y')
        {     short drinkNum;
            cout << "Enter the number for the drink.\n";
            do
            {
                cout << "Please enter a number in the range 1 - 5 to buy drink";
                cin  >> drinkNum;
           }while(drinkNum < 1 || drinkNum > 5);

            buyObject.buyDrink(drinkNum);
        }

        else
        cout << "Next time maybe!\n";




      return 0;
}


Any Help will be really apreciated
thanks
In DrinkMachine::DrinkMachine:

You didn,'t provide a constructor for DrinkInfo so the expression DrinkInfo("Cola", 0.75,20) isn't valid

The braces are valid only for initialization of an array, not for an assignment and you are trying to assign all the values to the 6th element of DrinkInfo
Thanks,
but if I wanted to initialize the specific array
with the specific values how would I do it?
Assuming you provided a constructor for DrinkInfo, you can assign a value to each element of the array
eg:
1
2
3
4
5
6
7
8
DrinkMachine::DrinkMachine()
{  
    drinkArray[0] = DrinkInfo("Cola", 0.75,20);
    drinkArray[1] = DrinkInfo("Root beer",0.75,20);
    drinkArray[2] = DrinkInfo("Orange Soda",0.75,20);
    drinkArray[3] = DrinkInfo("Grape Soda",0.75,20);
    drinkArray[4] = DrinkInfo("Bottled Water",0.75,20);
}
Thanks again but still won't work....
I feel we are close though
Here is the code:

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
#include<string>
using namespace std;
struct DrinkInfo
{   string drinkName;
    float  drinkPrice;
    int    drinkQuantity;
    DrinkInfo(string n,float p,int q)
    { drinkName = n; drinkPrice = p; drinkQuantity =q; }
};
//#ifndef DRINK_H
//#define DRINK_H
class DrinkMachine
{
    private:
    DrinkInfo drinkArray[5];
    void inputMoney(short);
    void dailyreport();

    public:
    DrinkMachine();
    void displayChoises();
    void buyDrink(short);
    ~DrinkMachine();
};
//#endif
DrinkMachine::DrinkMachine()
{   drinkArray[0]= DrinkInfo("Cola", 0.75,20);
    drinkArray[1]= DrinkInfo("Root beer",0.75,20);
    drinkArray[2]= DrinkInfo("Orange Soda",0.75,20);
    drinkArray[3]= DrinkInfo("Grape Soda",0.75,20);
    drinkArray[4]= DrinkInfo("Bottled Water",0.75,20);
}
void DrinkMachine::displayChoises()
{
    cout << "\tDRINK      " << "PRICE" << endl;
    for(int index = 0;index < 5;index++)
    {
    cout << "\t" << (index+1) <<"."<< setw(10) << drinkArray[index].drinkName << "$"
         << drinkArray[index].drinkPrice << endl;
    }
}

//buyDrink() function definiton
//calls the input money if the soda is avaliable
//otherwise displays a sorry message
void DrinkMachine::buyDrink(short choise)
{
    if(drinkArray[choise-1].drinkQuantity > 0)
    inputMoney(choise);

    else
    cout << "The specific soda has run out.We are sorry!\n";
}

void DrinkMachine::inputMoney(short sodaType)
{
    float moneyEntered = 0;
    float totalMoney = 0;
    cout << "PRICE $" << drinkArray[sodaType -1].drinkPrice;


        while(totalMoney <  drinkArray[sodaType -1].drinkPrice)
        {
            cout << "Please insert money or press 0 to esape.\n";
            cin  >> moneyEntered;
            totalMoney += moneyEntered;

            if(totalMoney == drinkArray[sodaType -1].drinkPrice)
           {
               cout << "Here is your drink.Thank You!" << endl;
               break;
           }
            else if(totalMoney > drinkArray[sodaType -1].drinkPrice)
            {    float change = totalMoney - drinkArray[sodaType -1].drinkPrice;

                 cout << "You change is " << change << endl;
                 cout << "Here is your drink.Thank you!" << endl;
                 break;
            }
            if(moneyEntered == 0)
            {
                cout << "Purchase never fiished!\n";
                cout << "Here is your $" << totalMoney << " back.\n";
                break;
           }
}

}
Here are the errors:

In constructor `DrinkMachine::DrinkMachine()':
error: no matching function for call to `DrinkInfo::DrinkInfo()'
note: candidates are: DrinkInfo::DrinkInfo(const DrinkInfo&)
note: DrinkInfo::DrinkInfo(std::string, float, int)
You need a DrinkInfo constructor with no arguments, you can change the constructor you provided using optional arguments.
Last edited on
are you sure?
because if I do that how can I use the arguments later
as you do in your example:

1
2
3
4
5
6
7
8
DrinkMachine::DrinkMachine()
{  
    drinkArray[0] = DrinkInfo("Cola", 0.75,20);
    drinkArray[1] = DrinkInfo("Root beer",0.75,20);
    drinkArray[2] = DrinkInfo("Orange Soda",0.75,20);
    drinkArray[3] = DrinkInfo("Grape Soda",0.75,20);
    drinkArray[4] = DrinkInfo("Bottled Water",0.75,20);
}


still getting related errors if I do it
You can have many different constructors in your class.
The constructor with no arguments is used when the array is initialized, you would use the constructor with the arguments when setting the values of the array elements.
Try changing the constructor you already have but using optional arguments:
DrinkInfo( string n = "", float p = 0, int q = 0 )
I would apreciate if you could post an example of running code
because Im really cant find out how to do it...
thanks anyway
Change line 7 of the code you posted with the line I provided in my previous post and it should run ( Except for the fact that you provided the declaration but not the body of the DrinkMachine constructor )
Last edited on
Program is running now!
thanks for all the help
Topic archived. No new replies allowed.