Thread: Beginner C++ Help. My very first program.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Beginner C++ Help. My very first program.

    So, I started learning C++ last night and thought I'd mess around with a bit of basic stuff and see how far I could get on my own. It's a pretty simple program that's straightforward to see what it does if you look at the code:

    NOTE: Yeah, I know it's a pretty lame thing to make a program about, but it's my first one. :P

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int DP_Left;
        int DP_Fridge;
    
        cout<<"How many cans of Dr Pepper do we have? ";
        cin>> DP_Left;
        if ( DP_Left <= 3 ) {
            cout<<"We need more Dr Pepper!\n";
        }
        else if ( DP_Left <= 6 ) {
            cout<<"We're running out of Dr Pepper!\n";
        }
        else if ( DP_Left >= 12 && DP_Left <= 24 ) {
            cout<<"We have plenty of Dr Pepper!\n";
        }
        else if ( DP_Left > 24 && DP_Left < 50 ) {
            cout<<"We have a ton of Dr Pepper!\n";
        }
        else {
            cout<<"We have too much Dr Pepper! Share it with the others who love it!\n";
        }
    //Next Question
        if ( DP_Left >= 12 ) {
            cin.ignore();
            do
            {
                cout<<"How many Dr Pepper are there in the refrigerator?";
                cin>> DP_Fridge;
                cout<<"Put more Dr Pepper in the refrigerator!\n";
                DP_Fridge++; Dr Pepper in the
                cout<< endl;and asks again.
            } while ( DP_Fridge < 6 );
        }
        else {
            cin.get();
        }
    }
    Now what I'm trying to get it to do is if there are more than 6 Dr Pepper if the refrigerator, to print something like "There are plenty of dr pepper in the fridge." without adding to the 'DP_Fridge' integer. Obviously any constructive criticism would be greatly appreciated whether it's about my problem or any other part of my code.

    Thanks in advance!

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Could you not do something like this:
    Code:
    do
            {
                cout<<"How many Dr Pepper are there in the refrigerator?";
                cin>> DP_Fridge;
                cout<<"Put more Dr Pepper in the refrigerator!\n";
                DP_Fridge++; Dr Pepper in the
                cout<< endl;and asks again.
            } while ( DP_Fridge < 6 );
    cout << "There are plenty of dr pepper in the fridge.\n";
    That way, if there are fewer than 12 cans in the fridge, but more than 6 it will tell you that you have enough Dr. Pepper (note that the 'cout' statement is inside the if statement). Alternatively you could consider adding another part to the if statement or something like that. If you are worried about the last '++' on the last time you run it you could add an 'DP_Fridge--' after the 'cout' command.

    By the way, I forgot to say - Welcome to the forum!
    Last edited by Swarvy; 10-08-2010 at 02:35 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Why not use a switch case and get rid of those if's?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM