the code works totally fine now! the one problem is I need to free up the memory using delete[] but i still dont know where to use it. if i type the strings too long the program crashes. can anyone help me implement the delete[] in the next couple hours? thanks!
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void Out(char* string[], int i); //Initialization of the function 'Out'
int main()
{
cout << " String Database" << endl;
cout << " Please type some strings, and the database will sort them" << endl;
cout << " Press control+Z to end the string input" << endl;
char* string[10] = {0}; //the initialized string
for(int b=0;b<10;b++)
string[b]="zzzzzz";
for (int i = 0; i < 10; i++) //i represents the number of strings the user can enter
{ if(cin.eof())
{ cout<<"done"<<endl;
Out(string,i-1);
exit(0);
}
else
{ char* temp = new char; //declares 'temp' so that it can be set equal to
//string[i] later on
cin.getline(temp, 500); //the user enters his string
if (cin.eof()) //checks if ctrl+z has been entered. if true,
//then the function that outputs the sorted string
{ //will output it minus the last entry (ctrl+z)
cout << "done";
Out(string,i-1);
exit(0);
}
else if(i==0) //the first time a user enters a valid string
{ string[i] = temp; // makes the pointer point to the newly allocated space
}
else
{ int k=0;
while(strcmp(temp,string[k])>=0) //compares the temp string to the other strings
{ //that have been entered, so it knows where to sort the string
k++;
}
int p;
for(p=9;p>k;p--)
{ string[p]=string[p-1];
}
string[p]=temp;
}
}
}
char* temp = new char;
while(cin.getline(temp,500)) //while loop runs until the user enters ctrl+z, otherwise
{ if(cin.eof()) //it outputs an error message
{ cout<<"done"<<endl;
exit(0);
}
cout<<"You've already entered 10 strings, press ctrl+z"<<endl;
}
Out(string,9);
return 0;
}
//Functions
void Out(char* string[], int x) //Function that actually outputs the code, uses the pointer that was
//declared in the beginning of the code
{ cout << "\n";
cout << "Your sorted strings are: \n";
for(int i = 0; i <=x ; i++) //outputs the amount of strings in their sorted order
{ cout << string[i] << "\n";
}
}