c++ array problem input and sub menus, assignment is due really soon

Pages: 123
I am a beginner of C++ and I still have no idea how to make the output like this especially question 5 to 7
https://docs.google.com/document/d/11Y5dVe_12rkJsdBK1yWpF0yaBBkPGGvF9gnhej5EtxM/edit?usp=sharing
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
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
using std::endl;

class Email
{
private:
    string en;
    string em;
    int pn;

public:
    void getDetails();
    void setDetails();

};

void Email::setDetails()
{
    cout << "Enter name : " << endl;
    cin >> en;
    cout << "Enter email address : " << endl;
    cin >> em;
    cout << "Enter phone number :" << endl;
    cin >> pn;
    
}

void Email::getDetails()
{
    cout << "Name: " << en  << "\n" << " Email: " << em << "\n" << " number " << pn << endl;
}


int main(int argc, char* argv [])
{
	int er = 1;
	int sbr = 1;
        int er2;
	while (er != 5)
	{
		cout << "*******MAIN MENU******\n";
		cout << "1. Initialize the address book\n";
		cout << "5. Quit\n";
		cout << "please enter 1, 2, 3, 4, 5";
		cin >> er;

	if (er == 1) //initialize the 4 emailname, address and phone number
	{
		        cout << "Name is : Larwarnce cheung \n";
                cout << "Email is : enccl@eie.polyu.edu.hk \n";
                cout << "telephone is : 27666131"; 
                cout << "Name is : Helen Wong \n";
                cout << "Email is : helenwong@yahoo.com \n";
                cout << "telephone is : 94665888";
                cout << "Name is : Simon Sui \n";
                cout << "Email is : ss123@gmail.com \n";
                cout << "telephone is : 64441234";
                cout << "telephone is : 64441234";
                cout << "Name is : Mary Ho \n";
                cout << "Email is : ho.mary1@@navigator.com \n";
                cout << "telephone is : 21111112";
			    cout << "initializting is completed" << endl;
	}
	else
	if (er >= 6) // if over 5 then it will have error output message
	{
		cout << "Error, try again" << endl;
	}
        else
        if (er == 2)
        {
            cout << "enter record of arrays";
            cin >> er2;
            if (er2 >= 10)
            {
                  cout << "Please enter a valid number." << endl;
            }
            else
            {
                Email emailArray[er2];
                for (int i = 0; i < er2; i++)
                {
                cout << "For records " << i + 1 << " :" << endl;
                emailArray[i].setDetails();
                }

        
                cout << "\nYou have entered : " << endl;
                for (int i = 0; i < er2; i++)
                {
                    emailArray[i].getDetails();
                }
            }
           
        }
	else
	if (er == 3) // sub menu
	{
        sbr =-1;
		while(sbr != 4)
		{
		  cout << "Sub menu\n";
		  cin >> sbr;
		  if (sbr > 4) // error message if enter above 4 on sub menu
		  {
			cout << "Error, Try again";
		  }
          else
          {
            cout << endl;
          }

		}
	}
	else
	{
		cout << "Goodbye!" << endl; //quit the main menu
	}
	}
	return 0;

}
Last edited on
I have done the class and question 1 to 4 which is easy for me, only struggling getting question 5 to 7 according to the pdf
well, the first lesson of computer science: that there link is on YOUR hard drive, and we can't see it.
I have the questions on my pdf
jonnin I have fixed my document about the question, you can read the question about the problems that I had mentioned
Last edited on
the output looks like just cout statements.
you need menus within a menu.

you may try a switch...

1
2
3
4
5
6
7
8
9
10
do
 main_menu();
 cin >> choice;
 switch choice
{
  case 1: initialize_menu(); break;
  case 2: create_records(); break;
  ... 
 default: cout << invalid choice message
}

and where necessary the sub menus work exactly like that.. create_records will have the exact same structure as the above --- call something to print the menu, then switch to handle the input and populate your data structure. you can have a for loop instead of while in this one because you know how many times it will execute.

but the idea is to break it down into simple blocks that call functions to handle the menus, so the menu code is small, easy to read, easy to modify, etc. If you try to do the work in the menus (eg in the switch cases) it can quickly bloat up and be hard to work with.
So the switch is used for a sub menu?
How do I quit the sub menu back to the main menu?
Also how do I put a new input and saved onto the array as a new saved record like question 5 has mentioned
Last edited on
yes, the main menu's switch activates the sub-menu.
the sub menu ends ... the create record one ends after a counter. the user says they want to put in 5, so you let them put in 5 and pop back out. other sub menus may need a choice to exit back to the main menu. since its driven by function calls, when the main menu calls the sub menu, the sub menu ends, you are back where you were in main, which should (if the switch is done right) just restart a loop of the main menu again from scratch.

usually prefer to write directly -- so each record, you write into array[index].something directly. No sense in loading up a middle man record and then copying all that to the real destination...
however with the data validation, that may still happen .. read into a string, validate it, if its good, write the data into the array (converting type as needed). This is sort of a design choice in how you store the data and how you validate it and so on.
Last edited on
so the break is used to exit the switch statement of the sub menu and return to main menu , am I correct?
Last edited on
no.
break is used on switch cases if you don't want to fall through to the next case(s)
this may end the switch, which may then be the end of the function. It is the function ending that returns back to where it was called from. But as I showed you, the switches are probably in some sort of a loop. If you need to stop that and get back to main, you need a way out. The most straightforward way to do that is just a 'return' statement.

Last edited on
Jonnin, I still have no idea how to do question 5, especially I have to create new records of input.
Last edited on
Also Jonnin, default on the switch is used if all cases did not met a requirement and can I use a while loop instead of switch for a main menu?
Last edited on
you were going to loop anyway (I said that up there somewhere).
the while does not replace the switch.
a bunch of if/else conditions can replace the switch, if you prefer that. Its fine to do that.

The best way to get help if you are stuck is to post some code, say what it is supposed to do, what is wrong with it, and see what we can tell you.
OK i may post my code later on
Seeplus and Jonnin, I have post my code on the above, already done the sub menu with while which I think it was easier but my while can only quit once on sub men to the main menu and I cant use the sub menu again from the main menu u when I typed 4 on the sub menu, meanwhile if i type below than 4 then it will quit to the main menu and it can repeat using the sub menu again from the main menu

Still struggling to do question 5 of creating new array spaces from the element with input
Last edited on
you do not create new array elements. arrays are fixed size and it says 10 max.
IF choice is 1 put in junk blah blah
ELSE IF CHOICE IS 2 don't fill in defaults, user provides.

add
subenter = -1 at 189 before while.
Ok so choice 2 isnt create new arrays, then how to create the new records for that specific array?
the records already exist. you are just filling them in.
int x[10]; //10 integers exist, with nonsense data in them
x[3] = 42; //put something into it.

If the choice is 2 ... After that, a sub-menu should be displayed for the end user to input ....
Last edited on
Ok i have to create int address[10] then fill in the new fresh records, then I have to type x[5] = 40 on If else 2?
Last edited on
@seeplus, Jonnin, I kinda figured about how to add 10 records but still dont know how to add the sub menu to allow to input to store these new arrays, do I have to use cin.getline() to input the email address, name and phone number into these new array records from question 5?

Not to mention I have to create another submenu to make a search for the new records for option 3 that I had created from the input according to the assignment questions 6 of the google docs, can you help? Assignment is due soon

1
2
3
4
5
6
7
8
std::cout << address[4];
std::cout << address[5];
std::cout << address[6];
std::cout << address[7];
std::cout << address[8];
std::cout << address[9];
std::cout << address[10];
std::cout << address[11];


Last edited on
Pages: 123