Compiler Issues For Assignment

I need help with this assignment. It had some compiler issues on it, some of which I managed to fix, but there are others I don't understand. The code is long, but the majority of it was made by the instructor and doesn't need to be bothered with. There's plenty of notes indicating what I coded and what he coded to test it.

This is the 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Name        : lab_5.cpp
 * Author      : Erik Ingvoldsen
 * Description : Practising Functions
 */

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <streambuf>
using std::cin;
using std::cout;
using std::endl;
using std::string;

/*
 * function name: Hello
 * parameters: none
 * default arguments: n/a
 * return type: void
 *
 * Display "Hello world!" to stdout (no newline character after)
 */
// CODE HERE (FUNCTION PROTOTYPE)
void Hello()
{
  cout << "hello world\n";
}
/*
 * function name: PrintMessage
 * parameters: string message (const call-by-reference)
 * default arguments: none
 * return type: void
 *
 * Display message to stdout (no newline character after)
 */
 // CODE HERE (FUNCTION PROTOTYPE)
 void PrintMessage(string message)
 {
   message="This is a message.";
   std::cout << message;
 }

/*
 * function name: GetAnswer
 * parameters: none
 * default arguments: n/a
 * return type: int
 *
 * Return the value 42
 */
// CODE HERE (FUNCTION PROTOTYPE)
int GetAnswer()
{
  int answer=42;
  return answer;
}

/*
 * function name: FindLarger
 * parameters: int (const call-by-reference), int (const call-by-reference)
 * default arguments: none
 * return type: int
 *
 * Return the larger of the two parameter values. Should work correctly
 * if the values are equivalent.
 */
// CODE HERE (FUNCTION PROTOTYPE)
int FindLarger(int num1, int num2)
{
  int max=0; 
  if (num1 > num2)
  {
    max = num1;
    return max;
  }
  else if (num2 > num1)
  {
    max=num2;
    return max;
  }
  else
  {
    cout << "Numbers are the same size.";
  }
}

/*
 * function name: GetStats
 * parameters: string (const call-by-reference), int (call-by-reference),
 *             int (call-by-reference)
 * default arguments: none
 * return type: int
 *
 * Return the length of string. On return second parameter (int) should contain
 * a count of the number of uppercase characters of first parameter (string),
 * third parameter (int) should contain a count of the number of lowercase
 * characters in the first parameter (string)
 */
// CODE HERE (FUNCTION PROTOTYPE)
int GetStats(string str, int upper, int lower)
{
  int size = str.length();
  for (int i = 0; i < (int)(str.length()); i++)
  {
    upper = str.at() >= 'A' && str.at() <= 'Z';
  }
  for (int i = 0; i < (int)(str.length()); i++)
  {
    lower = str.at() >= 'a' && str.at() <= 'z');
  }
  return size;
  return upper;
  return lower;
}

/*
 * function name: BuildMessage
 * parameters: string (const call-by-reference), bool (const call-by-reference)
 * default arguments: string = "" (empty string), bool = false
 * return type: string
 *
 * Return the string "Message: STRING", where STRING is replaced by the value of
 * the first parameter (string). If second parameter (bool) is true, convert
 * first parameter (string) to all uppercase letters before concatenating it
 * with "Message: ". If first parameter is the empty string, return
 * "Message: empty".
 */
// CODE HERE (FUNCTION PROTOTYPE)
string BuildMessage(string str, bool caps)
{
  std::cout << "Message: " << str;
  if(caps="true")
  {
    for (unsigned int i = 0; i < str.length(); i++)
    str.at(i) = toupper(str.at(i));
  }
  return "";
}

// For testing (DO NOT ALTER)
#include <cctype>
#include <vector>
void UnitTest();
void Test(bool test, int line_number, string more_info = "", string yours = "!",
          string actual = "!");
void OutputFailedTests();
unsigned int ut_passed = 0, ut_failed = 0, ut_total = 0, num_of_tests = 0;
std::vector<int> failed_tests;

// Program Execution Starts Here
int main() {
  // To test your code (DO NOT ALTER)
  UnitTest();
  // This ends program execution
  return 0;
}

// CODE HERE (FUNCTION DEFINITIONS)


// For testing (DO NOT ALTER)
void UnitTest() {
  cout << string(40, '-') << endl;
  cout << "UNIT TEST:\n" << string(40, '-') << endl;
  if (num_of_tests != 0)
    cout << "Total Number of Test: " << num_of_tests << endl;
  string yours = "", actual = "";

  // Tests
  std::streambuf* oldCout = cout.rdbuf();
  std::ostringstream captureCout;
  cout.rdbuf(captureCout.rdbuf());
  Hello();
  cout.rdbuf(oldCout);
  yours = captureCout.str();
  actual = "Hello world!";
  Test(yours == actual, __LINE__, "Hello()", yours, actual);
  captureCout.str("");
  cout.rdbuf(captureCout.rdbuf());
  PrintMessage("Hello again!");
  cout.rdbuf(oldCout);
  yours = captureCout.str();
  actual = "Hello again!";
  Test(yours == actual, __LINE__, "PrintMessage(\"Hello again!\")", yours,
       actual);
  Test(GetAnswer() == 42, __LINE__, "GetAnswer()");
  Test(FindLarger(-1, 1) == 1, __LINE__, "FindLarger(-1, 1)");
  Test(FindLarger(1, -1) == 1, __LINE__, "FindLarger(1, -1)");
  Test(FindLarger(1, 1) == 1, __LINE__, "FindLarger(1, 1)");
  int upper = 0, lower = 0;
  Test(GetStats("abc ABC", upper, lower) == 7 && upper == 3 && lower == 3,
       __LINE__, "GetStats(\"abc 123\", upper, lower)");
  Test(GetStats("abc", upper, lower) == 3 && upper == 0 && lower == 3, __LINE__,
       "GetStats(\"abc\", upper, lower)");
  Test(GetStats("ABC", upper, lower) == 3 && upper == 3 && lower == 0, __LINE__,
       "GetStats(\"ABC\", upper, lower)");
  Test(GetStats("", upper, lower) == 0 && upper == 0 && lower == 0, __LINE__,
       "GetStats(\"\", upper, lower)");
  yours = BuildMessage("hello");
  actual = "Message: hello";
  Test(yours == actual, __LINE__, "BuildMessage(\"hello\")", yours, actual);
  yours = BuildMessage("hello", true);
  actual = "Message: HELLO";
  Test(yours == actual, __LINE__, "BuildMessage(\"hello\", true)", yours,
       actual);
  yours = BuildMessage("HELLO", false);
  actual = "Message: HELLO";
  Test(yours == actual, __LINE__, "BuildMessage(\"HELLO\", false)", yours,
       actual);
  yours = BuildMessage("HELLO", true);
  actual = "Message: HELLO";
  Test(yours == actual, __LINE__, "BuildMessage(\"HELLO\", true)", yours,
       actual);
  yours = BuildMessage();
  actual = "Message: empty";
  Test(yours == actual, __LINE__, "BuildMessage()", yours, actual);

  cout << string(40, '-') << endl;
  cout << "Passed: " << ut_passed << " / " << ut_total << endl;
  OutputFailedTests();
  cout << string(40, '-') << endl;
  cout << "END OF UNIT TEST!\n";
  cout << string(40, '-') << endl;
  cout << "Be sure to run 'make style' to check for any style errors.\n"
       << "Please note that 'make style' does NOT check variable names or"
       << " indentation" << endl << endl;
}

// For testing (DO NOT ALTER)
void Test(bool test, int line_number, string more_info, string yours,
          string actual) {
  ut_total++;
  if (test) {
    cout << "PASSED TEST ";
    ut_passed++;
  } else {
    cout << "FAILED TEST ";
    ut_failed++;
    failed_tests.push_back(ut_total);
  }
  cout << ut_total << " " << more_info << "!" << endl;
  if (!test) {
    if (yours != "!")
      cout << "Yours:  \"" << yours << '"' << endl;
    if (actual != "!")
      cout << "Actual: \"" << actual << '"' << endl;
    cout << "  Check Line " << line_number << " for more info" << endl;
  }
}

void OutputFailedTests() {
  if (failed_tests.size()) {
    cout << "Failed test number(s): ";
    for (unsigned int i = 0; i < failed_tests.size() - 1; i++)
      cout << failed_tests.at(i) << ", ";
    cout << failed_tests.at(failed_tests.size() - 1) << endl;
  }
}


And this is the compile error.

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
$ make
/usr/bin/g++ -Wall -Wextra -pedantic -g lab_5.cpp  -o lab_5
lab_5.cpp: In function ‘int GetStats(std::string, int, int)’:
lab_5.cpp:107:20: error: no matching function for call to ‘std::basic_string<char>::at()’
     upper = str.at() >= 'A' && str.at() <= 'Z';
                    ^
lab_5.cpp:107:20: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from lab_5.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note: std::basic_string<_CharT, _Traits, _Alloc>::const_reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::const_reference = const char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note:   candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note: std::basic_string<_CharT, _Traits, _Alloc>::reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n)
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note:   candidate expects 1 argument, 0 provided
lab_5.cpp:107:39: error: no matching function for call to ‘std::basic_string<char>::at()’
     upper = str.at() >= 'A' && str.at() <= 'Z';
                                       ^
lab_5.cpp:107:39: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from lab_5.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note: std::basic_string<_CharT, _Traits, _Alloc>::const_reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::const_reference = const char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note:   candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note: std::basic_string<_CharT, _Traits, _Alloc>::reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n)
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note:   candidate expects 1 argument, 0 provided
lab_5.cpp:111:20: error: no matching function for call to ‘std::basic_string<char>::at()’
     lower = str.at() >= 'a' && str.at() <= 'z');
 
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
lab_5.cpp:111:20: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from lab_5.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note: std::basic_string<_CharT, _Traits, _Alloc>::const_reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::const_reference = const char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note:   candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note: std::basic_string<_CharT, _Traits, _Alloc>::reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n)
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note:   candidate expects 1 argument, 0 provided
lab_5.cpp:111:39: error: no matching function for call to ‘std::basic_string<char>::at()’
     lower = str.at() >= 'a' && str.at() <= 'z');
                                       ^
lab_5.cpp:111:39: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from lab_5.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note: std::basic_string<_CharT, _Traits, _Alloc>::const_reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::const_reference = const char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:875:7: note:   candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note: std::basic_string<_CharT, _Traits, _Alloc>::reference std::basic_string<_CharT, _Traits, _Alloc>::at(std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       at(size_type __n)
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:897:7: note:   candidate expects 1 argument, 0 provided
lab_5.cpp: In function ‘std::string BuildMessage(std::string, bool)’:
lab_5.cpp:134:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if(caps="true")
                 ^
lab_5.cpp: In function ‘void UnitTest()’:
lab_5.cpp:201:31: error: too few arguments to function ‘std::string BuildMessage(std::string, bool)’
   yours = BuildMessage("hello");
                               ^
lab_5.cpp:131:8: note: declared here
 string BuildMessage(string str, bool caps)
        ^
lab_5.cpp:216:24: error: too few arguments to function ‘std::string BuildMessage(std::string, bool)’
   yours = BuildMessage();
                        ^
lab_5.cpp:131:8: note: declared here
 string BuildMessage(string str, bool caps)
        ^
lab_5.cpp: In function ‘int FindLarger(int, int)’:
lab_5.cpp:87:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
makefile:10: recipe for target 'lab_5' failed
make: *** [lab_5] Error 1


Thanks for your patience. ^^;
error: no matching function for call to ‘std::basic_string<char>::at()’
     upper = str.at() >= 'A' && str.at() <= 'Z';
 note:   candidate expects 1 argument, 0 provided

http://www.cplusplus.com/reference/string/string/at/
Get character in string
Returns a reference to the character at position pos in the string.

you need to specify the index of the character that you want

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if(caps="true")

= is assignment
== is comparison

error: too few arguments to function ‘std::string BuildMessage(std::string, bool)’
   yours = BuildMessage("hello");

error: too few arguments to function ‘std::string BuildMessage(std::string, bool)’
   yours = BuildMessage();

The documentation says that BuilMessage provides defaults arguments.
However, that is not implemented

Change the prototype to string BuildMessage(string str="", bool caps=false);

In function ‘int FindLarger(int, int)’:
lab_5.cpp:87:1: warning: control reaches end of non-void function

¿what are you returning if the numbers are equivalent?
What does you mean by "the index" of the character?
What does you mean by "the index" of the character?

Consider string str = "Hello World"; How do you refer to, say, the 'e' character? The answer is that you refer to it by an index number. In C++ strings, the first character has index 0. The second has index 1, etc. You have to pass the index number to the at() method to tell it which character you're referring to, so in this example, str.at(1) is the 'e' character.
Other issues:

- Each of the professor's comments about the functions ends with // CODE HERE (FUNCTION PROTOTYPE) Then at line 160 it says // CODE HERE (FUNCTION DEFINITIONS) So the prof wants you to put all the prototypes at the top and the definitions down below. For example, up top you want
void Hello();
and down below is your definition:
1
2
3
4
void Hello()
{
  cout << "hello world\n";
}


- Read the prof's descriptions carefully and follow them exactly. For example, the Hello()function says
Display "Hello world!" to stdout (no newline character after)
Your code displays "hello world!\n"
- Hello should be capitalized
- there should not be a newline

Programming is not like most other disciplines. It is all about being very precise.

The next function is PrintMessage(). The description includes
parameters: string message (const call-by-reference)
but your prototype is
void PrintMessage(string message)
- the parameter is not const
- the parameter is not call by reference

You don't need to assign a value to variable to return it. For example, GetAnswer() can just be:
1
2
3
4
int GetAnswer()
{
  return 42;
}

FindLarger() could benefit greatly from this.
Topic archived. No new replies allowed.