Thread: Compiler Issues For Assignment

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    225

    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:

    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

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh God, my eyes.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Start with this:
    Code:
    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';
    What it means is that the way you called the at member function is wrong. In particular, the at function expects an argument to be passed to it.

    Next, look at this:
    Code:
    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")
                     ^
    This is not a compile error, but a warning. The warning turns out to be a good one here because it hints at a logic error: you are assigning "true" to caps when you probably want to compare for equality. Furthermore, caps is a bool, so you actually want to compare with true, not "true". But it would be simpler to just write:
    Code:
    if (caps)
    As for the error messages stating that there are "too few arguments" to a function: that should be self-evident.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I will note that you haven't implemented many functions according to the requirements, either. Many of them say call-by-reference, by which I presume the instructor means that the parameters shall be passed by reference. Some functions require you to return data, so references are important.
    Then there are some functions which are just plain wrong, like FindLarger. If both numbers are equally large, then you're printing something to screen and returning nothing, which will result in undefined behavior. Clearly it says it should work if both are equal, so you must return something.

    I will also note that

    int answer=42;
    return answer;

    Can be simplified to

    return 42;

    You don't need to return a variable. You can return an expression directly. You can use this elsewhere too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array assignment issues.
    By workisnotfun in forum C Programming
    Replies: 2
    Last Post: 11-24-2012, 03:54 PM
  2. Replies: 5
    Last Post: 04-23-2012, 07:54 AM
  3. Having issues with my compiler..
    By SirFloofy in forum C++ Programming
    Replies: 11
    Last Post: 02-26-2012, 02:10 PM
  4. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 12:55 PM
  5. Compiler Issues
    By marliwht in forum C Programming
    Replies: 6
    Last Post: 10-21-2005, 12:57 PM