Thread: I am working on homework regarding C++ selection programming and need help.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    12

    I am working on homework regarding C++ selection programming and need help.

    I am struggling with the part of the code that deals with calculations of a buy one get one half deal.

    here is what i have thus far.

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {


    //declare variables
    double itemOnePrice = 0.0;
    double itemTwoPrice = 0.0;
    char discountHalfOff = ' ';


    cout << "Enter item one price: ";
    cin >> itemOnePrice;
    cout << "Enter item two price: ";
    cin << itemTwoPrice;
    cout << "Discount? If yes press Y if no press any other key: ";
    cin >> discountPercentage;


    //calculate total owed
    total = itemOnePrice + itemTwoPrice / discountHalfOff;


    //Calculate total owed, if necessary
    if (toupper (discountPercentage) == 'Y')
    { total = itemOnePrice + itemTwoPrice / discountPercentage;

    After that I am lost as to what I need to do because I feel like this is incorrect (at least the calculations part). I think my if statement should be more specific, but I am unsure as to how to set that up.

    Thank you all in advance.

  2. #2
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    If you want to keep the current variables you have then you need to initialize a discountPercentage with ".50". Then read the char into discountHalfOff. As for your if statement it's if discountHalfOff=='Y' then total = itemOnePrice + itemTwoPrice / discountPercentage, and if not then just add up price one and price two.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Quote Originally Posted by setleaf View Post
    If you want to keep the current variables you have then you need to initialize a discountPercentage with ".50". Then read the char into discountHalfOff. As for your if statement it's if discountHalfOff=='Y' then total = itemOnePrice + itemTwoPrice / discountPercentage, and if not then just add up price one and price two.

    Would this be more accurate?

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {


    //declare variables
    double itemOnePrice = 0.0;
    double itemTwoPrice = 0.0;
    char discountHalfOff = ' ';


    cout << "Enter item one price: ";
    cin >> itemOnePrice;
    cout << "Enter item two price: ";
    cin << itemTwoPrice;
    cout << "Discount? If yes press Y if no press any other key: ";
    cin >> discountPercentage with ".50";


    //Calculate total owed, if necessary
    if (toupper (discountPercentage) == 'Y')
    { total = itemOnePrice + itemTwoPrice / discountPercentage;


    //calculate total owed
    total = itemOnePrice + itemTwoPrice / discountHalfOff;

    The discount must be off the lowest priced item.
    Thank you for your help.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Hi, I noticed a few things:
    - discountPercentage is not declared in the beginning, yet you try to assign it with cin. I'm assuming you meant to type discountHalfOff...
    - total is not declared
    - the final user input should go to the char variable
    - your if statement is missing a closing curly brace
    - remember without braces: multiplication/division occurs before addition/subtraction

    sidenotes:
    - I'm assuming discountPercentage has value 2, based on your formula. So I guesss percentage is not really a good word
    - maybe use shorter variable names

    EDIT:based on your first post.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    insert
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    
    
    //declare variables
    double itemOnePrice = 0.0;
    double itemTwoPrice = 0.0;
    char discountHalfOff = ' ';
    
    
    cout << "Enter item one price: ";
    cin >> itemOnePrice;
    cout << "Enter item two price: ";
    cin << itemTwoPrice;
    cout << "Discount? If yes press Y if no press any other key: ";
    cin >> discountHalfOff with ".50";
    //Calculate total owed, if necessary
    if (toupper (discountPercentage) == 'Y')
    { total = itemOnePrice + itemTwoPrice / discountPercentage;
    
    
    //calculate total owed
    total = itemOnePrice + itemTwoPrice / discountHalfOff;
    
    
    	{
    
    
    		total = itemOnePrice + itemTwoPrice / 2;
    
    
    	
    	}
    	cout << "Total Owed: $" << total << endl;
    
    
    	return 0;
    }
    Is this more accurate. The program must take the half off of the lowest priced item. Thank you for your notes on the program

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    This is an updated version of my program. It is still not working in Visual Studio, if you could point out my errors I woudl be greatly appreciative.

    Code:
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	//declare variables
    	double costOfItemOne = 0.0;
    	double costOfItemTwo = 0.0;
    	char discountHalfOff = ' ';
    	double total = 0.0;
    
    
    	cout << "Enter item one cost: ";
    	cin >> costOfItemOne;
    	cout << "Enter item two cost: ";
    	cin >> costOfItemTwo;
    	cout << "Discount the lowest costing item: ";
    	cin >> discountHalfOff;
    
    
    	//calculate total owed
    	total = CostOfItemOne + costOfItemTwo / discounHalfOff;
    
    
    	//calculate discount, if necessary
    	if (toupper(discountHalfOff) == 'Y')
    	
    	{
    
    
    	total = costOfItemOne + costOfItemTwo / discountHalfOff;
    
    
    	
    	}
    	cout << "Total Owed: $" << total << endl;
    
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    - your formula without discount contains a typo in a variable name
    - that formula should be different from the case with discount
    - you are dividing a number (double) by a character (char)
    Last edited by jiggunjer; 03-21-2015 at 03:05 PM.

  8. #8
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    I fixed the spelling error. When you say 'the formula should be different from the case with discount' which section of the program are you talking about? the calculation part?

    Could you possibly copy (not change) and make bold the sections of my program you are talking about so that I know what is ok and what I must fix. Sorry, I am new to this.
    Last edited by brittb93; 03-21-2015 at 03:18 PM.

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Sorry im on my mobile now so ill just use bulletpoints. I was talking about the calculation part.
    - both calculations of total are the same, obviously they should differ
    - i think you need <cctype> instead of <iomanip>
    - so you need to fix the 2 lines starting with "total ="
    P.s. dont worry I'm new too, just keep at it! Im also still learning.

  10. #10
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Should I delete the text for a numerical value on one of the "total =" lines such as total = costOfItemOne + costOfItemTwo / 2; or something more like total = total / 2?

    I am still working on the writing of them and my book is so incredibly confusing. Thank you for all of your help

    also should I change the char variable to double or integer?

  11. #11
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Yes delete the text and "hardcode" the 2. Or make a new numerical variable and assign it the value 2. As it is now, when the user types 10;5;'Y' you calculate 10+5/'Y' which of course makes no sense.

  12. #12
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    int main()
    {
        //declare variables
        double costOfItemOne = 0.0;
        double costOfItemTwo = 0.0;
        char discountHalfOff = ' ';
        double total = 0.0;
     
     
        cout << "Enter item one cost: ";
        cin >> costOfItemOne;
        cout << "Enter item two cost: ";
        cin >> costOfItemTwo;
        cout << "Discount the lowest costing item: ";
        cin >> discountHalfOff;
     
     
        //calculate total owed
        total = costOfItemOne + costOfItemTwo / discountHalfOff;
     
     
        //calculate discount, if necessary
        if (toupper(discountHalfOff) == '2')
         
        {
     
     
        total = costOfItemOne + costOfItemTwo / 2
     
     
         
        }
        cout << "Total Owed: $" << total << endl;
     
     
        return 0;
    }
    If this is still not even close can you give me an example of hardcoding the 2 in?

  13. #13
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    The second total line(with the 2) looks good, except for a missing semicolon. Now just fix the first calculation (the total before the if statement).

    edit: noticed you also replaced the halfOff variable in your if() condition, that is wrong. It seems you don't realize the halfOff has no purpose in the calculation itself, halfOff is just a character that is used to determine if the alternative total calculation should be done or not.
    Last edited by jiggunjer; 03-21-2015 at 04:41 PM.

  14. #14
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    would i rewrite that line to be total = total - discountHalfOff

    or total = total / 2

  15. #15
    Guest
    Guest
    Quote Originally Posted by brittb93 View Post
    would i rewrite that line to be total = total - discountHalfOff

    or total = total / 2
    As jiggunjer already pointed out in post #11, discountHalfOff is a character ("Y") so you would not do math with it. I think it would help if you explained your goal better, i.e. what a "buy one get one half deal." is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu selection with arrow key in c programming
    By slyern in forum C Programming
    Replies: 1
    Last Post: 06-22-2014, 08:37 AM
  2. Working on homework, hit a snag
    By devisezni in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2012, 03:14 PM
  3. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  4. Replies: 5
    Last Post: 04-03-2011, 02:05 PM
  5. Working with Nested "Switch Multiple-Selection"
    By MAV_DevWantB in forum C Programming
    Replies: 5
    Last Post: 09-18-2009, 08:05 AM

Tags for this Thread