Can you explain in bit more details what exactly you want to achieve?
Do you want to get input from the user in form of strings and then write these strings into a file, into an alphabetical order? Or am I missing something?
How would I alphabetize this with the different strings? Would I just need to use a bubble sort?
You are lucky you only have five strings. What if you needed a 100? How would your code look?
I suggest you look into some std containers. That would reduce your code to just a couple of lines, and also provide standard sort algorithm.
Last edited by VladimirF; May 16th, 2012 at 09:36 AM.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
well all I needed was a small database to store some lines of text but it uses user input and saves it to a text file, its something simple but it also doesn't save over the previous input. I just need to find a way to alphabetize the file after the inputs have been added.
well all I needed was a small database to store some lines of text but it uses user input and saves it to a text file, its something simple but it also doesn't save over the previous input. I just need to find a way to alphabetize the file after the inputs have been added.
Code:
#include <vector>
#include <string>
#include <algorithm>
typedef std::vector<std::string> StringArray;
using namespace std;
int main()
{
StringArray s;
//...
// names have been read into s
//...
// now sort s
sort(s.begin(), s.end());
}
As Vladimir stated, that one line of code sorts the strings if they are stored in the StringArray container.
As mentioned, you must change your code so that it stores strings in a container, not individual string variables. How are you going to sort 100 names, let alone 5 names, if all of those variables have different names?
Now, you don't know which string comes before which other string, and the variables are all named differently. I dare you to write a sorting function for these 5 separate strings, all with different variable names. I certainly wouldn't want to do it.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 16th, 2012 at 11:42 AM.
Alright after researching algorithms and trying to implement it into my code I have run into another problem.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
typedef vector <string> StringArray; /// line 7
using namespace std;
int main()
{
cout << "This program is a database for storing information." << endl;
ofstream myfile;
myfile. open ("data.txt", ios::app);
cout << "Please input what you want to store." << endl;
string s1;
string s2;
string s3;
string s4;
string s5;
getline (cin, s1);
myfile << s1 << endl;
getline (cin, s2);
myfile << s2 << endl;
getline (cin, s3);
myfile << s3 << endl;
getline (cin, s4);
myfile << s4 << endl;
getline (cin, s5);
myfile << s5 << endl;
sort (s.begin(), s.end(), greater<int>()); /// line 30
myfile. close ();
system("pause");
return 0;
}
I keep getting the errors
database.cpp|7|error: expected init-declarator before '<' token|
database.cpp|7|error: expected `,' or `;' before '<' token|
database.cpp||In function `int main()':|
database.cpp|30|error: `s' undeclared (first use this function)|
database.cpp|30|error: (Each undeclared identifier is reported only once for each function it appears in.)|
What am I doing wrong so I can figure out how to fix this?
Why are you still declaring 5 string variables? Again, what if you had 1000 names? Would you declare 1,000 variables? How about 10,000 names? Would you now declare 10,000 variables and write the same code over and over again 10,000 times? What if you wanted to sort all the names in the New York City phone book listing? 10,00,000 different variables and 10,00,000 copies of the same code? See how silly it will get? If that was the only way to write such programs in C++, no one would write programs in C++.
All you need is one variable -- a variable that can hold multiple names. That variable is the StringArray that I mentioned.
Code:
StringArray s; // a container that holds 5 names
std::string inString;
for (int i = 0; i < 5; ++i )
{
getline (cin, inString);
myfile << inString << endl;
s.push_back( inString );
}
This loop does exactly what your entire code did with those 5 separate input statements. If for some reason we need 1,000 names, then all I need to do is change that loop to go to 1000 instead of 5. Do you understand what's going on now?
Then when you sort, you are sorting s, which contains all of the names.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 22nd, 2012 at 01:25 PM.
10,00,000 copies of the same code? See how silly it will get? If that was the only way to write such programs in C++, no one would write programs in C++.
Your completely right and looping it is the best way to go thanks for the advice.
Bookmarks