I'm very new to programming and I have to write a program that freaking me out somewhat. Its a project and a little bit lost. I have to write a program that asks for a filename, pulls the data from the file and organizes it. If the file isnt there then I have to return an error message. Any help is appreciated.
I know how to use the std::cin but not how to output an error if the file isn't found.
I've worked on one project with std::ifstream but I'm not comfortable using it.
As far as I can remember we haven't been taught what std::vector does or how to use it.
std::sort sounds familiar but I'm not sure.
Here's an example of one of the files:
New Jersey
1 Andrews,Robert D 2265 RHOB
2 LoBiondo,Frank R 2427 RHOB
3 Runyan,Jon R 1239 LHOB
4 Smith,Chris R 2373 RHOB
5 Garrett,Scott R 2232 RHOB
6 Pallone,Frank D 237 CHOB
7 Lance,Leonard R 133 CHOB
8 Sires,Albio D 2342 RHOB
9 Pascrell,Bill D 2370 RHOB
10 Payne,Donald D 103 CHOB
11 Frelinghuysen,Rodney R 2306 RHOB
12 Holt,Rush D 1214 LHOB
And the instructions say we are supposed to use getline because of the blank spaces.
int main()
{
string filename;
cout<<"Enter the name of the input file: ";
cin>> filename;
ifstream inFile;
inFile.open(filename);
if (!inFile)
{
cout<<"Error: file could not be opened\n";
exit(1);
}
while (!inFile.eof)
You can make your code look pretty in your post by putting it [code]between code tags[/code] so that it shows up easier to read.
There's a few problems with what you currently have.
1. You should use std::getline so that the user can enter a filename with spaces in it.
2. You need to check if !inFile.isOpen() is true, not if !inFile is true - they mean different things.
3. You shouldn't completely exit the application just because they entered the wrong filename. Could you imagine spending an hour inputting data and then suddenly you lose all your work just because they typed a filename wrong? You should persistently ask them over and over until they give a valid filename.
4. inFile.eof() is a function, so you need the parameter list () even if it has no parameters. Though you shouldn't have this as your loop condition anyway; you should try an input operation repeatedly until it fails.
I'm using code blocks and it's giving me two errors that I don't understand:
1 2 3
Z:\Morken_J_Prj3.cpp||In function 'void floor_level(int, std::string)':|
Z:\Morken_J_Prj3.cpp|154|error: no match for'operator==' in 'officenames == std::basic_string<char>(((const char*)"CHOB"), (*(const std::allocator<char>*)(& std::allocator<char>())))'|
Z:\Morken_J_Prj3.cpp|154|note: candidates are:|
on line 168 you are comparing a stirng variable to a char. (Party_names=='R'). You could try this (Party_names=="R") looks like the brace on line 164 needs to be moved to line 150, and line 151 should look like this string office_name (string officenames)
The brace on 178 is the end of the floor_level function. Move it to 151. The problem is that you accidentally defined functions inside another function.