Vector manipulation bsar

I have been able to figure out how to manipulate and use information in vectors, but would now like to use the following code to make a question menu based on the two questions I ask. How would you make a switch case menu?
Thanks!

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
#include <iostream>
using namespace std;
#include <vector>
#include <string>

int main() {
   
   vector<string> names;
   string again = "";
   int length = 0;
   
   cout << "---------\tHello!\t----------" << endl;
   do {
      
      cout << "Please enter a name: " << endl;
      string name;
      cin >> name;
      length++;
   
      names.push_back(name);
   
      for (int i = 0; i < length; i++) {
         cout << names[i] << endl;
      }
      
      cout << "Would you like to erase one? (Y/N)" << endl;
      string deleting = "";
      cin >> deleting;
      
      deleting = toupper(deleting.at(0));
            
      if(!deleting.compare("Y")) {
         cout << "What name would you like to erase?" << endl;
         string which;
         cin >> which;
         
         for (int j = 0; j < names.size(); j++) {
            if (which == names[j]) {
               names.erase(names.begin()+j);
               j = names.size();
            }
            
         }
         
      }
      
      cout << "Would you like to enter another?:" << endl;
      cin >> again;
   
      again = toupper(again.at(0));
   
   } while(!again.compare("Y"));
   
   cout << "Here is your list of names:\n" << endl;
   for (int i = 0; i < names.size(); i++) {
      cout << "\t" << names[i] << endl;
   }
   
   
   
   return 0;
}
Last edited on
Do you know about functions? Do you know about passing parameters by reference?
Otherwise I’m afraid your switch-based menu would probably became a mess.

Anyway a basic idea could be like this:
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
#include <iostream>
#include <string>


int main()
{
    std::string again;
    do {
        int option;
        do {
            std::cout << "What do you want to do?\n1) Add a name\n"
                         "2) Delete a name\n\n"
                         ">>> ";
            std::cin >> option;
            switch (option) {
            case 1:
                // your code here (it's better to call a function)
                break;
            case 2:
                // your code here (it's better to call a function)
                break;
            default:
                std::cout << "Can't understand your choice. Please try again.\n";
                continue;
            }
        } while (option < 1 || 2 < option);

        std::cout << "Would you like to continue (Y/N)? ";
        std::cin >> again;
        again = toupper(again.at(0));
    } while(!again.compare("Y"));
    return 0;
}

Last edited on
Topic archived. No new replies allowed.