Thread: Need Help On an Assignment

  1. #91
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An element cannot be NULL. Something either exists or it does not. For pointers, there is a special type of pointer called null pointer, which means that it points to nothing. But you don't have pointers here.
    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.

  2. #92
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Well...that's what my constructors ask for...

    Quote Originally Posted by Erik Ingvoldsen View Post
    Default Constructor:
    Initializes the array to 10 elements and initializes the elements to NULL.

    Overloaded Constructor:
    Initializes the array via an unsigned integer parameter. If the parameter's valueis not valid (i.e. 0) set the array to a capacity of 10. Initializes all elements to NULL.

  3. #93
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should probably clarify with your instructor. Chances are, it is a typo error, i.e., your instructor probably meant "empty string" or equivalently in C++, "string of zero length".
    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. #94
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    OK, talked to instructor today and he helped me fix the test:

    Code:
    int main() {
      std::string * myarray[10];
      int i = 0;
      std::string str1 = "B";
      std::string str2 = "C";
      std::string str3 = "D";
      std::string str4 = "E";
      std::string str5 = "A";
      while (i < 10) {
        myarray[i] = NULL;
        ++i;
      }
      int currentsize = 4;
      myarray[0] = &str1;
      myarray[1] = &str2;
      myarray[2] = &str3;
      myarray[3] = &str4;
      cout << "Array List 1:" << endl;
      for (i = 0; i < 10; i++) {
        if (myarray[i] != NULL) {
          cout << *myarray[i] << "\n";
        }
      }
      for (int i=0; i<(currentsize - 1); i++) {
        myarray[i] = myarray[i + 1];
      }
      cout << "Array List 2:" << endl;
      for (i = 0; i < 10; i++) {
        if (myarray[i] != NULL) {
          cout << *myarray[i] << "\n";
        }
      }
      myarray[0] = &str5;
      cout << "Array List 3:" << endl;
      for (i = 0; i < 10; i++) {
        if (myarray[i] != NULL) {
          cout << *myarray[i] << "\n";
        }
      }
    }
    The output is this:

    $ ./test
    Array List 1:
    B
    C
    D
    E
    Array List 2:
    C
    D
    E
    E
    Array List 3:
    A
    D
    E
    E

    I've tried fiddling with the code a bit, but this would always lead to new errors, such as the amount of elements getting lower or the core dumping and, for the life of me, I can't figure out how to shift it to the right.

  5. #95
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about you start with a simpler example of not using pointers and ensuring you're not running off the end of the array?
    Hint: Use std::vector and .at() to catch programming errors. Switch to a C-array when everything is working.
    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.

  6. #96
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Oh wait, I finally figured it out. Got the function working!

    I'm actually almost done (until the instructor gives us the test file). I just need a bit of help with ToString. It requires the assistance of the friend operator (first time I've heard of a friend operator, let alone built one), and I'm not sure how to set it so the elements will be coma seperated...or how to use the friend operator in ToString.

  7. #97
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you not providing some way to access an element by index? If you are, then you do not need to declare ToString as a friend. If you are not, then perhaps ToString should be a member function instead: you can always write a non-member wrapper function of the same name if it is desirable.
    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

  8. #98
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    No, no, I'm not trying to make ToString a friend. I am referring to this:

    Quote Originally Posted by Erik Ingvoldsen View Post
    Member Function 13:
    Named ToString. Returns a string containing all of the strings in
    the array, comma separated.

    Overloaded Friend operator<<:

    Outputs a comma separated list of all of the strings in the array.
    It would appear these two go hand in hand, but I'm not sure how to make this friend operator or use it.

  9. #99
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you could end up with something like this:
    Code:
    ostream& operator<< (ostream& os, const DynamicStringArray& arr) 
    {
        // implementation ... the hard part
    }
    
    string DynamicStringArray::ToString() const
    {
        ostringstream result;
        result << *this;
        return result.str();
    }
    To me at least, you end up with the same thing no matter where the hard part is.

    What laserlight was saying still matters though. You need to implement a way to access all of the elements of the array. When you implement that, then you can focus on outputting a "comma separated list" with, say, a loop.
    Last edited by whiteflags; 03-31-2015 at 04:08 PM.

  10. #100
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Really quick, what's "*this" exactly?

  11. #101
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Erik Ingvoldsen View Post
    Really quick, what's "*this" exactly?
    this pointer - cppreference.com
    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.

  12. #102
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    I think I have an idea how to make this work...

    Code:
    for (i = 0; i < currentsize - 1; i++) {
      str = myarray[i];
      str = str + ",";
      myarray[i] = &str;
    }
    This should be able to access all the non null elements in the ray. And I believe subtracting 1 from "currentsize" in this for statement will remove the comma from the last element, giving us

    A, B, C

    instead of

    A, B, C,

    The problem, from my understanding, is this line:

    Code:
    &str = myarray[i];
    When I tried compiling it, it wasn't allowed. Is there a way to set the string up so it will be the same as an array element? This is my full test code:

    Code:
    int main() {
      std::string * myarray[10];
      int i = 0;
      std::string str;
      std::string str1 = "A";
      std::string str2 = "B";
      std::string str3 = "C";
      std::string str4 = "D";
      while (i < 10) {
        myarray[i] = NULL;
        ++i;
      }
      int currentsize = 4;
      myarray[0] = &str1;
      myarray[1] = &str2;
      myarray[2] = &str3;
      myarray[3] = &str4;
      cout << "Array List 1:" << endl;
      for (i = 0; i < 10; i++) {
        if (myarray[i] != NULL) {
          cout << *myarray[i];
        }
      }
      for (i = 0; i < currentsize - 1; i++) {
        str = myarray[i];
        str = str + ",";
        myarray[i] = &str;
      }
      cout << "Array List 2:" << endl;
      for (i = 0; i < 10; i++) {
        if (myarray[i] != NULL) {
          cout << *myarray[i];
        }
      }
    }
    And this is the compile error:

    Code:
    $ make
    /usr/bin/g++ -Wall -Wextra -g test.cpp  -o test
    test.cpp: In function ‘int main()’:
    test.cpp:36:20: error: invalid conversion from ‘std::string* {aka std::basic_string<char>*}’ to ‘char’ [-fpermissive]
         str = myarray[i];
                        ^
    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 test.cpp:1:
    /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:573:7: note: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
           operator=(_CharT __c)
           ^
    makefile:10: recipe for target 'test' failed
    make: *** [test] Error 1
    Last edited by Erik Ingvoldsen; 04-01-2015 at 01:17 AM.

  13. #103
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider a program like this:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    int main()
    {
        using namespace std;
    
        vector<string> words{"these", "are", "a", "few", "words"};
        if (!words.empty())
        {
            stringstream ss;
            ss << words[0];
            for (vector<string>::size_type i = 1; i < words.size(); ++i)
            {
                ss << "," << words[i];
            }
            cout << ss.str() << endl;
        }
    }
    The idea is that given the container of strings, assuming the container is not empty, you can start off a stringstream with the first string. Then, loop over the container and keep appending a comma followed by the current string. At the end, you can call the str() member function to get the resulting comma-separated value string.

    I leave modifying this to ignore empty strings as an exercise. You probably need to be most careful to handle the case where the very first string is empty.
    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

  14. #104
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>test.cpp:36:20: error: invalid conversion from ‘std::string* {aka std::basic_string<char>*}’ to ‘char’ [-fpermissive]
    What does this compile error tell you? And what do you think it means? Can you spot an error in your code?
    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.

  15. #105
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Quote Originally Posted by Elysia View Post
    What does this compile error tell you? And what do you think it means? Can you spot an error in your code?
    Well, last time I had that issue, it was solved by doing this:

    Code:
    myarray[i] = &str;
    But when I do that here

    Code:
    &str = myarray[i]
    I get this:

    Code:
    $ make
    /usr/bin/g++ -Wall -Wextra -g test.cpp  -o test
    test.cpp: In function ‘int main()’:
    test.cpp:36:10: error: lvalue required as left operand of assignment
         &str = myarray[i];
              ^
    makefile:10: recipe for target 'test' failed
    make: *** [test] Error 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need some help with an assignment..please.
    By ronaldh12 in forum C Programming
    Replies: 3
    Last Post: 09-30-2011, 09:33 AM
  2. Help!! Assignment!
    By Joeshua Lee in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2011, 09:30 AM
  3. Help!! Assignment!
    By Joeshua Lee in forum C Programming
    Replies: 3
    Last Post: 08-09-2011, 09:30 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. Assignment Help...maybe..
    By TerribleatC in forum C Programming
    Replies: 5
    Last Post: 01-05-2007, 08:05 AM