Excercise help again

This is a discussion on Excercise help again within the C++ Programming forums, part of the General Programming Boards category; Hi. Here is the question: "Write a program to read strings into a vector. Now, copy that vector into an ...

  1. #1
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Excercise help again

    Hi. Here is the question:

    "Write a program to read strings into a vector. Now, copy that vector into an array of character pointers. For each element in the vector, allocate a new character array and copy the data from the vector element into that character array. Then insert a pointer to the character array into the array of character pointers."

    Okay, the first part is easy but when it comes to the character array part I get very confused. I'd say I understand pointers but I don't seem to get this question. It says to insert a pointer to the new character array I create for each string element in the vector, but how can I insert a pointer into the original character array? I'm not asking for the full solution, I just need a little help by getting directed in the right path.

    How can I make an array of character pointers...? So if I do manage to make this array, if I do:

    Code:
    std::cout << Array[0] << std::endl;
    It will output the array that was created for each string element? Help, please!

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Code:
    char* charArrray[]
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    This is what I got:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
    	//Vector to store inital strings
    	std::vector<std::string> vStrings;
    	int strCount = 0;
    
    	std::cout << "How many strings?: ";
    	std::cin >> strCount;
    
    	//Lets insert each string into the vector
    	for(int i = 0; i < strCount; i++)
    	{
    		std::string Input;
    		std::cout << i << " = ";
    		std::cin >> Input;
    
    		vStrings.push_back(Input);
    	}
    	//This array points to pointers (the string pointers)
    	const char **pointerArray = new const char *[];
    
    	for(int i = 0; i < vStrings.size(); i++)
    	{
    		const char *strElement = vStrings[i].c_str();
    		pointerArray[i] = strElement;
    	}
    	//Vector
    	std::cout << "Vector: " << std::endl;
    
    	for(int i = 0; i < vStrings.size(); i++)
    	{
    		std::cout << "vStrings[" << i << "] = " << vStrings[i] << std::endl;
    	}
    	//Output the C-style strings
    	std::cout << "Array: " << std::endl;
    	for(int i = 0; i < vStrings.size(); i++)
    	{
    		std::cout << "pointerArray[" << i << "] = " << pointerArray[i] << std::endl;
    	}
    }
    It works great. I had to do const char **array = new char *[] to store the pointers in the array. Have I interpreted the question wrong? If not, is the code good enough and am I on the right track here?

    Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    It works great. I had to do const char **array = new char *[] to store the pointers in the array. Have I interpreted the question wrong? If not, is the code good enough and am I on the right track here?
    I think you are on the right track, but should declare array as a char** not a const char**.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    If I do that then the compiler complains it cannot convert const char* to char*.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Oh, now I see the problem. Your instructions are to "allocate a new character array and copy the data from the vector element into that character array", but you are just assigning pointers instead of copying the strings themselves.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Yeah. But when I output the array how will I know how long the string is?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Yeah. But when I output the array how will I know how long the string is?
    There will be a null terminator since you are in the realm of C style strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Quote Originally Posted by laserlight View Post
    There will be a null terminator since you are in the realm of C style strings.
    Oh yes, I forgot about that. Thanks, will rewrite it now.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Beware that you aren't freeing the memory. You might want to add that once you get it working.
    In the future, though, you may want to use std::vector instead of manually allocated char arrays.
    But that's for the future, not for this exercise
    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.

  11. #11
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Quote Originally Posted by laserlight View Post
    Oh, now I see the problem. Your instructions are to "allocate a new character array and copy the data from the vector element into that character array", but you are just assigning pointers instead of copying the strings themselves.
    Wait, isn't that part just meaning to copy the string element into a new character array as I've done:

    Code:
    const char *strElement = vStrings[i].c_str();
    I've allocated an array and copied the element into that as I've shown above. Now the last part:

    "Then insert a pointer to the character array into the array of character pointers."

    It tells me to then create a pointer to that array and insert it into the original array I created at the beginning:

    "Now, copy that vector into an array of character pointers"

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    I've allocated an array and copied the element into that as I've shown above.
    You created a dynamically allocated array of pointers, then you assigned the pointers returned by std::string::c_str() to the respective elements of this array. What you are supposed to do is to create a dynamically allocated array of pointers, then have each pointer point to a dynamically allocated array of characters, and then copy over the characters from the std::string over to this new array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Why is it when I try to free strElement as:

    Code:
    delete [] strElement;
    It crashes on the next part of the loop?

    I have:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
    	//Vector to store inital strings
    	std::vector<std::string> vStrings;
    	int strCount = 0;
    
    	std::cout << "How many strings?: ";
    	std::cin >> strCount;
    
    	//Lets insert each string into the vector
    	for(int i = 0; i < strCount; i++)
    	{
    		std::string Input;
    		std::cout << i << " = ";
    		std::cin >> Input;
    
    		vStrings.push_back(Input);
    	}
    	char *aStrings = new char[];
    	unsigned int pos = 0;
    
    	for(unsigned int i = 0; i < vStrings.size(); i++)
    	{
    		const char *strElement = vStrings[i].c_str();
    
    		for(unsigned int j = 0; j <= vStrings[i].size(); j++)
    		{
    			std::cout << strElement[j] << std::endl;
    			aStrings[pos] = strElement[j];
    			std::cout << "array: " << aStrings[pos] << std::endl;
    			pos++;
    		}
    		delete [] strElement;
    	}
    	//Vector
    	std::cout << "Vector: " << std::endl;
    
    	for(int i = 0; i < vStrings.size(); i++)
    	{
    		std::cout << i << " = " << vStrings[i] << std::endl;
    	}
    
    	//Array
    	std::cout << "Array: " << std::endl;
    	unsigned int pos2 = 0;
    
    	for(int i = 0; i < pos - 1; i++)
    	{
    		if((aStrings[i] == '\0') || (i == 0))
    		{
    			//New line
    			std::cout << "\n" << pos2 << " = ";
    		}
    		std::cout << aStrings[i];
    	}
    	std::cout << std::endl;
    	return 0;
    }
    Nearly works, but just crashes (if I remove delete [] strElement) it still crashes at the end.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Why is it when I try to free strElement as:
    strElement is a copy of the pointer returned by c_str(), and you do not own what is pointed to by strElement, so using delete[] on it is incorrect.

    Also, this is wrong:
    Code:
    char *aStrings = new char[];
    You should provide a size for the dynamically allocated array. It should be a pointer to pointer, from what I see of the earlier discussion in this thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Right, I rewrote it and it works great (apart from the memory management):

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main()
    {
    	//Vector to store inital strings
    	std::vector<std::string> vStrings;
    	unsigned int stringCount = 0;
    
    	std::cout << "How many strings will you like to input?: ";
    	std::cin >> stringCount;
    
    	//Lets insert the strings
    	for(unsigned int i = 0; i < stringCount; i++)
    	{
    		//Temporary storage for string
    		std::string tempInput;
    
    		std::cout << i << " = ";
    		std::cin >> tempInput;
    
    		//Insert the temp string into the vector
    		vStrings.push_back(tempInput);
    	}
    	//The amount of characters in total in the vector (includes null terminators)
    	unsigned int charCount = 0;
    
    	for(unsigned int i = 0; i < vStrings.size(); i++)
    	{
    		for(unsigned int j = 0; j <= vStrings[i].size(); j++) //<= for the null terminators too
    		{
    			charCount++;
    		}
    	}
    	//Allocate the needed storage for the array
    	char *aStrings = new char[charCount];
    	unsigned int newPos = 0;
    
    	//Now lets insert each string in the vector into the array
    	for(unsigned int i = 0; i < vStrings.size(); i++)
    	{
    		//Create a new array to store the string
    		const char *stringElement = new const char[vStrings[i].size()];
    		stringElement = vStrings[i].c_str();
    
    		//Scroll through each character and insert it into the array (includes null terminators)
    		for(unsigned int j = 0; j <= vStrings[i].size(); j++)
    		{
    			aStrings[newPos] = stringElement[j];
    			newPos++;
    		}
    		//Free the allocated memory for stringElement
    		//delete [] stringElement;
    	}
    	//Lets check if all was successful by outputting the values in both vector and array
    
    	//Vector
    	std::cout << "Vector: " << std::endl;
    
    	for(unsigned int i = 0; i < vStrings.size(); i++)
    	{
    		std::cout << i << " = " << vStrings[i] << std::endl;
    	}
    
    	//Array
    	std::cout << "Array: " << std::endl;
    	unsigned int Count = 0;
    
    	for(unsigned int i = 0; i < (charCount - 1); i++)
    	{
    		if(i == 0)
    		{
    			std::cout << Count << " = ";
    			Count++;
    		}
    		else if(aStrings[i] == '\0')
    		{
    			//Null terminator
    			std::cout << "\n" << Count << " = ";
    			Count++;
    			
    			//We want to skip the null terminator so we don't output it
    			i++;
    		}
    		std::cout << aStrings[i];
    	}
    	std::cout << std::endl;
    }
    So how could I free the memory used because in the next excercise it asks me to edit this code to make it output both the vector and array (done) and also asks me to delete the character arrays (which I assume is the stringElements created in the for loop).
    Last edited by gin; 07-05-2008 at 10:24 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 2
    Last Post: 10-16-2008, 10:23 PM
  2. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 8
    Last Post: 10-15-2008, 08:10 PM
  3. excercise
    By luigi40 in forum C# Programming
    Replies: 9
    Last Post: 11-22-2005, 03:25 AM
  4. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21