Need help with C++ project

0 aj burnett · June 14, 2015
long story short,For my project I have to calculate the cost of a mobile device service. I ask a user what package they want package a, b, or c. they each have a base price. right now im trying to calculate the cost of package a which has a base price of $9.95. I also ask the user how many many message units they want. if they choose more than 5 message units its $1.00 per message unit after 5 units. I dont know if my math is not right? when i run it it gives the wrong output for the price.

#include <iostream>
using namespace std;

int main() {
bool finished = false;



do {

    // to keep it simple
    double choice_a = 9.95;
    double choice_b = 19.95;
    double choice_c = 39.95;
    char choice;
    int message_units;
    double price;
    bool selected = false;
// this loop shows the options initially
    do {
        cout << "Which package do you choose (enter A, B or C)" << endl;
        // you will need to check this
        cin >> choice;
        // keeping it simple
        if (choice == 'A') { price = choice_a; selected = true; }
        else if (choice == 'B') { price = choice_b; selected = true; }
        else if (choice == 'C') { price = choice_c; selected = true; }
        cout << endl;
    }
    // loops until something was selected
    while (selected == false);






    // user enters how many units is wanted
    cout << "How many message units (enter 1 - 672)" << endl;
    // again check this (if homework requires checking input)
    cin >> message_units;

    // Calculating message units
    if(message_units > 5){
price += 100 * (message_units - 5);
}
    if(message_units > 15){
price += 50 * (message_units - 15);
}



// Total Price Output
    
cout << "Your total cost is " << price/100 << endl



    // Is user done?
    char done;
    cout << "Do you want to enter another? press enter to continue. If you are done, type something and press enter.";
    cin >> done;

    // check
    if (done != ' ') {
        finished = true;
    }
}
while (finished = false);





Post a Reply

Replies

Oldest  Newest  Rating
0 Scott Walker · June 19, 2015
and finish off with an else in case neither of these are true :-)
0 Scott Walker · June 19, 2015
^^ Note for K D's bottom point, that will need to be an else if statement rather than 2 ifs 

e.g.

        if(message_units > 5 && message_units < = 15)
        {
            // logic here
        }
        else if(message_units > 15)
        {
            // logic here
        }
0 K D · June 19, 2015
fix the main loop condition

also move those declarations out side of the main loop:
int main() {

bool finished = false;
// to keep it simple
double choice_a = 9.95;
double choice_b = 19.95;
double choice_c = 39.95;
char choice;
int message_units;
double price;
bool selected; // this is initialized inside the loop
do {
    selected = false;
    ....
} while (finished == false);  // The way you had this will give you a Infinite loop because finished will always equal false

there is an easier way for the iomanip stuff if you want all floating point numbers outputted the same way:
at the top of main put:
cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);

this says  show only 2 decimal places and if there aren't 2 fill with 0s up to 2 decimal places

for the A part just do:
    price = choice_b + (message_units - 5);  // if its greater then 5

for the B part just do:
    price = choice_b + ((message_units - 15)/2);  // if its greater then 15

thats what i see in the small amount of time i have. really hope this helps :).
0 aj burnett · June 19, 2015
Ok  For my project I have to add 1.00 per message unit to package A if more than five message units entered. Also I have to add .50 per message unit to package B if more than 15 message units are entered. For example if a user picked package A and picked six units the correct price output should read 10.95. If a user picks package B and selects 16 units the the price should be 20.45 because the base price of B 19.95 + .50 is 20.45
0 Scott Walker · June 18, 2015
didn't realise you changed the top post :-)

1. include the iomanip library and change the line of code displaying price to the below.

// Total Price Output

        cout << "Your total cost is " << fixed << setprecision(2) << price/100 << endl;


2. Try to keep data types similar if using math as the compiler can get confused, e.g message should also be a double.

3. Your logic is wrong, the first section of the code is fine this below doesn't make sense

// Calculating message units
if(message_units > 5){
price += 100 * (message_units - 5);
}
if(message_units > 15){
price += 50 * (message_units - 15);

so say I chose option A at 9.95. That is now stored in price. Say I choose 10 message_units, it jumps into the if statement and minuses 5. So now it is 5 * 100 = 500. then 500 + 9.95.


so that equals 509.95.

now you're dividing it by 100 which C++ will round up to 5.1 (once you have used the setprecision.

cout << "Your total cost is " << price/100 << endl


There are numerous logic issues with the code:

1. if message_units is less than 5, the if statements will not be used so your initial prices will be divided by 100 (not what you want I presume)
2. similarly, if message_units is over 15 you are doing multiple sums
e.g.

choice A
message_units 20

message_units - 5 * 100 + price =  1509.95;

next if statement

message_units - 15 * 50 + price = 1769.95

hope my points help :-)

p.s. could you explain the message units thing? it makes no sense to me?
0 Scott Walker · June 18, 2015
Can you post the changed code? 
0 aj burnett · June 18, 2015
As you can tell Im a beginner. I redid the code. Now I cant figure out why when I try to get the price of package B it outputs the cost of package A as well and vice versa. I just need the price outputted for each individual package. Also since I changed the prices to doubles the decimal point is out of place when outputting the price?
0 Scott Walker · June 15, 2015
Remove the +a on the final else if statement :-) so it should just be Atotalcost = messageunits * a;

should really proofread my post before I submit.

For future reference and good practice use the camelCase method for declaring your variables.

The first word is always lower case, each word following is capitalised.

E.G.

aTotalCost or messageUnits :-)
0 Scott Walker · June 15, 2015
Ok so reading through your code there were a few errors

1. cin a || b || c will not work, you can error test this by quickly cout the answer following the decision.
2. setting your values within char will not work
3. your loops were unnecessary, they  didn't loop.

I have edited the code so it works how I presume it was meant to, if you're struggling just ask :-)
<code>

#include <iostream>
using namespace std;
int main()
{
    int messageunits, choice;
    float Atotalcost;

        cout << "Which package do you choose(enter a, b, c,)" << endl;
        cin >> choice;

        cout << "how many message units(enter 1 - 672)" << endl;
        cin >> messageunits;


        if(choice == 1)
        {
            float a = 9.95;
            Atotalcost = messageunits * a;
            cout << "Your total cost is " << Atotalcost << endl;
        }
        else if(choice == 2)
        {
            float a = 19.95;
            Atotalcost = messageunits * a;
            cout << "Your total cost is " << Atotalcost << endl;
        }
        else if(choice == 2)
        {
            float a = 39.95;
            Atotalcost = a + messageunits * a;);
            cout << "Your total cost is " << Atotalcost << endl;
        }

        else
        {
            cout << "Your total cost is 9.95" << endl;
        }

    return 0;

}

</code>
0 K D · June 15, 2015
I am truly sorry if I come off blunt and/or rude I really don't mean to...


why are the prices chars they should be floats and Atotalcost should be a float. the way you have Atotalcost right now you will never see the cents because its value gets truncated. If you keep a, b, and c as a char I think you will actually get the equivalent char after the truncation of it but i am not sure.  What i do know tho is it won't contain 9.95 or any thing like it.

how are you making a choice for the package to use in the formula? what is the point of writing over a, b, or c if you are going to be needing that value for a formula later in the program?  i would suggest making another variable for that input statement...
By the way what are you trying to do with the "cin >> a || b || c;" i have never seen anything like that? I doubt it will do what you want it to...

shouldn't the formula be:
Atotalcost = a + (messageunit - 5);


why do all of your "do whiles loops" have x < 2 its always going to be equal to two because you start at 1 and increment right after input.  If you are using a Do While loop it will always execute at least Once even if x was equal to 10,000 so no point in having it check x.  why not have it check what they entered?

its best if you have the "{" and "}" around the blocks of code contained in else, if, else if, while, and for statements even if its one line of code because it makes it easier to read and to know what will happen.  Also did it just cut it off or is that the whole thing.  If that is the whole thing it won't even compile because of the fact that you don't have a return statement or a closing brace for the main.

If you are having trouble learning this I would highly suggest Bucky's c++ tutorials on this site.
if you need me to clarify any of that please ask I will be glad to help... :)
  • 1

C++

106,974 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator