Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Subtracting from a struct element
New Posts  All Forums:Forum Nav:

Subtracting from a struct element

post #1 of 3
Thread Starter 
My program is supposed to read from the user a soda name, the price and the quantity. Then the user makes makes a selection of what they want.

Here's where I'm stuck, in my transaction function will make sure the product is in stock and if it's not it will tell the user.
It also must input the money from the user, it must be less than a dollar and also calculate the change.

Anyway, here's what I've got so far. This is my first program dealing with structs so I'm still learning.

Code:

#include <iostream>

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


const int SIZE = 10;
struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink machine[], int &numitems);
void getChoice (Drink machine[], int numitems, int i, int &choice);
void transaction (Drink machine[], int &numitems, int i, int &choice);

int main()
{
    int i = 0;
    int numitems = 0;
    int choice = 0;
    Drink machine[SIZE];

    
    fillMachine (machine, numitems);
    getChoice (machine, numitems, i, choice);
    transaction (machine, numitems, i, choice);
   
    
    return 0;
}


    
void fillMachine (Drink machine[], int &numitems)
{
    string mydrink;
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   getline(cin, mydrink);
   
   while (mydrink != "Q" && numitems < SIZE)
   {
       machine[numitems].Name = mydrink;
       // Get the price
       cout << "Enter the price: ";
       cin >> machine[numitems].Cost;

       // Get the quantity
       cout << "Enter the quantity: ";
       cin >> machine[numitems].numDrinks;
       cin.ignore();
       numitems++;
       cout << "Enter the first drink, Q to quit: ";
       getline(cin, mydrink);
    }

}

void getChoice (Drink machine[], int numitems, int i, int &choice)
{
cout << endl;
for (i=0;i<numitems;i++)
    cout << (i+1) << ". " << machine[i].Name  << endl;
cout << (i+1) << ". Exit" << endl;

cin >> choice;

if (choice > (numitems+1))
    cout << "Bad choice." << endl;

cout << endl;


}




void transaction (Drink machine[], int &numitems, int i, int &choice)
{







    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #2 of 3
Since structs are public, their members can be directly accessed.

Code:
Drinks first;
cout << first.numDrinks << endl; // displays how many are left
post #3 of 3
Thread Starter 
Quote:
Originally Posted by floatingDivs View Post
Since structs are public, their members can be directly accessed.

Code:
Drinks first;
cout << first.numDrinks << endl; // displays how many are left
Do I have to declare first?

Edit: I figured it out.
Edited by skatingrocker17 - 2/18/11 at 7:00am
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Subtracting from a struct element