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!
#include <iostream>
#include <string>
usingnamespace 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"};
constint 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;
}
}
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.