updating array size

Please help i need to update the size of my array so that i can add another restaurant to the input. here is my code so far. can anyone help me? thanks a lot!



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()
{
	bool makan = true;
	bool found = false;
	string del;
	string resto[] = {"Applesbees","Betos","Cafe Rio","Dairy Queen","Edo Japan","Fazoli's","Golden Corral","Hot Dog on a stick"};
	const int max_size = 16;			
	int now = sizeof(resto)/sizeof(*resto);
	int current_size = now;
	
	while (makan)
	{
			cout<<"The Menu	"<<endl;
			cout<<"1. Display All restaurants"<<endl;
			cout<<"2. Add a restaurant"<<endl;
			cout<<"3. Remove a restaurant"<<endl;
			cout<<"4. Randomly rearrange the restaurants"<<endl;
			cout<<"5. Begin the tournament"<<endl<<endl;
			cout<<"Your choice : "; 
			int choice;
			
		
			cin>>choice;
			switch (choice)
			{
			case 1: for (int a=0; a<now; a++)
					{cout<<resto[a]<<endl;}
					cout<<endl;
					system ("Pause");
					system ("CLS");
					makan;continue;break;

			case 2:		  				
						 if (current_size < max_size)
						  {
							  cout<<"Enter the name of the restaurant you want to add : ";
							  string add; cin>>ws; getline(cin,add);
									  for(int pos=0; pos<current_size; pos++)
								{
									if ( add == resto[pos] )
									{
										found = true;
										cout << "The restaurant is already in the list!" << endl;
										break;								
									}
								}
								if (!found)
								{
									resto[current_size - 1] = add;
									cout << "The restaurant is successfully added to the list!" << endl;
								}							  						  				  
						  }
						 else
						 {
							 cout << "Number of restaurant is already maxed!" << endl;
						 }
						 
					system ("Pause");		
					system ("CLS");
					makan;
					continue;
					break;

			case 3:		cout << "Type in the name of the restaurant you want to delete : ";
						cin>>ws;
					    getline(cin,del);
						for(int pos=0; pos<current_size; pos++)
						{
							if ( del == resto[pos] )
							{
								found = true;
								resto[pos] = "";
								current_size--;
								cout << "The restaurant was successfully deleted!" << endl; // masih ada blank space//
								break;								
							}
						}
						if (!found) cout <<"Wrong input, not found" << endl;

					system ("Pause");		
					system ("CLS");
					makan;continue;break;

			case 4: for (int x=0; x < current_size - 1; x++)
					{
						string temp = resto[x];
						resto[x] = resto[x+1];
						resto[x+1] = temp;   // masih belom sempurna tapi ide nya udah dapet //
					}
				
					cout<<"Restaurant successfully rearranged!"<<endl;
					system ("Pause");
					system ("CLS");
					makan;continue;break;

			case 5: if (current_size != max_size) { cout << "Tournament cannot be started until numbers of restaurant is 16!" << endl;}
					system ("Pause"); system ("CLS");
					makan;continue;break;
			
			}

			system("Pause");
			return 0;
	}
	
}
look up "dynamic array"

change your code to be
1
2
int size=8;
string *resto=new string [size];

then initialize the array.

next, if you want to make it bigger by 1...
1
2
3
4
5
string *temp=new string[++size]
for (int i=0; i<size-1; i++)
    temp[i]=resto[i];
delete [] resto;
resto=temp;
Last edited on
Hi, you have a few options here. I've listed them in order of preference in a real-world perspective.

1) Use a STL container such as std::vector or std::list.

2) Use dynamic allocation for your restaurant array

3) Pick a constant maximum entry threshold for your statically allocated array.

Depending on what path you decide to take, the help that we can offer will be drastically different. If I might ask, can we see the problem statement so we can help you figure out what path that your instructor wants you to take?
threeright, your code worked, thank you., but if i want to display the restaurants after i added them, the name of the restaurant i added was not printed, its only a blank space. how do i fix that?
Luc Lieber, I dont understand what do you mean by problem statement? the maximum of the array is 16, the content should not be more than that, but the user can add another restaurant if the size is still less than 16.
By "Problem Statement", I meant the guidelines that either your instructor or text-book gave you that you are supposed to follow while solving the problem.
Last edited on
Topic archived. No new replies allowed.