| | |
Creating a Basic String Database
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Solved Threads: 0
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):
so I am working on a function for the first 'if' statement, called readstring:
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!
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):
c Syntax (Toggle Plain Text)
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;
c Syntax (Toggle Plain Text)
char *ReadString(char *a) { for (char *b = a; *b; b++)
c Syntax (Toggle Plain Text)
#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++)
Last edited by raydogg57; Feb 10th, 2007 at 3:52 pm.
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):
Not sure how to handle the Ctrl-Z off-hand though...
C++ Syntax (Toggle Plain Text)
#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...
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
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):
Not sure how to handle the Ctrl-Z off-hand though...C++ Syntax (Toggle Plain Text)
#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 }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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:
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.
char arrayOfStrings[10][257];
You can use a loop with 2 conditionals to control the input:
C++ Syntax (Toggle Plain Text)
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.
Last edited by Lerner; Feb 10th, 2007 at 7:13 pm.
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
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:
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.C++ Syntax (Toggle Plain Text)
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
c Syntax (Toggle Plain Text)
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!
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
>> 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.
•
•
•
•
also i think that the vector container has a sort function of his own, so you could you this to sort the strings!
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.
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Solved Threads: 0
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!
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!
Here's some pseudocode for two ways I might do it:
When you sort the char* array, you can use strcmp (from <cstring>) to compare them.
C++ Syntax (Toggle Plain Text)
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"; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ Practice Exam Help
- Next Thread: ctor/dtor problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






