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

Pages: 123
you need to read them in for sure. whether its getline or >> is up to you or whatever the assignment says. Not sure its a 'real' menu.. if they pick add records, from there it seems like it would be 'how many' read number -> for 0-number {gimme a name, read a name, gimme an email, read an email, gimme a whatever, read a whatever } type logic.

the submenus should be repeats of what you did before.

@Jonnin, so i have to do like this enter the input towards the arrays, am I correct or there is another method instead?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cin >> enn //input of enter new records for name;

for (int i = 0; i < enn; i++)
{
}
cin >> ene //input of enter new records for email;

for (int i = 0; i < ene; i++)
{
}

cin >> enp //input of enter new records for phone number;

for (int i = 0; i < enp; i++)
{
}
Last edited on
don't edit the OP, now the replies make no sense.

> I have done the class and question 1 to 4 which is easy for me
«Design and implement a simple Record class»
you wrote class Email, a useless 120 lines behemoth, which, based on your usage, seems to store a collection of four contacts
so no, you did not do questions 1 to 4 correctly
@ne555
> so no, you did not do questions 1 to 4 correctly
What is the correct one for question 1 to 4 ? I cant figure it how honestly and still struggling since you said my class was useless. Just give me a correct example so I can make it right for making my record class if you think you are correct

Not to mention that I have to keep those 4 records for question 1 to 4 and the due date was very soon
Last edited on
@so21623. You pm'd me for help. Note that I never provide any help via pm/email etc - only via the forum. In the first case, looking at section A of the assignment, I see nothing in the provided code re the required Record class. It's no use asking for help re Section B until you have correct code for Section A.

So unless you have correct code for Section A which you haven't provided, I suggest you start again from the beginning and produce what is asked - a Record class. (NB I wouldn't get too bothered about Section A 5 re the lib. You can complete the code for Section A and B without creating the .lib)

Once you haver that - and tested to make sure it's working - than you can move on to Section B. Don't try to run before you can crawl.

@seeplus I have already done reworking the new code, still dont know how to do question 6 to 7 so far... Not to mention I cant make an error message according to question 5 for the question
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
@OP Without comment.

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>
#include <string>

class Record
{
private:
    std::string name{"???"};
    std::string email_address{"?&??.???"};
    int phone_no;

public:
    Record(){};
    
    void setName(std::string aName)
    { name = aName;}

    std::string getName()
    { return name; }
    

    void setEmailAddress(std::string aEmail_address)
    {email_address = aEmail_address;}

    std::string getEmailAddress()
    { return email_address; }
    

    void setPhoneNo(int aphone_no)
    {phone_no = aphone_no;}

    int getPhoneNo()
    { return phone_no; }
};

int main()
{
    Record starter;
    
    std::string name;
    std::cout << "Enter name: ";
    getline(std::cin, name);
    
    std::string email_addr;
    std::cout << "Enter email address: ";
    std::cin >> email_addr;
    
    int p_no;
    std::cout << "Enter phone number: ";
    std::cin >> p_no;
    
    starter.setName(name);
    starter.setEmailAddress(email_addr);
    starter.setPhoneNo(p_no);
    
    std::cout
    << "=============== RECORD ===============\n"
    << starter.getName() << '-' << starter.getPhoneNo() << '\n'
    << starter.getEmailAddress()<< '\n'
    << "=============== RECORD ===============\n";
    
    return 0;
}


Enter name: Bob Smith
Enter email address: bob@smith.com
Enter phone number: 1029383847
=============== RECORD ===============
Bob Smith-1029383847
bob@smith.com
=============== RECORD ===============
Program ended with exit code: 0
@s021623. You still haven't provided the code for the required Record class. You seem to think you need an Email class - which is not what is asked for.

For a starter, he's a possible Record class implementation with some error checking:

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
#include <iostream>
#include <string>

class Record {
private:
	std::string name;
	std::string email_address;
	unsigned phone_no {};

public:
	Record() {};

	void setName(const std::string& aName) { name = aName; }
	std::string getName() const { return name; }

	bool setEmailAddress(const std::string& aEmail_address) {
		if (aEmail_address.find('@') != std::string::npos) {
			email_address = aEmail_address;
			return true;
		}

		return false;
	}

	std::string getEmailAddress() const { return email_address; }

	bool setPhoneNo(unsigned aphone_no) {
		if (aphone_no >= 10'000'000 && aphone_no <= 99'999'999) {
			phone_no = aphone_no;
			return true;
		}

		return false;
	}

	unsigned getPhoneNo() const { return phone_no; }
};

int main()
{
	Record starter;
	std::string name;

	std::cout << "Enter name: ";
	getline(std::cin, name);
	starter.setName(name);

	std::string email_addr;

	do {
		std::cout << "Enter email address: ";
		std::cin >> email_addr;
	} while (!starter.setEmailAddress(email_addr) && (std::cout << "Invalid email address\n"));

	unsigned p_no {};

	do {
		std::cout << "Enter phone number: ";
		std::cin >> p_no;
	} while (!starter.setPhoneNo(p_no) && (std::cout << "Invalid phone number\n"));

	std::cout
		<< starter.getName() << " - " << starter.getPhoneNo() << '\n'
		<< starter.getEmailAddress() << '\n';
}




As a starter for part B, perhaps consider:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <type_traits>

template<typename T = int>
typename std::enable_if_t< std::is_integral_v<T>, T>
getInp(const std::string& prm)
{
	const auto notsp{ [&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; } };
	T n{};

	while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
		std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return n;
}

class Record {
private:
	std::string name;
	std::string email_address;
	unsigned phone_no {};

public:
	Record() {};
	Record(const std::string& nam, const std::string& email, unsigned phone) : name(nam) {
		setEmailAddress(email);
		setPhoneNo(phone);
	}

	void setName(const std::string& aName) { name = aName; }
	std::string getName() const { return name; }

	bool setEmailAddress(const std::string& aEmail_address) {
		if (aEmail_address.find('@') != std::string::npos) {
			email_address = aEmail_address;
			return true;
		}

		return false;
	}

	std::string getEmailAddress() const { return email_address; }

	bool setPhoneNo(unsigned aphone_no) {
		if (aphone_no >= 10'000'000 && aphone_no <= 99'999'999) {
			phone_no = aphone_no;
			return true;
		}

		return false;
	}

	unsigned getPhoneNo() const { return phone_no; }
};

void create(Record& rec) {
	std::string name;

	std::cout << "Enter name: ";
	getline(std::cin, name);
	rec.setName(name);

	std::string email_addr;

	do {
		std::cout << "Enter email address: ";
		std::cin >> email_addr;
	} while (!rec.setEmailAddress(email_addr) && (std::cout << "Invalid email address\n"));

	while (!rec.setPhoneNo(getInp<unsigned>("Enter phone number: ")) && (std::cout << "Invalid phone number\n"));
}

int main()
{
	constexpr size_t MaxAddr {100};
	bool quit{ };

	Record AddressBook[MaxAddr]{ {"Larwarnce cheung", "enccl@eie.polyu.edu.hk", 27666131},
		{"Helen Wong", "helenwong@yahoo.com", 94665888},
		{"Simon Su", "ss123@gmail.com", 64441234},
		{"Mary Ho", "ho.mary1@@navigator.com", 21111112}};

	size_t noRecs {4};

	while (!quit) {
		std::cout << "\n1. Initialise the address book\n"
			<< "2. Create person contact information\n"
			<< "3. Lookup person contact information\n"
			<< "4. Lookup all person contact information\n"
			<< "5. Quit\n";

		switch (getInp<unsigned>("\nEnter option: ")) {
			case 1:
				noRecs = 4;
				break;

			case 2:
				if (noRecs < MaxAddr)
					create(AddressBook[noRecs++]);
				else
					std::cout << "Maximum number of records reached\n";
				break;

			case 3:
				std::cout << "Not yet implemented\n";
				break;

			case 4:
				std::cout << '\n';

				for (size_t i = 0; i < noRecs; ++i)
					std::cout << std::left << std::setw(25) << AddressBook[i].getName() <<
					std::left << std::setw(25) << AddressBook[i].getPhoneNo() << AddressBook[i].getEmailAddress() << '\n';

				break;

			case 5:
				quit = true;
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	}
}

Last edited on
@seeplus, so you use the switch instead?
Also can I do while loop instead of a switch for part B?
Last edited on
My starter for part B does use a while loop L92.

@seeplus Ok sorry but what I mean is using if and else for the while loop instead of a switch like I did for section B
@kbw So far I have done the new and simple class, but I still dont know how to do the question 5 to 7 somehow...

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
#include <iostream>   
#include <string>   
using std::cin;
using std::cout;
using std::string;
using std::endl;


class Record 
{
private:
    std::string en;
    std::string em;
    int pn;

public:
    std::string getName() const { return en; }
    std::string getEmailAddress() const { return em; }
    int getPhoneNo() const{ return pn; }
    void setName(std::string value) { en = value; }
    void setEmailAddress(std::string value) { em = value; }
    void setPhoneNo(int value) { pn = value; }
};



int main(int argc, char* argv[])

{
    int e = 1;
    int sbr = 1;
    int e2 = 1;

    while (e != 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 >> e;
        if (e == 1)
        {
            cout << "Name is : Larwarnce cheung \n";
            cout << "Email is : enccl@eie.polyu.edu.hk \n";
            cout << "telephone is : 27666131 \n";
            cout << "Name is : Helen Wong \n";
            cout << "Email is : helenwong@yahoo.com \n";
            cout << "telephone is : 94665888\n";
            cout << "Name is : Simon Sui \n";
            cout << "Email is : ss123@gmail.com \n";
            cout << "telephone is : 64441234\n";
            cout << "Name is : Mary Ho \n";
            cout << "Email is : ho.mary1@@navigator.com \n";
            cout << "telephone is : 21111112\n";
            cout << "Initializting is completed\n" << endl;
        }
        else
            if (e >= 6)
            {
                cout << "Error, try again" << endl;
            }
            else
                if (e == 2)
                {
                    cout << "Enter number of record arrays";
                    cin >> e2;
                    if (e2 > 6)
                    {
                        cout << "The array has reached the maximum value" << endl;
                    }
                    else
                    {
                        Record recordArray[6];
                        for (int i = 0; i < e2; i++)
                        {
                            cout << "For records " << i + 1 << " :" << endl;
                            recordArray[i].setName();
                        }
                        cout << "\nYou have entered : " << endl;
                        for (int i = 0; i < e2; i++)
                        {
                            recordArray[i].getName();
                        }
                    }
                }
                else
                    if (e == 3)
                    {
                        sbr = -1;
                        while (sbr != 4)
                        {
                            cout << "******SUB MENU******\n";
                            cout << "4. exit the submenu\n";
                            cin >> sbr;
                            if (sbr > 4)
                            {
                                cout << "Error, Try again";
                            }
                            else
                            {
                                cout << endl;
                            }
                        }
                    }
                    else
                    {
                        cout << "Goodbye!" << endl;
                    }
    }
    return 0;
}
You don't need this stuff.
1
2
3
4
using std::cin;
using std::cout;
using std::string;
using std::endl;
You can replace it with:
 
using namespace std;

You need to use meaningful names here:
1
2
3
4
5
6
class Record 
{
private:
    std::string en;
    std::string em;
    int pn;
Really, what does en, em, pn mean? What does is cost to use name, email, phoneno?

Your code should look like:
1
2
3
4
5
6
7
8
9
10
loop:
    show menu
    selector = user selection
    switch: selector
        case 1:  add sample records
        case 2:  add user records
        case 3:  search records
        case 4:  display records
        case 5:  quit
        default:  bad input

Use functions to implement the features to keep the main program simple. It's easier to read and spot errors in simple code.

You mentioned you were struggling with step 5 and beyond.
5. If the choice is 2, the program should display a message “Please enter the total number of records to be created”, and then store the input integer. You may set the maximum number of new records is 10.
What part of this do you find hard?
Last edited on
@kbw
The hardest part is to enter the maximum amount of records which means after 10 records are inputed then there should be no more records added and have a warning output of maximum records have reached.

Also I have to make an input to insert each record of the name, phone number and email address . The email address should contain a @ or else it will output an error message without the @ on the input of the new records. The phone number should contain eight integers or else it will output an error message if there arent eight integers or contain letters on the input towards the phone number input record.

These questions requires a teststring.at(i) and cin.getline to give restrictions to the input and I don't know where to put the for and teststring.at(i) for question 5
Last edited on
The hardest part is to enter the maximum amount of records which means after 10 records are inputed then there should be no more records added and have a warning output of maximum records have reached.
You ask for the input, check if it's in the range [1..10], report an error if it's outside that range, and try again. Something like:
1
2
3
4
5
6
7
8
9
10
do
    selection = get user input
    if (selection out of range)
        print error message
while (selection out of range)

for (i = 0; i < selection; ++i)
    prompt for record details
    enter record
    add record to list of records

Of course, that's pseudo code, you'll need to code that as C++.
@kbw
Ok this is also the hardest part as well
>Also I have to make an input to insert each record of the name, phone number and email address . The email address should contain a @ or else it will output an error message without the @ on the input of the new records. The phone number should contain eight integers or else it will output an error message if there arent eight integers or contain letters on the input towards the phone number input record.

These questions requires a teststring.at(i) and cin.getline to give restrictions to the input and I don't know where to put the for and teststring.at(i) for question 5
Last edited on
You need to keep your Records somewhere, right? You don't appear to be storing them anywhere.

The simplest place to keep them is in an array. So you'd have something like:
1
2
3
constexpr int MaxRecords = 20;
Record records[MaxRecords];
int lastRecord = 0;


Then you can add a helper function like:
1
2
3
4
5
6
void addRecord(Record rec) {
    if (lastRecord < MaxRecords) {
        records[lastRecord] = rec;
        ++lastRecord;
    }
}

And with that, you can write a function that reads a record:
1
2
3
4
5
6
7
8
9
10
Record readNewRecord() {
    Record record;
    // prompt the user for name
    std::cin >> record.name;
    // prompt the user for email
    std::cin >> record.email;
    // prompt the user for phoneno
    std::cin >> record.phoneno;
    return record;
}


And with that you can do the Read Record part with:
1
2
3
4
for (int i = 0; i < selection; ++i) {
    Record record = readNewRecord();
    addRecord(record);
}
Last edited on
@kbw
Ok the final part I need to do for question 5 was the email address should contain a @ or else it will output an error message without the @ on the input of the new records. The phone number should contain eight integers or else it will output an error message if there arent eight integers or contain letters on the input towards the phone number input record, a getline and string.at is required。
The hardest part is to enter the maximum amount of records which means after 10 records are inputed then there should be no more records added and have a warning output of maximum records have reached.


Look at my case 2. This just allows 1 record to be entered. But it checks for max no of records. You just need to ask for the number of records to add, make sure when added to the current number of records it doesn't exceed maximum required and then use create() in a loop. Easy peasy...

Record setters also need further tests to check for valid input as per the requirement.
Last edited on
Pages: 123