Does my header look alright?

This is only the second time I've made a header for my code, but last time my code was just a bunch of functions. This time, these functions are in a class. I just want to double check and make sure that there aren't any different rules to adhere to.

Here's my cpp code:

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
#include "user_input_handler.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

class UserInputHandler {
public:
  char GetSingleCharacter() {
    std::string str;
    char letter;
    int i;
    const std::string invalid_char_input_("Failure: Too many characters.");
    const std::string char_not_allowed_("Failure: Invalid character.");
    bool loopend = false;
    while (loopend == false) {
      std::getline(std::cin, str);
      i = (int)str.length();
      if (std::string::npos == str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
        if (i > 1) {
          std::cout << invalid_char_input_ << endl;
        } else {
          letter = str[0];
          std::cout << "Character Entered: " << letter << endl;
          loopend = true;
        }
      } else {
        std::cout << char_not_allowed_ << endl;
      }
    }
    return letter;
  }
  int GetInteger(const int& min, const int& max) {
    const std::string invalid_int_("Failure: No integer found.");
    const std::string low_int_("Failure: Integer is below minimum value.");
    const std::string high_int_("Failure: Integer is above maximum value.");
    int result;
    bool loopend = false;
    while (loopend == false) {
      std::string line;
      if (getline(std::cin, line)) {
        std::stringstream ss(line);
        if (ss >> result && ss.eof()) {
          if (result < min) {
            std::cout << low_int_ << endl;
          } else if (result > max) {
            std::cout << high_int_ << endl;
          } else {
            std::cout << "Integer Entered is: " << result << endl;
            loopend = true;
          }
        } else {
        std::cout <<invalid_int_ << endl;
        result = 0;
        }
      }
    }
  return result;
  }
  double GetDouble() {
    const std::string invalid_double_("Failure: No double found.");
    double result;
    std::string line;
    bool loopend = false;
    while (loopend == false) {
      if (getline(std::cin, line)) {
        std::stringstream ss(line);
        if (!(ss >> result && ss.eof())) {
          std::cout <<invalid_double_ << endl;
        } else {
          std::cout << "Double Entered is: " << result << endl;
          loopend = true;
        }
      }
    }
    return result;
  }
    bool GetBoolean() {
    std::string str;
    bool test;
    string result;
    const std::string invalid_bool_("Failure: Must input 'T', 'F', 'true', or 'false'. Case sensitive.");
    bool loopend = false;
    while (loopend == false) {
      std::getline(std::cin, str);
      if (str == "T") {
        test = true;
        result = "true";
        loopend = true;
      } else if ( str == "F") {
        test = false;
        result = "false";
        loopend = true;
      } else if (str == "true") {
        test = true;
        result = "true";
        loopend = true;
      } else if (str == "false") {
        test = false;
        result = "false";
        loopend = true;
      } else {
        std::cout << invalid_bool_ << endl;
      }
    }
    std::cout << "Boolean: " << result << endl;
    return test;
  }
  string GetString(int charnumber = -1, bool empty = true) {
    const std::string too_long_string_("Failure: Too many characters");
    const std::string empty_string_("Failure: String is empty.");
    int i;
    bool infinite;
    if (charnumber == -1) {
      infinite = true;
    }
    std::string str;
    bool loopend = false;
    while (loopend == false) {
      std::getline(std::cin, str);
      i = (int)str.length();
      if (i == 0) {
        if (empty == false) {
          std::cout << empty_string_ << endl;
        } else {
          std::cout << "String Entered: " << str << endl;
          loopend = true;
        }
      } else if (i > charnumber) {
        if (infinite == false) {
          std::cout << too_long_string_ << endl;
        } else {
          std::cout << "String Entered: " << str << endl;
          loopend = true;
        }
      } else {
        std::cout << "String Entered: " << str << endl;
        loopend = true;
      }
    }
    return str;
  }
};

int main() {
  UserInputHandler handler;
  handler.GetSingleCharacter();
  handler.GetInteger(0,100);
  handler.GetDouble();
  handler.GetBoolean();
  handler.GetString();
}


And here's my header:

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef USER_INPUT_HANDLER_H
#define USER_INPUT_HANDLER_H
#include <string>
using std::string;

char GetSingleCharacter();
int GetInteger(const int& min, const int& max);
double GetDouble();
bool GetBoolean();
string GetString(int charnumber = -1, bool empty = true);

#endif /* USER_INPUT_HANDLER_H */ 


Does it all look OK? Thanks.
The class declaration for UserInputHandler should be in the .h file, not in the .cpp file.
What do you mean?
That header as you've got it doesn't do anything for you. You've declared and defined all your member functions within the body of the class, in the .cpp file. What you done is to declare a bunch of functions with the same names, but in a different scope. In other words, within your cpp file you have the following names visible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char ::GetSingleCharacter();
char UserInputHandler::GetSingleCharacter();

int ::GetInteger(const int& min, const int& max);
int UserInputHandler::GetInteger(const int& min, const int& max);

double ::GetDouble();
double UserInputHandler::GetDouble();

bool ::GetBoolean();
bool UserInputHandler::GetBoolean();

string ::GetString(int charnumber, bool empty);
string UserInputHandler::GetString(int charnumber, bool empty);


See the difference? I've tried to "properise" it without changing your code. Some general points to note:

The header file should contain your class declaration
The cpp file should contain the class implementation
The whole point of "using std::string" is that afterwards you can just use "string", so you don't have to type "std::string" everywhere.
When you define a boolean variable, it's value can only be true or false, so:
Instead of
1
2
bool loopend = false;
while (loopend == false) {...

prefer
1
2
bool loopend = false;
while (!loopend) {...



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
180
181
182
183
184
185
186
#ifndef USER_INPUT_HANDLER_H
#define USER_INPUT_HANDLER_H

// file : #include "user_input_handler.h"

#include <string>

class UserInputHandler 
{
public:

	char GetSingleCharacter();
	int GetInteger(const int& min, const int& max);
	double GetDouble();
	bool GetBoolean();
	std::string GetString(int charnumber = -1, bool empty = true);
};

#endif /* USER_INPUT_HANDLER_H */ 

// file: user_input_handler.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

char UserInputHandler::GetSingleCharacter() 
{
    string str;
    char letter;
    int i;
    const string invalid_char_input_("Failure: Too many characters.");
    const string char_not_allowed_("Failure: Invalid character.");
    bool loopend = false;
    while (loopend == false) {
      std::getline(cin, str);
      i = (int)str.length();
      if (string::npos == str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
        if (i > 1) {
          cout << invalid_char_input_ << endl;
        } else {
          letter = str[0];
          cout << "Character Entered: " << letter << endl;
          loopend = true;
        }
      } else {
        cout << char_not_allowed_ << endl;
      }
    }
    return letter;
}

int UserInputHandler::GetInteger(const int& min, const int& max) 
{
    const string invalid_int_("Failure: No integer found.");
    const string low_int_("Failure: Integer is below minimum value.");
    const string high_int_("Failure: Integer is above maximum value.");
    int result;
    bool loopend = false;
    while (loopend == false) {
      string line;
      if (getline(cin, line)) {
        stringstream ss(line);
        if (ss >> result && ss.eof()) {
          if (result < min) {
            cout << low_int_ << endl;
          } else if (result > max) {
            cout << high_int_ << endl;
          } else {
            cout << "Integer Entered is: " << result << endl;
            loopend = true;
          }
        } else {
        cout <<invalid_int_ << endl;
        result = 0;
        }
      }
    }
  return result;
}

double UserInputHandler::GetDouble() 
{
    const string invalid_double_("Failure: No double found.");
    double result;
    string line;
    bool loopend = false;
    while (loopend == false) {
      if (getline(cin, line)) {
        stringstream ss(line);
        if (!(ss >> result && ss.eof())) {
          cout <<invalid_double_ << endl;
        } else {
          cout << "Double Entered is: " << result << endl;
          loopend = true;
        }
      }
    }
    return result;
}

bool UserInputHandler::GetBoolean() 
{
    string str;
    bool test;
    string result;
    const string invalid_bool_("Failure: Must input 'T', 'F', 'true', or 'false'. Case sensitive.");
    bool loopend = false;
    while (loopend == false) {
      std::getline(cin, str);
      if (str == "T") {
        test = true;
        result = "true";
        loopend = true;
      } else if ( str == "F") {
        test = false;
        result = "false";
        loopend = true;
      } else if (str == "true") {
        test = true;
        result = "true";
        loopend = true;
      } else if (str == "false") {
        test = false;
        result = "false";
        loopend = true;
      } else {
        cout << invalid_bool_ << endl;
      }
    }
    cout << "Boolean: " << result << endl;
    return test;
  }

string UserInputHandler::GetString(int charnumber, bool empty) 
{
    const string too_long_string_("Failure: Too many characters");
    const string empty_string_("Failure: String is empty.");
    int i;
    bool infinite;
    if (charnumber == -1) {
      infinite = true;
    }
    string str;
    bool loopend = false;
    while (loopend == false) {
      std::getline(cin, str);
      i = (int)str.length();
      if (i == 0) {
        if (empty == false) {
          cout << empty_string_ << endl;
        } else {
          cout << "String Entered: " << str << endl;
          loopend = true;
        }
      } else if (i > charnumber) {
        if (infinite == false) {
          cout << too_long_string_ << endl;
        } else {
          cout << "String Entered: " << str << endl;
          loopend = true;
        }
      } else {
        cout << "String Entered: " << str << endl;
        loopend = true;
      }
    }
    return str;
}

int main() 
{
  UserInputHandler handler;
  handler.GetSingleCharacter();
  handler.GetInteger(0,100);
  handler.GetDouble();
  handler.GetBoolean();
  handler.GetString();
}
Topic archived. No new replies allowed.