Help with Arrays, Menu, and Sorting Functions

So, I have done projects with all of the following, and now it is my time to compile them all together... And I can't seem to do it... All I'm really looking for is a skeleton because I can't figure it out... I have no background knowledge in C++ and am a shrimp among sharks in my class...

I am looking to first have the user input 5 numbers by creating an array as a global class as the first function. This will then lead to me move forward to the second function, opening up a menu by asking the user to view these numbers in either Ascending or Descending orders. Each of these options will then create a third and fourth function, respectively.

I am also looking to take this one step at a time as I work along with new revelations...


My header file looks 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
#include <string>
using namespace std;


class numbers
{
private:
	int numb[5];
public:
	int menu();
	int Ascending(int[], int);
	int Descending(int[], int);

	void setMenu(int);
	void setAscending(int);
	void setDescending(int);
	

	int getMenu() const
	{
		return Menu;
	}
	int getAscending() const
	{
		return Ascending;
	}
	int getDescending() const
	{
		return Descending;
	}
};



Edit 1: Edited for variable definitions.
Last edited on
What are num1, num2 and num3? I don't see those defined anywhere.


Oops, you're right. Okay, Will fixed that right away.

I'm also going to post my .cpp file just to show you the direction I am taking...

You will have to excuse me, because the directions are all over the place...

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
#include <iostream>
using namespace std;
#include "header.h"
#include <cstdlib>
#include <cstring>


int main()
{

	while (1)
	{
		char choice[2];
		int num1 = 1;
		int num2 = 2;
		int up;

		cout << "::Welcome to my sorting program\n\n"
			<< "Please choose an option to view my work\n\n"
			<< "1. Ascending Order \n"
			<< "2. Descending Order \n"
			<< "3. Ascending and Descending Order \n"
			<< "4. Exit \n\n"
			<< "Choice: ";
		cin >> choice;
		if (choice[0] == '1')
		{
			{




				{ {
					
					return 0;
				}
				}
			}
			system("CLS");
			cout << "Ascending\n\n";
			system("PAUSE");
			system("CLS");
		}
		else if (choice[0] == '2')
		{
			{
				{
					
					return 0;
				}
			}
			system("CLS");
			cout << "Descending Order\n\n";
			system("PAUSE");
			system("CLS");
		}
		else if (choice[0] == '3')
		{
			{
				
				}

				return 0;
				}
			}
			system("CLS");
			cout << "Ascending and Descending Order\n\n";
			system("PAUSE");
			system("CLS");
		}

		else if (choice[0] == '4')
		{
			{
				return 0;
			}
		else if (choice[0] > '4' || choice[0] < '1')
		{
			system("CLS");
			cout << "Invalid Input\n\n";
			system("PAUSE");
			system("CLS");
		}
		else
		{
			system("CLS");
			cout << "Option 1\n\n";
			system("PAUSE");
			system("CLS");
		}

			}
			system("CLS");
			cout << "Ascending Order\n\n";
			system("PAUSE");
			system("CLS");
		}
	}



















	//int numbers[10];
	for (int i = 0 , j = 0; i < 10; i++)
	{
		cout << " Please enter number " << i << endl;
		cin >> numbers[i];

	}

	i = 0;
	int greater = 0;
	for (; i<10; i++)
	{
		if (numbers[i] >= 10)
			greater++;

		cout << "number of elements greater or equal to 10 is: " << greater << endl;//stuff I found on yahoo answers   

		return 0;
}






Some stuff I found on yahoo answers to maybe solve my arrays issue... I looked and saw that this may be more difficult than it needs to be and saw that I can instead use a Bubble Sort.

Next issue is not being able to file these functions and be able to have function 1 (The Array) call Function 2 (The Menu) which then can call Function 3, 4, 5 (Ascending, Descending, and Ascending and Descending options)
Last edited on
Still need help... This newbie has been working on it for 10 days now...
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
#include<iostream>
#include<string>

using namespace std;
int main(char a, char b, char c)
{

	int array[5];                                      
	cout << "Enter 5 numbers randomly : " << endl;          //Ask User To Enter 5 numbers
	for (int i = 0; i<5; i++)
	{
		cin >> array[i];                                        // Input for entering 5 numbers
	}
	cout << endl;


	cout << "a) Increasing Order !!.\n"                        // Displaying  Menu for user to choose between 3 choices
		"b) Decreasing Order  !!.\n"
		"c) Both Orders  !!.\n" << endl;

	char choice;
	cout << "enter a, b, or c: " << endl;               // Asking user to choose one of the choices
	cin >> choice;                                     //  input for user to choose 
	switch (choice)
	{







		if (choice = a)
		{                                                       //if user enters choice a
	case'a':	for (int L = 0; L <= 4; L++)
	{
		for (int j = 0; j < 4; j++)
		{
			if (array[j] > array[j + 1])
			{
				int	temp1 = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp1;
			}
		}
	}
				cout << "*******Increasing   is: " << endl;
				for (int m = 0; m < 5; m++)
				{
					cout << array[m] << endl;                          // Displaying elements by increasing order
				}
				break;                                                 //Program stops
		}








		if (choice = b)                                           // if user enters choice b
		{
	case'b':for (int k = 0; k <= 4; k++)
	{
		for (int j = 0; j < 4; j++)
		{
			if (array[j] < array[j + 1])
			{
				int	temp2 = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp2;
			}
		}
	}
			cout << "*******Decreasing  is: " << endl;
			for (int m = 0; m < 5; m++)
			{
				cout << array[m] << endl;                              // displaying  elements by decreasing order
			}
			break;                                                     //Program stops
		}













		if (choice = c)                                               // if user chooces c


	case 'c':
		{
			break;                                                   //Program stops
			}

	default:                                                         //if user does not choose one of the above choices
	{cout << "You Entered Wrong Choice !!!!!" << endl;                 //display  message user entered wrong
	break;

		}

	}
	return 0;
}


Okay, this is what I worked on so far....

I'm not really able to have it work because it is still missing a header file.

Much help, still appreciated...
Firstly, I'd strongly recommend you adopt a sensible, consistent indentation style. It would make it much easier for both you and us to actually read your code, and see the flow of control and logic.

Why is your main function defined as taking 3 char arguments? That's not legal. What are you trying to achieve there?

You don't seem to know the difference between the assignment operator = and the equality comparison operator ==. I suggest you learn that, and fix your code so that your comparisons do what you want them to do.

Your switch-case statement seems to be trying to use an odd mix of cases and if statements that don't make a lot of sense. I suspect you need to go back and re-read the section on switch-case, and learn how you really write them.
I agree, OMG formatting:
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
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int array[5];
	cout << "Enter 5 numbers randomly : " << endl;          //Ask User To Enter 5 numbers
	for (int i = 0; i<5; i++)
	{
		cin >> array[i];                                        // Input for entering 5 numbers
	}
	cout << endl;

	cout << "a) Increasing Order !!.\n"                        // Displaying  Menu for user to choose between 3 choices
		"b) Decreasing Order  !!.\n"
		"c) Both Orders  !!.\n" << endl;

	char choice;
	cout << "enter a, b, or c: " << endl;               // Asking user to choose one of the choices
	cin >> choice;                                     //  input for user to choose 
	switch (choice)
	{                                                  //if user enters choice a
	case'a':
		for (int L = 0; L <= 4; L++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (array[j] > array[j + 1])
				{
					int	temp1 = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp1;
				}
			}
		}
		cout << "*******Increasing is: " << endl;
		for (int m = 0; m < 5; m++)
		{
			cout << array[m] << endl;                          // Displaying elements by increasing order
		}
		break;                                                 //Program stops
	case'b':
		for (int k = 0; k <= 4; k++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (array[j] < array[j + 1])
				{
					int	temp2 = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp2;
				}
			}
		}
		cout << "*******Decreasing is: " << endl;
		for (int m = 0; m < 5; m++)
		{
			cout << array[m] << endl;                              // displaying  elements by decreasing order
		}
		break;                                                     //Program stops
	case 'c':
		{
			break;                                                   //Program stops
		}
	default:                                                         //if user does not choose one of the above choices
		{
			cout << "You Entered Wrong Choice !!!!!" << endl;                 //display  message user entered wrong
		}
		break;
	}

	return 0;
}


This compiles and does part of what you want, option 'c' isn't finished (as you probably know). You had if statements in the middle of your switch statement! Removing those mostly fixed your problems. I would probably make separate functions to display them ascending/descending and call them in the appropriate spot in the switch statement.
Last edited on
Topic archived. No new replies allowed.