require help in solving a problem

hello
i am almost new to c++ programming and i am trapped with the solution of this riddle
question. Write a menu based system in C++ that will take input from user for the following menu. (use array)
Press 1 To Enter Values In Array.
Press 2 To Read Array Elements.
Press 3 To Show Array Elements In Reverse Order.
Press 4 To Show Array Elements Which Are Multiples Of 3.
Press 5 To Show Sum Of All The Elements.
Press 6 To Exit.

Use switch statement for the menu based system.
Use pointers for option 2, 3, 4, 5.
the code that i wrote for the program is something 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
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
#include<iostream>
using namespace std;
void showchoice();
main()
{
	int no,x,sum;
	char choice;
	cout<<"Please enter the total number of values you want to enter    ";
	cin>>no;
	int menu [no];
		do
	{
		showchoice();	
		cin>>choice;
		switch (choice)
		{	
			case '1':
				for(x=0;x<no;x++)
				{
					cout<<"Enter data in array";
						cin>>menu [x];
					}
					cout<<no<<" elements entered in array"<<"\n";
				break;
				case '2':
					for (x=0;x<no;x++)
					{	
						cout<<"\n";	
						cout<<menu [x]<<"\n";
					}
				break;
				case '3':
					for (x=no-1;x>=0;x--)
					{	
						cout<<"\n";	
						cout<<menu [x]<<"\n";
					}
				break;
				case '4':
					for (x=0;x<no;x++)
					{
						if (menu[x]%3==0)
							cout<<"Multiples of three are "<<menu[x]<<"\n";
					}
				break;					
				case '5':
					for (x=0;x<no;x++)
					{
						sum = sum + menu[x];
					}
					cout<<"Sum of all array elements is "<<sum<<"\n";
				break;
				case '6':
				break;	
				default:
					cout<<"Please enter value from 1 to 6";
				break;
		}
	}while (choice!='6');
}
void showchoice()
{
	cout<<"Press 1 to enter data in array:"<<"\n";
	cout<<"Press 2 to read array elements:"<<"\n";
	cout<<"Press 3 to show array elements in reverse order:"<<"\n";
	cout<<"Press 4 to show array elements which are multiple of 3:"<<"\n";
	cout<<"Press 5 to show sum of all elements of array:"<<"\n";
	cout<<"Press 6 to exit:"<<"\n";
	cout<<"Enter your choice:    ";
}
Last edited on
and this is working fine but the part where i have to ask the user for reinput if data has not been entered in the first time, i cant figure that one out
Welcome. Please use code tags. The following link shows you how: http://www.cplusplus.com/articles/jEywvCM9/

pioneer06a wrote:
this is working fine


It does not even compile. For this portion of the code,

main() should be int main()
1
2
3
	cout << "Please enter the total number of values you want to enter ";
	cin >> no;
	int menu[no];


Are you supposed to use dynamically allocate arrays?

pioneer06a wrote:
Use pointers for option 2, 3, 4, 5.


I do not see any use of the pointers. Maybe, take a look at this link:

https://www.programiz.com/cpp-programming/pointers-arrays

hey pioneer!
we see this a lot. To understand the above post, you need to understand that compilers have a 'relaxed syntax' setting and many default to this (g++'s default is so relaxed it could be another language!). So many compiler will accept a variable length array (int x[variable] but the c++ strict language (which we prefer to use here) will not compile that, it requires int x[constant] instead. Also void main, main, etc compile in relaxed but the c++ standard is int main. You should turn on your compiler settings to error out on illegal code so you learn it correctly and avoid bad habits early.

to force the user to input data, you can use a Boolean or integer (0 or 1) if you cannot use bool yet (some people can't use stuff they have not yet covered in class)
bool hasentered = false;
do
get data into array code
if(entered ok) hasentered = true; //once you are happy with their efforts
while(hasentered == false)
Last edited on
thank you for your cooperation. i am using Bloodshed Software Dev C++. in my compiler the program is running fine. But i can't understand the dynamic array allocation and what is its need here.
Moreover, i forgot to mention the last requirement which is as under
"If the user chooses any option other than 1, for the first time, when array is empty, so the user will get a message “Array is empty, first enter data in array""
i have been able to complete the program with following code
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
#include<iostream>
using namespace std;
void showchoice();
int main()
{
	int no,x,sum;
	char choice,ans;
	cout<<"\n"<<"Please enter the total number of values you want to enter    ";
	cin>>no;
	do
	{
		showchoice();
		cin>>choice;
		if (choice=='1')
		{
			break;
		}
		else
		{
			cout<<"\n"<<"Array is empty. First enter data in array"<<"\n";
		}
	}while (choice!='1');
	int menu [no];
	do
	{
		switch (choice)
		{	
			case '1':
				cout<<"\n";
				for(x=0;x<no;x++)
				{
					cout<<"Enter data in array    ";
					cin>>menu [x];
				}
					cout<<"\n"<<no<<" elements entered in array"<<"\n";
				break;
			case '2':
				cout<<"\n"<<"Read the array elements:"<<"\n";
				for (x=0;x<no;x++)
				{	
					cout<<"\n";	
					cout<<menu [x]<<"\n";
				}
				break;
			case '3':
				cout<<"\n"<<"Display the array elements in reverse order:"<<"\n";
				for (x=no-1;x>=0;x--)
				{	
					cout<<"\n";	
					cout<<menu [x]<<"\n";
				}
				break;
			case '4':
				cout<<"\n"<<"Display the multiples of 3:"<<"\n";
				cout<<"Multiples of three are ";
				for (x=0;x<no;x++)
				{
					if (menu[x]%3==0)
					cout<<"\n"<<menu[x];
				}
				break;					
			case '5':
				cout<<"\n"<<"Display sum of array elements:"<<"\n";
				for (x=0;x<no;x++)
				{
					sum = sum + menu[x];
				}
				cout<<"\n"<<"Sum of array elements is "<<sum<<"\n";
				break;
			case '6':
				break;
			default:
				cout<<"\n"<<"Please enter value from 1 to 6"<<"\n";
				break;
		}
		cout <<"\n"<<"Do you want to continue (Y/N)?\n";
    	cin >> ans;
    	if ((ans == 'Y') || (ans == 'y'))
		{
			showchoice();
			cin>>choice;
		}
		else
			break;
	}while (choice!='6');
}
void showchoice()
{
	cout<<"\n";
	cout<<"Press 1 to enter data in array:"<<"\n";
	cout<<"Press 2 to read array elements:"<<"\n";
	cout<<"Press 3 to show array elements in reverse order:"<<"\n";
	cout<<"Press 4 to show array elements which are multiple of 3:"<<"\n";
	cout<<"Press 5 to show sum of all elements of array:"<<"\n";
	cout<<"Press 6 to exit:"<<"\n";
	cout<<"\n"<<"Enter your choice:    ";
}


now ill try to use the pointer and functions to perform the tasks at option 2, 3, 4, 5.
can any one help me with that?
pioneer06a wrote:
can any one help me with that?


Take a look at the Example 3 on this link:

https://www.programiz.com/cpp-programming/pointers-arrays

If you have learned in school about dynamically allocated arrays, then I would use it to obtain the values from the user. The link below seem like a good place to read about dynamically allocated arrays (if you have learned about it at your school).

https://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/


To give you an idea...

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <cctype> // To be used with the toupper function.

// Function prototypes.
void displayMenu(int&);
void askNumberOfValues(int&);
void enterValuesInArray(int*, const unsigned int);
void readValuesInArray(const int*, const unsigned int);
void showArrayReverseOrder(const int*, const unsigned int);
void showMultiplesThrees(const int*, const unsigned int);
void sumAllTheElements(const int*, const unsigned int);

int main()
{
	int sizeOfArray{};
	int* arrayOfInt{ nullptr };
	int choice{};
	int sum{};
	char deleteArrayChoice{};
	bool keepLooping{ true };

	// Keep looping until the user hits option 6 to close the program.
	while (keepLooping)
	{
		displayMenu(choice);

		switch (choice)
		{
			case 1:
				if (sizeOfArray == 0)
				{
					askNumberOfValues(sizeOfArray);
					arrayOfInt = new int[sizeOfArray];
				}
				else
				{
					std::cout << "This option will override the current array.\n";
					std::cout << "Do you wish to continue (Y/N)?: ";
					std::cin >> deleteArrayChoice;

					if (std::toupper(deleteArrayChoice) == 'Y')
					{
						delete[] arrayOfInt;
						arrayOfInt = nullptr;
						sizeOfArray = 0;

						askNumberOfValues(sizeOfArray);
						arrayOfInt = new int[sizeOfArray];
					}
					else
					{
						break;
					}
					
				}

				enterValuesInArray(arrayOfInt, sizeOfArray);
				keepLooping = true;
				break;

			case 2:
				readValuesInArray(arrayOfInt, sizeOfArray);
				keepLooping = true;
				break;

			case 3:
				showArrayReverseOrder(arrayOfInt, sizeOfArray);
				keepLooping = true;
				break;

			case 4:
				showMultiplesThrees(arrayOfInt, sizeOfArray);
				keepLooping = true;
				break;

			case 5:
				sumAllTheElements(arrayOfInt, sizeOfArray);
				keepLooping = true;
				break;

			case 6:
				std::cout << "Closing the program...\n";
				keepLooping = false;
				break;

			default:
				std::cout << "Please choose between 1 and 6.\n";
				keepLooping = true;
		}
	}

	delete[] arrayOfInt;
	arrayOfInt = nullptr;

	return 0;
}

void displayMenu(int& choice)
{
	std::cout << "\n            Menu Based Array\n";
	std::cout << "-----------------------------------------\n";
	std::cout << "1) Enter Values in Array\n";
	std::cout << "2) Read Array Elements\n";
	std::cout << "3) Display Array in Reverse Order\n";
	std::cout << "4) Display Array Elements in Multiple of 3s.\n";
	std::cout << "5) Display the Sum of the Elements in Array.\n";
	std::cout << "6) Exit the program\n\n";
	std::cout << "Enter your choice: ";
	std::cin >> choice;
}

void askNumberOfValues(int& numOfValues)
{
	std::cout << "Enter the number of values in the array: ";
	std::cin >> numOfValues;
}

void enterValuesInArray(int* arrayOfInt, const unsigned int sizeOfArray)
{
	for (unsigned int count = 0; count < sizeOfArray; count++)
	{
		std::cout << "Element number " << count + 1 << " : ";
		std::cin >> *(arrayOfInt + count);
	}
}

void readValuesInArray(const int* arrayOfInt, const unsigned int sizeOfArray)
{
	if (sizeOfArray == 0)
	{
		std::cout << "Array is empty, first enter data in array.\n";
	}
	else
	{
		std::cout << "The array elements are...\n";
		for (unsigned int count = 0; count < sizeOfArray; count++)
		{
			std::cout << "Element number " << count + 1 << " : "
				<< *(arrayOfInt + count) << std::endl;
		}
	}

}

void showArrayReverseOrder(const int* arrayOfInt, const unsigned int sizeOfArray)
{
	if (sizeOfArray == 0)
	{
		std::cout << "Array is empty, first enter data in array.\n";
	}
	else
	{
		std::cout << "The reversed array elements are...\n";
		for (int count = sizeOfArray - 1; count > -1; count--)
		{
			std::cout << "Element number " << count + 1 << " : "
				<< *(arrayOfInt + count) << std::endl;
		}
	}
}

void showMultiplesThrees(const int* arrayOfInt, const unsigned int sizeOfArray)
{
	int countMulOf3s{ 0 };

	if (sizeOfArray == 0)
	{
		std::cout << "Array is empty, first enter data in array.\n";
	}
	else
	{
		std::cout << "Displaying elements with multiple of threes...\n";
		for (unsigned int count = 0; count < sizeOfArray; count++)
		{
			if (*(arrayOfInt + count) % 3 == 0)
			{
				std::cout << "Element number " << count + 1 << " : "
					<< *(arrayOfInt + count) << std::endl;
				countMulOf3s++;
			}
		}

		if (countMulOf3s == 0)
		{
			std::cout << "No elements with multiple of threes in the array\n";
		}
		
	}
}

void sumAllTheElements(const int* arrayOfInt, const unsigned int sizeOfArray)
{
	int sum{};

	if (sizeOfArray == 0)
	{
		std::cout << "Array is empty, first enter data in array\n";
		return;
	}
	else
	{
		for (unsigned int count = 0; count < sizeOfArray; count++)
		{
			sum += *(arrayOfInt + count);
		}
	}

	std::cout << "The sum of all elements in the array is: " << sum << std::endl;

}
chicofeo wrote:
To give you an idea...
thank you very much for your support
Topic archived. No new replies allowed.