Help needed

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.
How much have you written already?

Do you know how do user input with std::cin? How to validate and ask again in case of error?

Do you know how to use C++ file streams such as std::ifstream?

Do you know how to store data in a collection like std::vector? Are you allowed to use STL classes like std::vector?

Do you know how to use std::sort? Do you have to write your own sorting algorithm?
I've just started the project.

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.
Here's what I have so far.

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>


using namespace std;


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)



return 0;
}

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've been working and this is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<iostream>
#include <string>
#include<fstream>
#include <iomanip>
using namespace std ;

void floor_level (int, string);
string office_name (string);
string Parties (char);
char party;

  int main()
   {
         int District_number,room_number;
         string name, office_building, office, state, building_name, Party_names;
         char party;
         int count1=0, count2=0;


         ifstream inFile;    //input file stream variable
         string filename;    // name of the input file


        cout << "Enter the name of the input file:  " ;
        cin >> filename;

        inFile.open(filename.c_str ()  );

        //inData.open("numbers1.txt");
        if ( !inFile)
         {
              cout << " input file not found \n";
              return 1;
         }
        getline(inFile, state);

        cout<<"\n==========================================\n";
        cout<<"|  Directory for the representatives from  |\n";
        cout<<"|                                          |\n";
        cout<<"|              "<<state<<"                 |\n";
        cout<<"|                                          |\n";
        cout<<"============================================";



         while ( inFile!=0)
         {
         inFile>>District_number>>name>>party>>room_number>>office_building;

         building_name=office_name(office_building);
         Party_names=Parties(party);

         cout<<left<<"District"<<District_number;
         cout<<" "<<Party_names<<"\n";
         cout<<setw(15)<<name<<"\t";
         cout<<setw(15)<<" "<<building_name;
         cout<<left<<" Room "<<room_number;
         floor_level(room_number,office_building);
         cout<<"\n\n";

                if(party=='R')
                    {
                        count1++;
                    }
                else if(party=='D')
                    {
                        count2++;
                    }
         }

    cout<<"Number of Republicans in "<<state<<": "<<count1<<"\n";
    cout<<"Number of Democrats in "<<state<<": "<<count2<<"\n";

    cout<<"\nProgrammer: Josef Morken\n";
    cout<<"Date:3/22/2013\n";
    cout<<"CRN:23856\n";

         return 0;
   }
    void floor_level(int room_number, string office_building)
    {
        if (office_building == "CHOB")
        {

        if (room_number<200)
            cout<<"First Floor";
        else if(room_number>200 && room_number<300)
        {
            cout<<"Second Floor";
        }
        else if(room_number>300 && room_number<400)
        {
            cout<<"Third Floor";
        }

        else if(room_number>400 && room_number<500)
        {
            cout<<"Fourth Floor";
        }
        else if(room_number>500 && room_number<600)
        {
            cout<<"Fifth Floor";
        }
        }
    if (office_building == "LHOB")
        {

        if (room_number<1200)
            cout<<"First Floor";
        else if(room_number>1200 && room_number<1300)
        {
            cout<<"Second Floor";
        }
        else if(room_number>1300 && room_number<1400)
        {
            cout<<"Third Floor";
        }

        else if(room_number>1400 && room_number<1500)
        {
            cout<<"Fourth Floor";
        }
        else if(room_number>1500 && room_number<1600)
        {
            cout<<"Fifth Floor";
        }
        }
    if (office_building == "RHOB")
        {

        if (room_number<2200)
            cout<<"First Floor";
        else if(room_number>2200 && room_number<2300)
        {
            cout<<"Second Floor";
        }
        else if(room_number>2300 && room_number<2400)
        {
            cout<<"Third Floor";
        }

        else if(room_number>2400 && room_number<2500)
        {
            cout<<"Fourth Floor";
        }
        else if(room_number>2500 && room_number<2600)
        {
            cout<<"Fifth Floor";
        }
        }
    string officenames(string office_name);
    {
             string buildingname;
            if (officenames==string("CHOB"))
               buildingname = "Cannon Building";
            if (officenames==string("LHOB"))
               buildingname = "Longworth Building";
            if (officenames==string("RHOB"))
               buildingname = "Rayburn Building";

            return buildingname;

    }
    }
string Parties(char party)
{
     string Party_names;
      if (Party_names=='R')
           Party_names="Republican";
       else if (Party_names=='D')
           Party_names="Democrat";

       return Party_names;
}
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:|
Last edited on
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)
I've fixed those issues, now it says that I cannot have a function-definition befe a "{" token.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include <string>
#include<fstream>
#include <iomanip>
using namespace std ;

void floor_level (int, string);
string office_name (string);
string Parties (char);
char party;

  int main()
   {
         int District_number,room_number;
         string name, office_building, office, state, building_name, office_names, Party_names;
         char party;
         int count1=0, count2=0;


         ifstream inFile;    //input file stream variable
         string filename;    // name of the input file


        cout << "Enter the name of the input file:  " ;
        cin >> filename;

        inFile.open(filename.c_str ()  );

        //inData.open("numbers1.txt");
        if ( !inFile)
         {
              cout << " input file not found \n";
              return 1;
         }
        getline(inFile, state);

        cout<<"\n==========================================\n";
        cout<<"|  Directory for the representatives from  |\n";
        cout<<"|                                          |\n";
        cout<<"|              "<<state<<"                 |\n";
        cout<<"|                                          |\n";
        cout<<"============================================";



         while ( inFile!=0)
         {
         inFile>>District_number>>name>>party>>room_number>>office_building;

         building_name=office_name(office_building);
         Party_names=Parties(party);

         cout<<left<<"District"<<District_number;
         cout<<" "<<Party_names<<"\n";
         cout<<setw(15)<<name<<"\t";
         cout<<setw(15)<<" "<<building_name;
         cout<<left<<" Room "<<room_number;
         floor_level(room_number,office_building);
         cout<<"\n\n";

                if(party=='R')
                    {
                        count1++;
                    }
                else if(party=='D')
                    {
                        count2++;
                    }
         }

    cout<<"Number of Republicans in "<<state<<": "<<count1<<"\n";
    cout<<"Number of Democrats in "<<state<<": "<<count2<<"\n";

    cout<<"\nProgrammer: Josef Morken\n";
    cout<<"Date:3/22/2013\n";
    cout<<"CRN:23856\n";

         return 0;
   }
void floor_level(int room_number, string office_building, string office_names)
    {
    if (office_building == "CHOB")
        {

        if (room_number<200)
                cout<<"First Floor";
            else if(room_number>200 && room_number<300)
            {
                cout<<"Second Floor";
            }
            else if(room_number>300 && room_number<400)
            {
                cout<<"Third Floor";
            }

            else if(room_number>400 && room_number<500)
            {
                cout<<"Fourth Floor";
            }
            else if(room_number>500 && room_number<600)
            {
                cout<<"Fifth Floor";
            }
        }
        if (office_building == "LHOB")
        {

            if (room_number<1200)
                cout<<"First Floor";
            else if(room_number>1200 && room_number<1300)
            {
                cout<<"Second Floor";
            }
            else if(room_number>1300 && room_number<1400)
            {
                cout<<"Third Floor";
            }

            else if(room_number>1400 && room_number<1500)
            {
                cout<<"Fourth Floor";
            }
            else if(room_number>1500 && room_number<1600)
            {
                cout<<"Fifth Floor";
            }
        }
        if (office_building == "RHOB")
        {

            if (room_number<2200)
                cout<<"First Floor";
            else if(room_number>2200 && room_number<2300)
            {
                cout<<"Second Floor";
            }
            else if(room_number>2300 && room_number<2400)
            {
                cout<<"Third Floor";
            }

            else if(room_number>2400 && room_number<2500)
            {
                cout<<"Fourth Floor";
            }
            else if(room_number>2500 && room_number<2600)
            {
                cout<<"Fifth Floor";
            }
        }

    string officenames(string office_names)
    {   string buildingname;
            if (office_names==("CHOB"))
               buildingname = "Cannon Building";
            if (office_names==("LHOB"))
               buildingname = "Longworth Building";
            if (office_names==("RHOB"))
               buildingname = "Rayburn Building";

            return buildingname;
    }


    string Parties(char party)
    {
     string Party_names;
      if (party=='R')
           Party_names="Republican";
      if (party=='D')
           Party_names="Democrat";

       return Party_names;
    }



    }
the errors are on lines 153 and 166
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.
Topic archived. No new replies allowed.