delgadough
delgadough

Reputation: 21

Structures, arrays, functions

Newbie here, having some difficulty with my structured array arguments being sent into the function. It appears my basic bloodshed compiler is not liking the functions I setup. I have made some improvements with the program overall but have been struggling with this for hours. I tried looking at comparable programs on this site but nothing similar enough for me to pin point the errors. My inventory function's job is to send a message saying sold out when the drink such as cola = 0. Any help would be greatly appreciated. I have moved my function definition from the bottom after int main to the top to clear a different compiler error. I am open to all feedback of any part of this program as it is a class assignment and can use pointers to have success on my next test. Thanks

   #include<iostream>
#include<iomanip>
using namespace std;

struct Soda
{
   string name;
   float  price;
    int    inv;
};

void functInventory(Soda[],int);       //prototype

void functInventory(Soda drink[],int num)       //function definition
{
      if ( drink[num].inv = 0)
      cout << "SOLD OUT" <<endl;
}

int main()
{
    const int option = 5;
    string cola, rbeer, lemlime, grape, cream;

    int InsAmount, choice;
    int income = 0;

    Soda array[option] =    {
                            {cola,   .75, 20},
                            {rbeer,  .75, 20},
                            {lemlime,.75, 20},
                            {grape, .80,   20},
                            {cream, .80,   20}
                          }; 

     cout << "Please choose 1-6 " << endl;
     cout    << " 1. Cola = $.75 " << endl;
     cout  << " 2. Root Beer = $.75 "  << endl;
     cout << " 3. Lemon Lime = $.75 " << endl;
     cout  << " 4. Grape Soda = $.80 " << endl;
     cout << " 5. Cream Soda = $.80 " << endl;
     cout << " 6. QUIT & EXIT "       << endl;

  switch(choice)
     {
       case 1:   array[0].inv - 1 = array[0].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;

       case 2:    array[1].inv - 1 = array[1].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;

       case 3:    array[2].inv - 1 = array[2].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;

       case 4:    array[3].inv - 1 = array[3].inv;
                  income = income + .80;
                  functInventory(array, choice);
                  break;

       case 5:    array[4].inv - 1 = array[4].inv;
                  functInventory(array, choice)
                  income = income + .80;
                  break;

       case 6:  cout << "The total amount earned is: $" << income <<endl;
                  break;

        default:
                cout <<  "Please enter a capital letter from 1-6 " << endl;
   }

     cout << "Please Tell Me How Much Money You Are Inserting:  " <<endl;
     cin  >> InsAmount;

     change = InsAmount - .75;        //I will work on creating a more accurate function 4 change
     cout << "your change is " << change; << "  "<< endl;
    return 0;

     }

Upvotes: 0

Views: 207

Answers (2)

austin
austin

Reputation: 5876

The errors you are getting from the compiler are probably like the following:

test2.cpp:46:46: error: lvalue required as left operand of assignment

In your switch statement, your assignment statements (the first line in each case) have a value on the left side of the equal sign instead of the right side. It's kind of like saying:

int my_age;
26 = my_age;

which isn't going to work. Variables are your lvalues here.

Upvotes: 0

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154035

You said Bloodshed? I think this is based on gcc: turn on warnings, e.g., -W -Wall and possibly others! This would have told you that this is probably not what you meant to do:

if ( drink[num].inv = 0)

This is an assignment, not a comparison! You probably meant

if (drink[num].inv == 0)

... or, to prevent an accidental = to mess your logic up

if (0 == drink[num].inv)

... because this would cause a compiler error if you tried to use an assignment. I haven't checked for other errors but this seems to be an obvious one. Since it is a recent habit of mine, I shall also point out that you shouldn't use std::endl.

Looking a bit further down in the code: This shouldn't even compile:

array[0].inv - 1 = array[0].inv;

The results of the expression array[0].inv - 1 is a temporary built-in type and I don't think you can assign to this type (and it certainly fails when I try to compile the code; when I try to compile it it nicely emits a warning about the problem I mentioned above).

Upvotes: 1

Related Questions