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

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




Help with First Program

 
Reply to this topicStart new topic

Help with First Program

kiasta
25 Nov, 2007 - 02:07 PM
Post #1

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Hello everyone, I am having a really hard time with my code. I am trying to create a soda machine program. When someone inputs too low of a number it goes to my loop which continues until the correct amount has been given. However, I can't figure out how to add the input number with itself...Sorry but I'm not good at explaining things...here is my loop:

CODE
else if (selection == 2)
    {
        float deposit = 0;
        cout << "Please insert $1.25" << endl;
        cout << "Amount you wish to deposit: $";
        cin >> deposit;
        machine = deposit;

        if (deposit == 1.25)
            cout << "Thank you and enjoy your Sprite!" <<endl;
        else if (deposit > 1.25)
            cout << "Please take your change, Thank You! $" << machine << endl;
        else
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
            cout << "Amount Needed: $" << botPrice - deposit << endl;
            deposit = botPrice - deposit;
            cin >> deposit;

            while (machine < botPrice)
            {
                cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
                cout << "Amount Needed: $" << botPrice - deposit << endl;
                deposit = botPrice - deposit;
                cin >> deposit;
            }  




If you would like to look at my full source code I have attached it just rename it to .cpp or keep it as a .txt file whatever you prefer. Thank you for your time ^^. Oh BTW I am using Microsoft Visual C++ 2008 Express if that matters.

This post has been edited by kiasta: 25 Nov, 2007 - 02:10 PM


Attached File(s)
Attached File  Coke_Machine.txt ( 10.81k ) Number of downloads: 21
User is offlineProfile CardPM
+Quote Post

Bench
RE: Help With First Program
25 Nov, 2007 - 02:38 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
First off, I think your indentation is misleading you. MSVC++ should be able to indent this code for you, so that you can see what's really going on.

(Re-formatted using MSVC++'s auto-indent)
CODE

    else if (selection == 2)
    {
        float deposit = 0;
        cout << "Please insert $1.25" << endl;
        cout << "Amount you wish to deposit: $";
        cin >> deposit;
        machine = deposit;

        if (deposit == 1.25)
            cout << "Thank you and enjoy your Sprite!" <<endl;
        else if (deposit > 1.25)
            cout << "Please take your change, Thank You! $" << machine << endl;
        else
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
        cout << "Amount Needed: $" << botPrice - deposit << endl;
        deposit = botPrice - deposit;
        cin >> deposit;

        while (machine < botPrice)
        {
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
            cout << "Amount Needed: $" << botPrice - deposit << endl;
            deposit = botPrice - deposit;
            cin >> deposit;
        }  


Notice the code at your 'else' statement - the only line which runs under the else condition is the first one. all the others are part of a different block, according to the rules of C++.

if you wish to wrap multiple lines into an if/else statement, then you need the curly parenthesis -

CODE

        else
        {
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
            cout << "Amount Needed: $" << botPrice - deposit << endl;
            deposit = botPrice - deposit;
            cin >> deposit;

            while (machine < botPrice)
            {
                cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
                cout << "Amount Needed: $" << botPrice - deposit << endl;
                deposit = botPrice - deposit;
                cin >> deposit;
            }
        }


Also, think about the order in which your instructions are being run. you have two instructions here -
CODE
            deposit = botPrice - deposit;
            cin >> deposit;

The first of which will have no effect, since you store the result of a calculation in 'deposit' then on the very next line, you ask the user to input a value into it.
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Help With First Program
25 Nov, 2007 - 03:14 PM
Post #3

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Oh I see, I didn't know that about the indentation...I guess I picked up some bad habits from python lol, now I understand the reason it was acting funny. Anyways, about these instructions:

CODE
            deposit = botPrice - deposit;
            cin >> deposit;


I was trying to get the user input and then add upon itself every time they input a new number until it equaled the proper number, which is (in this circumstance) 1.25. I thought that maybe it would add on to it automatically but it did not work that way, and I have no idea how to add to deposit without it storing a new value, well I'm going to try a couple of things, if you have any more advise i would appreciate it. Thanks alot for the advise.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Help With First Program
25 Nov, 2007 - 05:27 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(kiasta @ 25 Nov, 2007 - 11:14 PM) *

Anyways, about these instructions:

CODE
            deposit = botPrice - deposit;
            cin >> deposit;


I was trying to get the user input and then add upon itself every time they input a new number until it equaled the proper number, which is (in this circumstance) 1.25. I thought that maybe it would add on to it automatically but it did not work that way, and I have no idea how to add to deposit without it storing a new value, well I'm going to try a couple of things, if you have any more advise i would appreciate it. Thanks alot for the advise.

the >> operation will cause your deposit variable to be filled up with whatever the user inputs. Anything which was in that memory space before is gone. If you want to keep the value of deposit, or change it based on user input, then I would suggest that read the input into a new area of memory (you could create a temporary input variable), and perform any calculations with your new input and deposit seperately.
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Help With First Program
25 Nov, 2007 - 05:42 PM
Post #5

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
WooHoo! I figured it out, it was actually quite simple really, all I did was made a new variable when a number was input then I...well it's easier if you just look.

OLD CODE:

CODE
    else if (selection == 2)
    {
        float deposit = 0;
        cout << "Please insert $1.25" << endl;
        cout << "Amount you wish to deposit: $";
        cin >> deposit;
        machine = deposit;

        if (deposit == 1.25)
            cout << "Thank you and enjoy your Sprite!" <<endl;
        else if (deposit > 1.25)
            cout << "Please take your change, Thank You! $" << machine << endl;
        else
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
        cout << "Amount Needed: $" << botPrice - deposit << endl;
        deposit = botPrice - deposit;
        cin >> deposit;

        while (machine < botPrice)
        {
            cout << "Please insert the correct amount of change. $" << (botPrice - deposit) << endl;
            cout << "Amount Needed: $" << botPrice - deposit << endl;
            deposit = botPrice - deposit;
            cin >> deposit;
        }


NEW CODE:

CODE
    else if (selection == 7)
    {
        cout << "Please insert $" << canPrice << endl;
        cout << "Amount you wish to deposit: $";
        cin >> deposit;
        machine = deposit;
        
        if (deposit == canPrice)
        {
            cout << "Thank you and enjoy your Coke!" <<endl;
        }
        else if (deposit > canPrice)
        {
            cout << "You have inserted $" << machine << endl;
            cout << "Please take your change of $" << (machine - canPrice) << " Thank You!" << endl;
        }    
        else if (deposit < canPrice)
        {
            while (machine < canPrice)
            {
                cout << "Please insert the correct amount of change." << endl;
                cout << "Amount Needed: $" << (canPrice - machine) << endl;
                cin >> newdep;
                machine = (newdep + machine);
                deposit = newdep;
            }
            if (machine == canPrice)
            {
                cout << "Thank you and enjoy your Coke!" <<endl;
            }
            else if (machine > canPrice)
            {
                cout << "You have inserted $" << machine << endl;
                cout << "Please take your change of $" << (machine - canPrice) << " Thank You!" << endl;
            }
        }
    }
}


And I redid my code (Attached) made it smaller and more efficient. Not bad after 2 day's of watching movies and reading pdf's and your help Bench, thanks again!

This post has been edited by kiasta: 25 Nov, 2007 - 06:52 PM


Attached File(s)
Attached File  Soda_Machine.txt ( 13.49k ) Number of downloads: 29
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:23PM

Be Social

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

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month