I am all confused. Need Help

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."

Thanks
closed account (48T7M4Gy)
I keep trying but still not getting it.

Show us what you have so far. :-)
YouTube C++ Tutorials

"C++ Tutorials,playlist" <---- type that into YouTube
Right, this is a simple one.

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
else if(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
}


Hope that helps :-)
Last edited on
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;

//if else clauses
if (appletype== "premimum")
{
unitcost = .40;
}
else if (appleType == "juicy")
{
unitcost = .30;
}
else if (appleType == "snack")
{
unitcost = .20;
}
else if (appleType == "cooking")
{
unitcost = .10;
}

// basic else case for invalid entry.... as in not one of the four types else
{
cout <<"Invalid entry"<< endl;
}

//calculations
total = (number*unitcost);
tax = .07*total;
total +=tax;
cout <<"Total cost of" <<number<<""<<
appleType<<"apple(s) is: $" <<total<<endl
return 0;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;
int main ()
{

    std::string input;

    //std::string appletype;
    std::string appleType;

    int number;

    //float "unicost,total,tax";
    float unitcost = 0,total = 0,tax = 0;

    cout << "Enter the number of apples you'd like t order:" << endl;
    //cin >> input;
    cin >> number;

//convert input string to int number=atoi(input.c_str());
    cout << "Enter the type of apples" << endl;
    //cin >> appletype;
    cin >> appleType;

//if else clauses
    //if (appletype== "premimum")
    if (appleType== "premium")
    {
        unitcost = .40;
    }
    else if (appleType == "juicy")
    {
        unitcost = .30;
    }
    else if (appleType == "snack")
    {
        unitcost = .20;
    }
    else if (appleType == "cooking")
    {
        unitcost = .10;
    }

// basic else case for invalid entry.... as in not one of the four types else
    else //<|---
    {
        cout <<"Invalid entry"<< endl;
        return -1; ///
    }

//calculations
    total = (number*unitcost);
    tax = .07*total;
    total +=tax;
    cout <<"Total cost of" <<number<<""<<
         //appleType<<"apple(s) is: $" <<total<<endl
         appleType<<"apple(s) is: $" <<total<<endl;
    return 0;
}

I need help with the "switch case" and I think I will be fine!
Topic archived. No new replies allowed.