I've spent the last 12 hours writing a code that is supposed to take two text files, one containing names and the other numbers, and merge them into one text file. The code then needs to sort the given text file alphabetically and display the first alphabetically displayed lines.
I've successfully managed to merge the files but I am struggling to organize the file in alphabetical order. The best answer I've found was converting the lines of the file into vectors and then sort using that. However, every time I do so I get a big error saying that the vector can't be found. I've yet to find any other solutions outside of vectors. Any input is welcome because I'm tired of this project.
I know the code looks rather sketch and messy, but right now it works. Though if anyone could help me improve it would be greatly appreciated. I'm using visual studio and I know it can be touchy sometimes. I'm near the end of the semester so use any code you feel fit to help.
> while(getline(ofs, oneword));
That trailing ; is a doozy. You made the body of the loop into a do-nothing loop.
1 2 3 4 5 6 7 8 9 10
while(getline(ofs, oneword))
{
; // this is the long form of what you wrote
}
// so this happens only once.
{
words.push_back(oneword);
// you probably want to sort them after you've read them all
sort(words.begin(), words.end());
}
Say
1 2 3 4 5 6
while(getline(ofs, oneword))
{
words.push_back(oneword);
}
// you probably want to sort them after you've read them all
sort(words.begin(), words.end());
> for (int i = 0; i <= 50; i++)
Use words.size(), not 50.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
int main()
{
const std::string in_file1_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNames.txt" ;
const std::string in_file2_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNums.txt" ;
const std::string out_file_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneCombined.txt" ;
if( std::ifstream names_file{in_file1_name} ) // if the names file was opened
{
// read the names into a vector
std::vector<std::string> vec ;
std::string name ;
while( std::getline( names_file, name ) ) vec.push_back(name) ;
// open the numbers file and append the numbers to the string after the corresponding name
if( std::ifstream numbers_file{in_file2_name} )
{
std::string number ;
std::size_t index = 0 ; // index into the vector, we start with the name at the front
for( ; index < vec.size() && numbers_file >> number ; ++index )
{
// pad the name with spaces on the right (to get the effect of << left << setw(32))
vec[index].resize( 32, ' ' ) ;
vec[index] += " | " + number ; // append the phone number
}
}
// sort the lines in the vector
std::sort( vec.begin(), vec.end() ) ;
// write the lines in the sorted vector to the output file
if( std::ofstream output_file{out_file_name} )
{
// range based loop: https://www.stroustrup.com/C++11FAQ.html#forfor( const std::string& line : vec ) output_file << line << '\n' ;
}
}
}
When I used your solution I came up with the same error that I usually get:
Unhandled exception at 0x00CBF2F6 (ucrtbased.dll) in Sorted Phone List.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
If I knew what this meant then maybe I could fix it, but sadly I don't. Do any of you guys happen to know what this means?
The program organizes the words in an odd way that I didn't expect. The way in which I've merged the numbers is:
I managed to get it going using you all as a reference. Thank you all for your help. I love this site. There was a lot of things wrong with my program so I needed to take a little bit from all of you plus just a little bit more outside help.