Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,264 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,224 people online right now. Registration is fast and FREE... Join Now!




functions...very last assignment

 
Reply to this topicStart new topic

functions...very last assignment

paintballfreak72
13 Apr, 2008 - 10:12 AM
Post #1

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

CODE
//program name: Vending Machine
//pregrammer name: Joe Deslauriers

#include <windows.h>
#include <iostream>
#include <process.h>
#include <iomanip>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;

void greetings (void);
void update_vending (int items[] , char x, int &i);
void thanks_for_shopping (int &i);

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr(int x, int y)
{
    COORD                       coordScreen = { x, y };
    DWORD                       cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO  csbi;
    DWORD                       dwConSize;
    HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '),
                               dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
                               dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
}

    void main(void)

{
    int items[6] = {5, 5, 5, 5, 5, 5};
    char x = '0';
    int i = 0;

    greetings( );

    update_vending(items, x, i);
    thanks_for_shopping(i);

        _getch();

}

    void greetings(void)
    {
        gotoxy(30,10);
        cout << "Welcome to Vitto's!" << endl;
        gotoxy(30,12);
        cout << "Mangiare, Mangiare!" << endl;

    }

    void update_vending (int items[], char x, int &i)
    {
        system("cls");
    
        gotoxy(30,8);
        cout << "VITTO'S  VENDETTA VENDING" << endl;

        gotoxy(20,10);
        cout << "ITEM #" << endl;

        gotoxy(23,12);
        cout << "1." << endl;

        gotoxy(23,13);
        cout << "2." << endl;

        gotoxy(23,14);
        cout << "3." << endl;

        gotoxy(23,15);
        cout << "4." << endl;

        gotoxy(23,16);
        cout << "5." << endl;

        gotoxy(23,17);
        cout << "6." << endl;

        gotoxy(23,18);
        cout << "N." << endl;

        gotoxy(30,10);
        cout << "ITEM NAME" << endl;

        gotoxy(30,12);
        cout << "Soprano Soup" << endl;

        gotoxy(30,13);
        cout << "Godfather Pasta" << endl;

        gotoxy(30,14);
        cout << "Gotti Gum" << endl;

        gotoxy(30,15);
        cout << "Capone Crisps"<< endl;

        gotoxy(30,16);
        cout << "Gambino Pie" << endl;

        gotoxy(30,17);
        cout << "Luciano Lunch" << endl;

        gotoxy(30,18);
        cout << "No More Purchases" << endl;

        gotoxy(51,10);
        cout << "PRICE" << endl;

        gotoxy(51,12);
        cout << "1.99" << endl;

        gotoxy(51,13);
        cout << "2.85" << endl;

        gotoxy(51,14);
        cout << "1.10" << endl;

        gotoxy(51,15);
        cout << "0.99" << endl;

        gotoxy(51,16);
        cout << "1.75" << endl;

        gotoxy(51,17);
        cout << "2.59" << endl;

        gotoxy(61,10);
        cout << "QTY" << endl;

        gotoxy(61,12);
        cout << items[0] << endl;

        gotoxy(61,13);
        cout << items[1] << endl;

        gotoxy(61,14);
        cout << items[2] << endl;

        gotoxy(61,15);
        cout << items[3] << endl;

        gotoxy(61,16);
        cout << items[4] << endl;

        gotoxy(61,17);
        cout << items[5] << endl;

        gotoxy(20,20);
        cout << "make a selection (1-6 or n)" << endl;

        gotoxy(20,21);
        cin >> x;

        do
        {
            if(x == '1')
            {
                items[0] = items[0]-1;
                i++;
                if(items[0] == 0)
                {                                        //if items[] reaches zero, it should say OUT and not allow the user to  
                    gotoxy(61,12);                        // enter another value
                    cout << "OUT" << endl;
                }
                gotoxy(61,12);
                cout << items[0] << endl;
                gotoxy(20,21);
                cin >> x;

            }

            if(x == '2')
            {
                items[1] = items[1]-1;
                i++;
                if(items[1] == 0)
                {
                    gotoxy(61,13);
                    cout << "OUT" << endl;
                }
                gotoxy(61,13);
                cout << items[1] << endl;
                gotoxy(20,21);
                cin >> x;
            }

            if(x == '3')
            {
                items[2] = items[2]-1;
                i++;
                if(items[2] == 0)
                {
                    gotoxy(61,14);
                    cout << "OUT" << endl;
                }
                gotoxy(61,14);
                cout << items[2] << endl;
                gotoxy(20,21);
                cin >> x;
            }
            if(x == '4')
            {
                items[3] = items[3]-1;
                i++;
                if(items[3] == 0)
                {
                    gotoxy(61,15);
                    cout << "OUT" << endl;
                }
                gotoxy(61,15);
                cout << items[3] << endl;
                gotoxy(20,21);
                cin >> x;
            }
            if(x == '5')
            {
                items[4] = items[4]-1;
                i++;
                if(items[4] == 0)
                {
                    gotoxy(61,16);
                    cout << "OUT" << endl;
                }
                gotoxy(61,16);
                cout << items[4] << endl;
                gotoxy(20,21);
                cin >> x;
            }
            if(x == '6')
            {
                items[5] = items[5]-1;
                i++;
                if(items[5] == 0)
                {
                    gotoxy(61,17);
                    cout << "OUT" << endl;
                }
                gotoxy(61,17);
                cout << items[5] << endl;
                gotoxy(20,21);
                cin >> x;
            }

        }while(x != tolower('N'));


    }

    void thanks_for_shopping(int &i)
    {
        system("cls");

        gotoxy(30,10);
        cout << "You have purchased " << i << " number of times" << endl;
        gotoxy(30,11);
        cout << "Thank you for shopping at Vitto's!" << endl;

    }



k so in the function "update_vending" this is what needs to be done.
-clear the screen //works
-display the menu //works
-prompt for selection of a item //works
-updates (in the array as well as on the menu) the quantity left of that specific item
-the user should be allowed to choose as many snacks from the vending machine as they wish (one at a time) (if the quantity left of an item reaches zero, then the quantity left for that item should be displayed as “OUT”, not 0, and the buyer should not be allowed to select that item
//this is the only part i cant get

one last thing, how do i get the computer to wait a certain time before it calls the other function and clears the screen? i need this for inbetween the "greetings" and "update_vending" functions.

thanks for the help


User is offlineProfile CardPM
+Quote Post


gabehabe
RE: Functions...very Last Assignment
13 Apr, 2008 - 10:47 AM
Post #2

i > u
Group Icon

Joined: 6 Feb, 2008
Posts: 7,381



Thanked: 154 times
Dream Kudos: 2850
Expert In: Lots of things.

My Contributions
To display "OUT", you have a few problems: (I'll take option 1 as an example, theyre all the same smile.gif)
ANOTHER EDIT: Sorry, I made a mistake
cpp

if(x == '1')
{
items[0] = items[0]-1;
i++;
if(items[0] <= 0)
{ //if items[] reaches zero, it should say OUT and not allow the user to
gotoxy(61,12); // enter another value
cout << "OUT" << endl;
}
else
{
gotoxy(61,12); //otherwise output the value
cout << items[0] << endl;
}
gotoxy(20,21); //update coordinates for input
cin >> x;
}

As for timing the output, try making a timer function - there is a tutorial for it on http://www.cplusplus.com but i honestly can't remember where... sorry sad.gif

Hope this helps smile.gif

EDIT: I found it smile.gif
http://www.cplusplus.com/reference/clibrary/ctime/clock.html

This post has been edited by gabehabe: 13 Apr, 2008 - 10:55 AM
User is offlineProfile CardPM
+Quote Post

paintballfreak72
RE: Functions...very Last Assignment
14 Apr, 2008 - 07:53 PM
Post #3

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

thanks sooo much for the help wink2.gif
works perfectly noww

one more thing, does anyone know how to use the "tolower" function...
its for at the end of the dowhile loop, so when the user doesnt want to purchase an item, and presses 'N' instead of 'n' it wont make a difference

thanks guys...i wanted to get into aviation but now im thinkin of bein some kind of programmer


edit**** nvm i found the solution...thanks neways

This post has been edited by paintballfreak72: 14 Apr, 2008 - 08:48 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 12:22PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month