Creating a Basic String Database

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Creating a Basic String Database

 
0
  #1
Feb 10th, 2007
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):
  1. while (1){
  2. read a string;
  3. if not successful, end the while loop;
  4. else insert the string at a sorted position in the string database;
  5. }
  6. display the string database;
so I am working on a function for the first 'if' statement, called readstring:

  1. char *ReadString(char *a)
  2. {
  3. 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!

  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6.  
  7. int main()
  8. {
  9. cout << " String Database" << endl;
  10. cout << " Please type some strings, and the database will sort them" << endl;
  11. cout << " Press ^Z to end the string input" << endl;
  12.  
  13. while(1)
  14. {
  15. //read the string using ReadString function
  16. //If not successful, break;
  17. //else, insert the string at a sorted position in the database
  18. }
  19. //display the string database
  20.  
  21.  
  22. }
  23.  
  24. //Functions
  25.  
  26. char *ReadString(char *a)
  27. {
  28. for (char *b = a; *b; b++)
Last edited by raydogg57; Feb 10th, 2007 at 3:52 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Creating a Basic String Database

 
0
  #2
Feb 10th, 2007
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):
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8. vector<string> stringDatabase;
  9. string input;
  10. while(true)
  11. {
  12. getline(cin, input, '\n'); // I forget the exact parameter order
  13. /* find where the string goes */
  14. /* put the string in the vector */
  15. }
  16. /* output the contents of the vector
  17. }
  18.  

Not sure how to handle the Ctrl-Z off-hand though...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Creating a Basic String Database

 
0
  #3
Feb 10th, 2007
Originally Posted by Infarction View Post
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):
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8. vector<string> stringDatabase;
  9. string input;
  10. while(true)
  11. {
  12. getline(cin, input, '\n'); // I forget the exact parameter order
  13. /* find where the string goes */
  14. /* put the string in the vector */
  15. }
  16. /* output the contents of the vector
  17. }
  18.  
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Creating a Basic String Database

 
0
  #4
Feb 10th, 2007
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:

  1. int numberOfStrings starts at zero
  2. bool enterAnotherString is true
  3.  
  4. while(numberOfStrings < 11 and enterAnotherString is true)
  5. enter string into arrayOfStrings
  6. increcement numberOfStrings
  7. ask user if they want to enter another string
  8. if input is no
  9. 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.
Last edited by Lerner; Feb 10th, 2007 at 7:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Creating a Basic String Database

 
0
  #5
Feb 10th, 2007
Originally Posted by Lerner View Post
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:

  1. int numberOfStrings starts at zero
  2. bool enterAnotherString is true
  3.  
  4. while(numberOfStrings < 11 and enterAnotherString is true)
  5. enter string into arrayOfStrings
  6. increcement numberOfStrings
  7. ask user if they want to enter another string
  8. if input is no
  9. 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:

  1. int numberstrings == 0;
  2. while (numberstrings<11)
  3. {
  4. cin.getline(string, 500)
  5. store_string(char*string) // where store_string is the storage function;
  6. numberstrings++;
  7. }
  8. //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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,334
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1454
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Creating a Basic String Database

 
0
  #6
Feb 10th, 2007
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Creating a Basic String Database

 
0
  #7
Feb 11th, 2007
also i think that the vector container has a sort function of his own, so you could you this to sort the strings!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,334
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1454
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Creating a Basic String Database

 
0
  #8
Feb 11th, 2007
Originally Posted by n.aggel View Post
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Creating a Basic String Database

 
0
  #9
Feb 11th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Creating a Basic String Database

 
0
  #10
Feb 11th, 2007
Here's some pseudocode for two ways I might do it:

  1. int main()
  2. {
  3. char* stringDB[10] = {0};
  4.  
  5. // method 1: add in order
  6. while(/* condition to keep inputting */)
  7. {
  8. read in a line
  9. find where the line goes
  10. put the line in the correct spot, adjusting others as necessary
  11. }
  12.  
  13. // method 2
  14. for(int i = 0; i < 10 && /* condition */; ++i)
  15. {
  16. input string and add it as stringDB[i];
  17. }
  18. sort stringDB;
  19.  
  20. // then output
  21. for(int i = 0; i < 10; ++i)
  22. std::cout << stringDB[i] << "\n";
  23. }
When you sort the char* array, you can use strcmp (from <cstring>) to compare them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC