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:
Code:
/*
* 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.
Code:
$ 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');
^
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