954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating a Basic String Database

I need to make a program that asks the user to input a number of strings, and then outputs the string database. The program should maintain a string database where each record is a string. The program keeps reading strings that the user has typed, and inserts the string at a sorted position in the database dynamically. The user presses ctrl+Z to end the process of inputting the strings, and then the program displays the whole string database.

The program only allows the user to input up to 10 strings, and if more are inputted an error message should be displayed.

Basically, the user types a number of sentences, and then the sentences are sorted (by the first letter of that sentence) and then outputted in that new order. So if someone typed:

Hello
How are you today
Are you okay?

the result would be:

Are you okay?
Hello
How are you today

The idea I have for my code is the following:

for the main function (pseudocode):

while (1){
read a string;
if not successful, end the while loop;
else insert the string at a sorted position in the string database;
}
display the string database;



so I am working on a function for the first 'if' statement, called readstring:

char *ReadString(char *a)
{
    for (char *b = a; *b; b++)



I believe this for loop scrolls through every character for a string, and I think what I need to do is have one pointer store the first character to one pointer, and then have that pointer store the next character as another pointer, and so on. I am really unclear as exactly how to approach this problem. If anyone can help me with this function/the rest of the problem that would be great, thanks!

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    while(1)
    {
        //read the string using ReadString function
        //If not successful, break;
        //else, insert the string at a sorted position in the database
    }
    //display the string database


}

//Functions

char *ReadString(char *a)
{
    for (char *b = a; *b; b++)
raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

If you're going to use C++ I/O, you might as well use the std::string class rather than a char*. And for reading in a whole line of input at a time, use getline(). For the database itself, a vector would probably be appropriate. You could do something like this (pseudo c++ code):

#include <vector>
#include <iostream>
#include <string>

int main()
{
  using namespace std;
  vector<string> stringDatabase;
  string input;
  while(true)
  {
     getline(cin, input, '\n'); // I forget the exact parameter order
    /* find where the string goes */
    /* put the string in the vector */
  }
  /* output the contents of the vector
}


Not sure how to handle the Ctrl-Z off-hand though...

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

If you're going to use C++ I/O, you might as well use the std::string class rather than a char*. And for reading in a whole line of input at a time, use getline(). For the database itself, a vector would probably be appropriate. You could do something like this (pseudo c++ code):

#include <vector>
#include <iostream>
#include <string>

int main()
{
  using namespace std;
  vector<string> stringDatabase;
  string input;
  while(true)
  {
     getline(cin, input, '\n'); // I forget the exact parameter order
    /* find where the string goes */
    /* put the string in the vector */
  }
  /* output the contents of the vector
}
Not sure how to handle the Ctrl-Z off-hand though...

i should have clarified that I need to do this assignment using pointers and arrays, thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

an array of strings is a 2D array of type char. If the array is to hold up to 10 strings and each may be up to 256 char each, then you could declare it like this:

char arrayOfStrings[10][257];

You can use a loop with 2 conditionals to control the input:

int numberOfStrings starts at zero
bool enterAnotherString is true

while(numberOfStrings < 11 and enterAnotherString is true)
   enter string into arrayOfStrings
   increcement numberOfStrings
   ask user if they want to enter another string
     if input is no
        AnotherString is false


Once user input is ended sort the strings using standard string functions like strcmp() and your favorite sorting algorhithm. Since you will have at most 10 strings to sort, a bubble sort should be satisfactory and seems to be a favorite of programmers just starting their career. If you have to inplement your own function to compare strings, good luck. It's doable, it's a reasonable learning exercise, and it's a bit of a pain.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

an array of strings is a 2D array of type char. If the array is to hold up to 10 strings and each may be up to 256 char each, then you could declare it like this:

char arrayOfStrings[10][257];

You can use a loop with 2 conditionals to control the input:

int numberOfStrings starts at zero
bool enterAnotherString is true

while(numberOfStrings < 11 and enterAnotherString is true)
   enter string into arrayOfStrings
   increcement numberOfStrings
   ask user if they want to enter another string
     if input is no
        AnotherString is false
Once user input is ended sort the strings using standard string functions like strcmp() and your favorite sorting algorhithm. Since you will have at most 10 strings to sort, a bubble sort should be satisfactory and seems to be a favorite of programmers just starting their career. If you have to inplement your own function to compare strings, good luck. It's doable, it's a reasonable learning exercise, and it's a bit of a pain.

to be safe I am going to make it 500 characters. I know that we are not supposed to ask the user to input more strings than 10, so I don't believe I need the boolean. basically the user can keep entering strings until he or she presses ctrl z (^z). any strings he or she enters more than 10 will just result in an error "No more strings!". so the program only has to record the first 10 strings it enters. i am unclear, however, how to implement this using ptrs. how do I get the user to keep inputting strings? is it just something like while numberstrings <11, cin.getline; numberstrings++;? but I would need a function after cin.getline that saves and stores the string to a new location to sort later, correct? so could the code be something like this:

int numberstrings == 0;
while (numberstrings<11)
{
cin.getline(string, 500)
store_string(char*string) // where store_string is the storage function;
numberstrings++;
}
//after while loop ends, I can create a function to display the sorted strings.


does this appear right? and if so, how can i go about creating this store_string function? and do I need a function later to sort the strings after they all have been entered? since the user can quit entering strings early, should this be an 'if' statement instead? thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Lerner: A couple problems with the code you posted:

>> 1. int numberstrings == 0;

you need to use the assignment operator = instead of boolean operator ==.:mrgreen:

>>5. store_string(char*string)
This is a function prototype, not a function call

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

also i think that the vector container has a sort function of his own, so you could you this to sort the strings!

n.aggel
Posting Whiz in Training
203 posts since Nov 2006
Reputation Points: 23
Solved Threads: 12
 
also i think that the vector container has a sort function of his own, so you could you this to sort the strings!

The sort function is a function of STL, nor vector. Here is an example of how to vector of sort c++ classes

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

so i want to continue from the code i was thinking of up above, where there is a while loop for numberstrings<11. then i have a function which stores the string to a separate database each time. what would be a correct way to write this function?

I was thinking something where:

char *ReadString(char *a)
{
for (char *b = a; *b; b++)

so it reads through every character in the string first, then can I do something like:

c* = b, underneith the for loop? I want something so that it stores the whole string to another location for each string, so i can sort it later. so say i wanted string one to go to location: 00FFFF0000 or something, then it stores string 2 to location 00FFFF00001, so that i can sort all these locations later. does this make sense? will something like c*=b store the entire string? or will it just keep replacing c with each letter in that string. do i need to make a string as a pointer? like string1* = b? this is due tomorrow, any help i can get would be great! thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Here's some pseudocode for two ways I might do it:

int main()
{
  char* stringDB[10] = {0};

  // method 1: add in order
  while(/* condition to keep inputting */)
  {
    read in a line
    find where the line goes
    put the line in the correct spot, adjusting others as necessary
  }

  // method 2
  for(int i = 0; i < 10 && /* condition */; ++i)
  {
    input string and add it as stringDB[i];
  }
  sort stringDB;

  // then output
  for(int i = 0; i < 10; ++i)
    std::cout << stringDB[i] << "\n";
}



When you sort the char* array, you can use strcmp (from ) to compare them.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

Here's some pseudocode for two ways I might do it:

int main()
{
  char* stringDB[10] = {0};

  // method 1: add in order
  while(/* condition to keep inputting */)
  {
    read in a line
    find where the line goes
    put the line in the correct spot, adjusting others as necessary
  }

  // method 2
  for(int i = 0; i < 10 && /* condition */; ++i)
  {
    input string and add it as stringDB[i];
  }
  sort stringDB;

  // then output
  for(int i = 0; i < 10; ++i)
    std::cout << stringDB[i] << "\n";
}
When you sort the char* array, you can use strcmp (from ) to compare them.

i want to go with method 2. So I tried the following code with my Visual C++, but it gave me an error about a null pointer and the program wouldn't run. Right now I just want to worry about getting the main part of my function to work, without sorting the strings for now. Once I get my program to work with storing the strings I can worry about creating a function to sort them. so this is my code thus far:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    char* string[10] = {0};

    for (int i = 0; i <= 10; ++i)
    {
        cin.getline(string[i], 500);
    }

    //Function to sort the string

    for(int i = 0; i <= 10; ++i)
    {
    cout << string[i] << "\n";
    }
    
    


}


Can anyone see why it gives me a null pointer error? thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

There's two bugs I see here. One's my fault, the other isn't ;)

The first is that the array values are set to 0, which is null memory. They need to be given different values so that they point to memory you can write to. There's two ways to do this:
- Initialize them all at the beginning:

char* string[10];
for(int i = 0; i < 10; ++i)
  string[i] = new char[500];
// ... rest of the program
for(int i = 0; i < 10; ++i)
  delete string[i]; // free the memory
return 0; // end of main


- allocate them as you need them:

char* string[10] = {0};
// loop to read things in:
for(int i = 0; i < 10; ++i)
{
  char* temp = new char[500];
  cin.getline(temp, 500);
  string[i] = temp; // makes the pointer point to the newly allocated space
  // also need to delete at the end of the end of the program
}


The benefit of the second is that if the user only inputs 3 strings, you only allocate memory 3 times.

The second bug is that you have <= in the for loop conditions, when it should just be <. Easy fix ;)

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

thanks! i went with the second method and it worked fine. I think i can figure out how to execute the program early when the user types ^z, but i need help with sorting the database after its been stored. the program needs to sorts the strings in ascending order according to the first letter of each string. so do I need to create a function that points to the first letter of each string? and then sorts it? and is there already a preexisting function that will take my string database and sort it in ascending order for me already? thanks! below is my code thus far:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    char* string[10] = {0};

    for (int i = 0; i < 10; ++i)
    {
        char* temp = new char[500];
        cin.getline(temp, 500);
        string[i] = temp;
    }

    //Function to sort the string

    for(int i = 0; i <= 10; ++i)
    {
    cout << string[i] << "\n";
    }
    
    


}

//Functions
raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Your last for loop is off by one -- running from 0 to 10 where it should have gone from 0 to 9.

As far as sorting is concerned, something like t his might be worth reading to see if it serves your purpose....

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

You shouldn't be using string as a variable name.

And don't forget to use delete[] if you're using new .

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

You shouldn't be using string as a variable name.

And don't forget to use delete[] if you're using new .

Right now I am having a problem with the sort part of my code at this point:

while(temp>=string[k])


The program is not recognizing this comparison correctly. below is my current code:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;



int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    char* string[10] = {0};
    for(int b=0;b<10;b++)
        string[b]="zzzzzz";
    
    

    for (int i = 0; i < 10; i++)
    {    
        char* temp = new char;//[500];
        cin.getline(temp, 500);
        if(i==0)
        {    string[i] = temp; // makes the pointer point to the newly allocated space
        }
        else
        {    int k=0;
            while(temp>=string[k])
            {    cout<<temp<<">="<<string[k]<<"?"<<endl;
                k++;
            }
            cout<<k<<endl;

            int p;
            for(p=9;p>k;p--)
            {    string[p]=string[p-1];
            }
            string[p]=temp;
        }
    }

    for(int i = 0; i < 10; ++i)
    {  cout << string[i] << "\n";
    }
    
    return 0;


}


Right now the program outputs whether each character you type is greater than the other. but if you type 'd' on one line, and then 'b' the next, it says 'b' is > 'd'. can anyone help me debug this? thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

That's because you're confusing c++ and c.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
That's because you're confusing c++ and c.

I just switched my code around to use strcmp and now it works fine! I now need help implementing a function that executes the program early if i type ^Z or ^z. if the user wants to sort the strings early they input this command. So I was thinking of making a boolean function that returns false if they input this, but i want to make sure that the ^Z is not included in the database when it sorts. this is what i have for my code thus far but I am having problems with it:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

bool z();

int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    char* string[10] = {0};
    for(int b=0;b<10;b++)
        string[b]="zzzzzz";
    
    

    for (int i = 0; i < 10 && z(); i++)
    {    
        char* temp = new char;//[500];
        cin.getline(temp, 500);
        if(i==0)
        {    string[i] = temp; // makes the pointer point to the newly allocated space
        }
        else
        {    int k=0;
            while(strcmp(temp,string[k])>=0)
            {    
                k++;
            }
            

            int p;
            for(p=9;p>k;p--)
            {    string[p]=string[p-1];
            }
            string[p]=temp;
        }
    }

    for(int i = 0; i < 10; ++i)
    {  cout << string[i] << "\n";
    }
    
    return 0;


}

//Functions

bool z()
{ char* a[10]={0};
    cin.getline(blah);
    if(a=="^z" || a=="^Z")
        return false;
    return true;
}


it gives me conversion errors. does anyone know if I can use a boolean function like this to accomplish what I need to get done? do I need to do something else to make sure that ^Z wont be included in the database after it sorts? thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
does that mean I can't do the comparison that way? i need to do this using c++

No, you can do the comparison using char * instead of std::strings. But you would have to use strcmp instead. And you would have to include the header file #include <cstring> .


I believe this was mentioned before.

Unfortunately, because c++ accepts both c and c++ syntax it is very easy for newbies, like yourself to try and use the two variants of c and c++ as one language.

Of course there is nothing wrong with that if you know what you're doing. You don't.

Therefore I would advise you to use either one or the other. c or c++, unless of course your teacher wants you to use char* with c++.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

No, you can do the comparison using char * instead of std::strings. But you would have to use strcmp instead. And you would have to include the header file #include <cstring> .

I believe this was mentioned before.

Unfortunately, because c++ accepts both c and c++ syntax it is very easy for newbies, like yourself to try and use the two variants of c and c++ as one language.

Of course there is nothing wrong with that if you know what you're doing. You don't.

Therefore I would advise you to use either one or the other. c or c++, unless of course your teacher wants you to use char* with c++.

That's because you're confusing c++ and c.

I just switched my code around to use strcmp and now it works fine! I now need help implementing a function that executes the program early if i type ^Z or ^z. if the user wants to sort the strings early they input this command. So I was thinking of making a boolean function that returns false if they input this, but i want to make sure that the ^Z is not included in the database when it sorts. this is what i have for my code thus far but I am having problems with it:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

bool z();

int main()
{
    cout << "                            String Database" << endl;
    cout << "         Please type some strings, and the database will sort them" << endl;
    cout << "                  Press ^Z to end the string input" << endl;

    char* string[10] = {0};
    for(int b=0;b<10;b++)
        string[b]="zzzzzz";
    
    

    for (int i = 0; i < 10 && z(); i++)
    {    
        char* temp = new char;//[500];
        cin.getline(temp, 500);
        if(i==0)
        {    string[i] = temp; // makes the pointer point to the newly allocated space
        }
        else
        {    int k=0;
            while(strcmp(temp,string[k])>=0)
            {    
                k++;
            }
            

            int p;
            for(p=9;p>k;p--)
            {    string[p]=string[p-1];
            }
            string[p]=temp;
        }
    }

    for(int i = 0; i < 10; ++i)
    {  cout << string[i] << "\n";
    }
    
    return 0;


}

//Functions

bool z()
{ char* a[10]={0};
    cin.getline(a);
    if(a=="^z" || a=="^Z")
        return false;
    return true;
}


it gives me conversion errors. does anyone know if I can use a boolean function like this to accomplish what I need to get done? do I need to do something else to make sure that ^Z wont be included in the database after it sorts? thanks!

raydogg57
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You