I have never programed in my life and have to take the C++ for my engineering class. Once I get it, I totally get confused again. Can someone help me with this solution to this question: I keep trying but still not getting it.
"The Ajax apple company has four grades of apple; Premium, juicy, snack and cooking. The cost per apples is : Premium (40 cents), juicy (30cents), snack(20 cents) and cooking (10cents). You are to write 2 programs, one using “if-else” and the other using “Switch-case” which allows a user to input the number and type of apples desired and print out the total cost. Be sure to add a 7% sales tax to the final price for the governor."
The Ajax apple company has four grades of apple; Premium, juicy, snack and cooking.
This tells us there are 4 types of apples, with 4 different costs. We can store all those costs in an array of numbers, and we can retrieve them with an enumeration.
1 2
int Apples[4] = { 40, 30, 20, 10 }
enum Type { Premium, Juicy, Snack, Cooking };
The above code would Allow us to do something such as:
1 2
float Total = Apples[Premium]*4; // Cost of 4 premium apples
float TotalWithTax = (Apples[Premium]*4)*1.07; // Cost of 4 premium apples with 7% tax
You are to write 2 programs, one using “if-else” and the other using “Switch-case” which allows a user to input the number and type of apples desired
Right then..
The if-else chain can be implemented a couple of ways, I'll show you one example:
1 2 3 4 5 6 7 8 9 10
std::cout << "Please choose an apple type\n";
std::cout << "0) Premium \t 1) Juicy \t 2) Snack \t 3) cooking \n";
int choice; choice = 0;
std::cin >> choice; // User enters a number, 0 1 2 or 3 for desired product
std::cout << "How many?\n";
int sum; sum = 0;
std::cin >> sum; // User enters amount they want
if(choice == Premium) // Do what is necessary
elseif(choice == Juicy)
This switch is similar, except we have cases:
1 2 3 4 5 6 7 8 9 10 11
std::cout << "Please choose an apple type\n";
std::cout << "0) Premium \t 1) Juicy \t 2) Snack \t 3) cooking \n";
int choice; choice = 0;
std::cin >> choice; // User enters a number, 0 1 2 or 3 for desired product
switch(choice)
{
case Premium: /* code here */ break;
case Juicy: // Do stuff
}
Kmort: You requested for what I put up.. This is it. I am not sure of it.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string input;
std::string appletype;
int number;
float "unicost,total,tax";
cout << "Enter the number of apples you'd like t order:" << endl;
cin >> input;
//convert input string to int number=atoi(input.c_str());
cout << "Enter the type of apples" << endl;
cin >> appletype;