Answer to Question #159554 in C++ for Earl

Question #159554

A street vendor sells three types of juice: apple, grape, and orange. They all sell for $2.49. Ask the vendor how much of each were sold today and then calculate the sales ($) for each. Format your output as shown below, making sure Sales numbers have exactly two decimal places and that all widths / alignments match mine.

 

Output:

How much apple juice did you sell today?       [assume user inputs 5]

How much grape juice did you sell today?       [assume user inputs 8]

How much orange juice did you sell today?      [assume user inputs 10]

FRUIT    QTY SALES($)

Apple     5   12.45

Grape     8   19.92

Orange    10   24.90

-------------------------

1234567890123456789012345 <-- DO NOT output this area. It is here to help you align things.


1
Expert's answer
2021-02-02T10:34:12-0500
#include <iostream>
#include <iomanip>

int query(const std::string& juice){
    int amount;
    std::cout << "How much " << juice << " did you sell today?\n";
    std::cin >> amount;

    return amount;
}

int main() {
    int apple, grape, orange;

    apple = query("apple");
    grape = query("grape");
    orange = query("orange");

    std::cout << std::fixed;
    std::cout << std::setprecision(2);

    std::cout << "FRUIT    QTY SALES($)\n";
    std::cout << "Apple     " << apple <<"   " << apple*2.49 << std::endl;
    std::cout << "Grape     " << grape <<"   " << grape*2.49 << std::endl;
    std::cout << "Orange    " << orange <<"   " << orange*2.49 << std::endl;
    std::cout << "-------------------------";

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog