Vectors Menu Switch


I need help making a menu using vectors this is what I have so far, I don't know how to combine it.

// This program demonstrates the vector's empty member function.
#include <iostream>
#include <vector>
#include <iomanip> //setprecision library.
#include <cmath> //For using math formula
using namespace std;

// Function prototype
double avgVector(vector<int>);

int main()
{
vector<int> values; // A vector to hold values
int numOfClasses; // The number of values
double average; // To hold the average
//defining variables.
int choice,tipChoice,averageGrades;
double bill, tipBill, totalBill;


//Introducing user to the program.
cout << " Tip and average grade calculator\n";
cout << "Choose one option\n";
do
{
//User chooses the options he or she wants to use.
cout << "-------------------------\n";
cout << "[1] Tip Calculator\n";
cout << "[2] Average Grade Calculator\n";
cout << "[3] Quit Program\n";
cin >> choice;
cout << "\n";

// Set the numeric output formatting.
cout << fixed << showpoint << setprecision(1);

switch (choice)
{
case 1:
cout << "Tip Calculator\n";

do
{
//Display the options for tip calculator
cout << "Pick the tip percentage you would like to give\n";
cout << "[1] 8%\n";
cout << "[2] 10%\n";
cout << "[3] 15%\n";
cout << "[4] 20%\n";
cout << "[5] Go Back to Menu\n";
cin >> tipChoice;

//User chooses the options he or she wants to use
//for the tip calculator.
switch (tipChoice)
{
//User inputs the price of the bill, the tip is added to it
//which then then the program displays the total price of the bill.
case 1:
cout << "Please enter the price of your bill.\n";
cin >> bill;
tipBill = bill * .08;
cout << "With a 8% tip being " << tipBill << "$\n";
totalBill = bill + tipBill;
cout << "The price of your bill with tip is " << totalBill << "$\n";
break;
case 2:
cout << "Please enter the price of your bill.\n";
cin >> bill;
tipBill = bill * .10;
cout << "With a 10% tip being " << tipBill << "$\n";
totalBill = bill + tipBill;
cout << "The price of your bill with tip is " << totalBill << "$\n";
break;
case 3:
cout << "Please enter the price of your bill.\n";
cin >> bill;
tipBill = bill * .15;
cout << "With a 15% tip being " << tipBill << "$\n";
totalBill = bill + tipBill;
cout << "The price of your bill with tip is " << totalBill << "$\n";
break;
case 4:
cout << "Please enter the price of your bill.\n";
cin >> bill;
tipBill = bill * .20;
cout << "With a 20% tip being " << tipBill << "$\n";
totalBill = bill + tipBill;
cout << "The price of your bill with tip is " << totalBill << "$\n";
break;
case 5:
cout << "Closing the program\n";
break;
}
} while ((tipChoice > 0 && tipChoice < 5));
break;
case 2:
cout << "Average Grade Calculator\n";
do
{
cout << " Please pick one\n";
cout << " --------\n";
cout << "[1] Calculate your Average Grade\n";
cout << "[2]Go back to menu\n";
cin >> averageGrades;

switch (averageGrades) //switching cases based on user input
{
case 1:
cout << "Please enter the number of classes you have taken : ";
cin >> numOfClasses;
do
{
// Get the values and store them in the vector.
for (int count = 0; count < numOfClasses; count++)
{
int gradeValue;
cout << "Enter a value: ";
cin >> gradeValue;
values.push_back(gradeValue);
}
// Get the average of the values and display it.
average = avgVector(values);
cout << "Average: " << average << endl;
return 0;
}
double avgVector(vector<int> vect)
{
int total = 0; // accumulator
double avg; // average

case 1: (vect.empty()) // Determine if the vector is empty
{
cout << "No values to average.\n";
avg = 0.0;
}
case 2:
{
for (int count = 0; count < vect.size(); count++)
total += vect[count];
avg = total / vect.size();
}
return avg;
}
break;
case 2: cout<< "Quit program\n";
break;
}
}while (choice! = 2);
system ("pause");
return 0;
}
Last edited on
Hello ValDe,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code. Along with proper indenting.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

As I was going over your code I found some problems. There are two missing closing curly braces missing at the end of the program.

This section of code is all messed up:
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
		case 2:
			cout << "Average Grade Calculator\n";
			do
			{
				cout << " Please pick one\n";
				cout << " --------\n";
				cout << "[1] Calculate your Average Grade\n";
				cout << "[2]Go back to menu\n";
				cin >> averageGrades;

				switch (averageGrades) //switching cases based on user input
				{
				case 1:
					cout << "Please enter the number of classes you have taken : ";
					cin >> numOfClasses;
					do
					{
						// Get the values and store them in the vector.
						for (int count = 0; count < numOfClasses; count++)
						{
							int gradeValue;
							cout << "Enter a value: ";
							cin >> gradeValue;
							values.push_back(gradeValue);
						}
						// Get the average of the values and display it.
						average = avgVector(values);
						cout << "Average: " << average << endl;
						return 0;
					}
					double avgVector(vector<int> vect)
					{
						int total = 0; // accumulator
						double avg; // average

				case 1: (vect.empty()) // Determine if the vector is empty
				{
					cout << "No values to average.\n";
					avg = 0.0;
				}
				case 2:
				{
					for (int count = 0; count < vect.size(); count++)
						total += vect[count];
					avg = total / vect.size();
				}
				return avg;
					}
					break;
				case 3: cout << "Quit program\n";  // <--- Changed case number.
					break;
				}
			} while (choice != 2);
			system("pause");
			return 0;
		}

lines 54 and 55 should be just before the closing brace of main.

I do not know if you realize it or not , but what comes after the key word "case" must be unique, i.e., no duplicate case numbers or letters.

Since you are repeating case numbers at the end it makes me thing that you are missing a switch.

I need to do some reformatting and figure out what goes with what and how the switches flow.

Hope that helps for now,

Andy
> I need help making a menu using vectors this is what I have so far, I don't know how to combine it.

It is generally easier to combine functionality if we write many small functions, each function doing just one thing.
In particular, avoid very long functions and deeply nested control structures.

Doing this is also conducive to writing correct code, testing each step before moving on to the next step.
Write one function, test it, get it working perfectly, and then move on to the next function.

For example:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

// display a menu (the items to display are in the vector)
void show_menu( const std::string& title, const std::vector<std::string>& menu )
{
    std::cout << '\n' << title << "\n\n" ;
    for( int i = 0 ; i < menu.size() ; ++i )
        std::cout << '[' << i+1 << "] " << menu[i] << '\n' ;
    std::cout << '\n' ;
}

// get an integer from stdin in the interval [minv,maxv]
// prompt ask for fresh input in case of input errors
int get_int( int minv, int maxv )
{
    std::cout << "enter a value in [1," << maxv << "]: " ;
    int value ;
    if( std::cin >> value )
    {
        if( value >= minv && value <= maxv ) return value ; // valid value, return it
        else std::cout << value << " is not a valid value\n" ;
    }

    else // you may want to skip this part (input error handling) for now
         // it takes care of the situation where the user did not enter a number
    {
        std::cout << "input failure. non-numeric input\n" ;
        std::cin.clear() ; // clear the failed state
        std::cin.ignore( 1000, '\n' ) ; // ignore this line of input
    }

    return get_int( minv, maxv ) ; // try again
}

// display a menu and return a valid choice entered by the user
int get_menu_choice( const std::string& title, const std::vector<std::string>& menu )
{
    if( !menu.empty() )
    {
        show_menu( title, menu ) ;

        // valid choices are from 1 up to the number of items in the menu
        return get_int( 1, menu.size() ) ;
    }
    else return 0 ;
}

double get_bill_amount() // get a positive value as the bill amount
{
    std::cout << "\nenter the bill amount: " ;

    double bill ;
    if( std::cin >> bill && bill > 0 ) return bill ; // valid input, return it

    // input error handling
    std::cout << "input failure. bad bill amount\n" ;
    std::cin.clear() ; // clear the possible failed state
    std::cin.ignore( 1000, '\n' ) ; // ignore this line of input
    return get_bill_amount() ;
}

void tip_calculator()
{
    std::cout << "\ntip calculator\n----------------\n" ;
    const std::vector<std::string> menu = { "8%", "10%", "15%", "20%", "back to menu" } ;
    const int choice = get_menu_choice( "Pick the tip percentage you would like to give", menu ) ;

    if( choice != menu.size() ) // if the choice is not the last item in the menu (back)
    {
        // look up the tip percentage in an array
        const double tip_value[] = { 0.00 /* unused place holder*/, 0.08, 0.10, 0.15, 0.20 }  ;
        const double tip = tip_value[choice] ; // choice is guaranteed to be a valid value

        const double bill = get_bill_amount() ;

        std::cout << std::fixed << std::setprecision(2)
                  << "bill amount:" << std::setw(10) << bill << '\n'
                  << "      % tip:" << std::setw(10) << tip*100 << '\n'
                  << " tip amount:" << std::setw(10) << bill*tip << '\n'
                  << "      total:" << std::setw(10) << bill + bill*tip << '\n' ;
    }
}

double average( const std::vector<int>& grades )
{
    long long total = 0 ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int g : grades ) total += g ;

    // defensive: avoid the division 0/0
    return grades.empty() ? 0 : double(total) / grades.size() ;
}

void grade_calculator()
{
    std::cout << "\ngrade calculator\n----------------\n" ;
    const std::vector<std::string> menu = { "calculate average grade", "back to menu" } ;
    const int choice = get_menu_choice( "pick one of these choices", menu ) ;

    if( choice == 1 )
    {
        std::cout << "number of classes? " ;
        const int num_classes = get_int( 1, 10 );

        // create a vector containing num_classes values
        std::vector<int> grades(num_classes) ;

        std::cout << "enter grades for " << num_classes << " classes\n" ;
        for( int& g : grades ) // for each value in the vector
        {
            std::cout << "? " ;
            g = get_int(0,100) ; // accept a non-negative grade, max 100
        }

        std::cout << "\naverage grade: " << std::fixed << std::setprecision(2)
                  << average(grades) << '\n' ;
    }
}

int main()
{
    std::vector<std::string> main_menu = { "Tip Calculator", "Grade Calculator", "Quit Program" } ;

    int choice = get_menu_choice( "\nwhat do you want to do?", main_menu );

    // keep going till the choice is the last item in the menu (quit program)
    while( choice != main_menu.size() )
    {
        if( choice == 1 ) tip_calculator() ;
        else grade_calculator() ;

        choice = get_menu_choice( "\nwhat do you want to do?", main_menu );
    }
}

Topic archived. No new replies allowed.