Switch case problem

Hi all, I'm at the end of my assignment, I am just stuck on one last thing. Within my switch case I have the user enter a number between 1-5.
Option 1 should enter a new SIN at the end of your array.
Option 2 will delete a SIN from the array.
Option 3 will print the array as-is (unsorted and/or sorted)
Option 4 will sort the array using our bubble sort algorithm
Option 5 will exit program.
The problem I am running into is that when I enter 3 SINs at the start and print them out, they all print out, but when I back to option 1 to insert another SIN, and then print again, it's like the array resets or something, it will not show the first 3 SINs and the new one, it will display just the new one. Any suggestions would be greatly appreciated!!! :)

Here is my 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
while (choice != 5)
	{
	switch  (choice)
	{
		case '1': 
		
		
			for (i = 0; i < SIZE; i++){
			cout << "Enter SIN number. Press 0 to stop entering numbers." << endl;
			cin >> myArray[i];
			//cout << "i = " << myArray[i] << ", " << endl;
			if (myArray[i] == 0){
			break;
			}
			}
			break;

		case '2':
			for (i = 0; i < SIZE; i++)
			{
			myArray[i] = -1;
			//cout << myArray[i] << endl;
			
			}
			cout << *myArray;
			break;
				
		case '3':
			for (i = 0; i < SIZE; i++)
			{
			cout << myArray[i] << endl;
			}
			break;

		case '4':
			for (int i = 0; i < SIZE; i++)
			{
			ArraySorter sorter;
			sorter.setSize(5);
			sorter.setArray(myArray);
			sorter.doBubbleSort();
			}
			break;
		default:
			cout << "Must enter a valid response!" << endl;
		//end switch case
	}

	meTest.displayMenu();
	cin >> choice;
	
	}
cuz you're over-writing the old values each time you go back into the for loop.
If i'm understanding the problem correctly that is.
I believe you're right mutexe, I am just unsure of how to go about NOT over-writing the old values.
change your array to a vector and use push_back (http://www.cplusplus.com/reference/vector/vector/push_back/)

to ADD your SIN number to the vector. You wont over-write values then.

and on line 7 call clear() on the vector (http://www.cplusplus.com/reference/vector/vector/clear/).
Last edited on
The assignment asks for array, not a vector.
what's the value of SIZE?



100
okay, so when you press 0 to say you dont want anymore SINs to be entered, you have at point the number of SINs in your array (i). Save this int to another variable (e.g. nNumSINs).

That variable will be "how full" your array is, so when you wanna add some more you use this val as the index to add at, NOT 0.
I don't quite understand. What I am getting out of this is that I should do:

int numSins = i; // where do I do this? Also where do I use numSins?
this line:

cin >> myArray[i];

will work the first time around (as you've discovered), but not subsequent times.

You dont want to put it in the i'th element. You want to keep track of how big that array is. Hence my suggestion to remember how many you've already put in there.

edit: oops, not "how big that array is", you want to keep track of how many elements in your array have been 'set'.
Last edited on
Why do I need to keep track? I just need to make it so after I use option 3 (print array) I can enter in a new element and then print all of my elements instead of it overlapping.
Let's see what happens. User enters '1' to enter SIN.

1
2
3
4
5
6
7
8
9
for (i = 0; i < SIZE; i++){
			cout << "Enter SIN number. Press 0 to stop entering numbers." << endl;
			cin >> myArray[i];
			//cout << "i = " << myArray[i] << ", " << endl;
			if (myArray[i] == 0){
			break;
			}
			}
			break;


Each time the user enters a SIN, value of "i" increases by one. If the user enters 3 SIN, by the time you exit this part "i" should be 3 (including entering of "0" to exit). Say the 3 SIN are 3, 5 and 10;

myArray[0] = 3;
myArray[1] = 5;
myArray[2] = 10;
myArray[3] = 0;

Now the user prints it yada yada and wants to enter more SIN.

User enters "1" again to go to case 1.

for (i = 0; i < SIZE; i++)

"i" is assigned back to 0. Let's say the user wants to input one new SIN and it is 1000. Your array looks like this now.

myArray[0] = 1000;
myArray[1] = 0; <----- To exit.
myArray[2] = 10;
myArray[3] = 0;

Listen to mutexe's suggestion.

Edit: Additionally, knowing exactly how big your array is will prevent you from printing garbage values (or 0 values if your initialized your array) by having a proper limit set for your for loop conditions (instead of just using 100).

If there's only 3 elements entered in your array, your array size isn't 100 right? What happens when you try to print myArray[4] and more?
Last edited on
Okay so I have the element being inserted into the array, but when I enter 100, 200, 300 and then enter 150, it does not get inserted into the back of the array, when I print out all my array elements it comes out 100, 150, 200, 300 instead of 100, 200, 300, 150. Here is my code so far:

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
while (choice != 5)
	{
	switch  (choice)
	{
		case '1': 
			for (i = 0; i < SIZE; i++){
			cout << "Enter SIN number. Press 0 to stop entering numbers." << endl;
			cin >> myArray[i];
			int numSins;
			for (int next = 0; next < SIZE; ++next){
				numSins = myArray[next];
				int moveItem = next;
			while((moveItem > 0) && (myArray[moveItem-1] > numSins))
			{
			myArray[moveItem] = myArray[moveItem - 1];
			moveItem--;
			}
			myArray[moveItem] = numSins;
			}
				for (i = 0; i < SIZE; i++){
					cout << myArray[i];
					cout << endl;
				}
			
			if (myArray[i] == 0){
			break;
			}
			}
			
			cout << endl;
			break;
I'm not very sure what's going on in that code. Trying it out myself, it doesn't even work properly. Why even bother reassigning "i" back to 0 here? Why not just leave it the way it is?

Have the user input to a local variable (ie. an int variable declared inside case 1). If it is 0, break out of the loop. If it isn't 0, assign that value to myArray[i]. Only increment "i" when a valid input has been entered.

Or it can be done your way. If you can explain your thought process for that code it'll be great.
Topic archived. No new replies allowed.