GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Apr-2004, 15:26
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road

Help with C++ pointers


Hey guys, who wants to do my homework for me? Just kidding. But Im working on another assignment for school that uses pointers. I have to create a string tokenizer class that accepts a string durring object creation and creates a pointer to the string. The class is also supposed to return the pointer to each token I think. You know, I don't even fully understand what is being asked of me. If it will help, you can read the assignment here:
debryro.uvsc.edu

I don't even understand pointers that well. They are just memory addresses of variables and arrays right?

Anyway, Im having a hard time getting started on this. Im getting these errors when trying to comile the class implementation (stringTokenizer.cpp):

Quote:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\UVSC\CNS1250\projects\stringTokenizer\tokenizer .cpp:
Error E2034 C:\UVSC\CNS1250\projects\stringTokenizer\tokenizer .cpp 9: Cannot convert 'char * *' to 'char *' in function stringTokenizer::stringTokenizer(char *)
Warning W8057 C:\UVSC\CNS1250\projects\stringTokenizer\tokenizer .cpp 11: Parameter 'theString' is never used in function stringTokenizer::stringTokenizer(char *)
*** 1 errors in Compile ***

Tool completed with exit code 1

Any insight would be great. Here is my code so far.......

CPP / C++ / C Code:
//--------------------main(driver.cpp)-------------------------------------
#include <iostream>
using namespace std;

#include "tokenizer.cpp";

int main()
{
	bool loopFlag = false;

             const int MAX = 100;
	char* theString[MAX];

	cout << "Enter a string of characters " << endl;
	cin.get(theString, MAX);

	cout << theString;

	stringTokenizer st(theString);
    
       return 0;

}
//------------------class implementation(stringTokenizer.cpp)--------------
#include "tokenizer.h";

#include <iostream>
using namespace::std;

stringTokenizer::stringTokenizer(char theString[])
{

	thePointer = &theString;
	delimiter = NULL;
}
void stringTokenizer::next()
{
}
//-----------------class header(stringTokenizer.h)-------------------------
#ifndef studentInfoH
#define studentInfoH

#include <iostream>

class stringTokenizer
{
   private:

   	char* thePointer;
   	char delimiter;
   public:
      // Constructor
      // Purpose: Initializes a stringTokenizer object
      // Parameters: string (character array)
      // Returns: none
      // Pre-conditions: none
      // Post-conditions: none
      stringTokenizer (char []);

      // Function: next
      // Purpose: returns pointer to next token in string
      // Returns: pointer to next token in string
      // Pre-conditions: none
      // Post-conditions: none
      void next( );

 };
 #endif

In the meantime im going to read about pointers
Last edited by Mjkramer21 : 15-Apr-2004 at 15:45. Reason: used [c] tags instead of [c++]. Don't know if it makes a difference
  #2  
Old 15-Apr-2004, 16:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello MJ. One thing should let your program compile:

CPP / C++ / C Code:
stringTokenizer::stringTokenizer(char theString[])
{

	thePointer = theString;
	delimiter = NULL;
}

An array is already basically a pointer, so assignment does not need an ampersand. When you give it an ampersand, you are saying the * to the *, thus your error. This also would work:

CPP / C++ / C Code:
thePointer = &theString[0];

HTH
  #3  
Old 15-Apr-2004, 18:18
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road
Can I actually place a delimiter into the array where I want using the pointer that I created in the class?

Something like this:

CPP / C++ / C Code:
for(int x = 0; x < arraySize; x++)
{
      if(pointer[x] == " ")
      {
         pointer[x] = delimiter;
       }
}

I tried it like this:

CPP / C++ / C Code:
for(int x = 0; x < arraySize; x++)
{
      if(*(pointer + x) == " ")
      { 
         *(pointer + x) = delimiter;
      }
}

And guess what. Yea, didn't work.
  #4  
Old 15-Apr-2004, 18:32
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
jus change those double quotes to single quotes. that might work.


P.S. i think what your instructor wants you to do is that when you encounter a space, he wants you to put the rest of the string into a new string each time. the space itself is your delimiter.
  #5  
Old 15-Apr-2004, 18:41
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Mjkramer21
Can I actually place a delimiter into the array where I want using the pointer that I created in the class?

Something like this:

CPP / C++ / C Code:
for(int x = 0; x < arraySize; x++)
{
      if(pointer[x] == " ")
      {
         pointer[x] = delimiter;
       }
}

I tried it like this:

CPP / C++ / C Code:
for(int x = 0; x < arraySize; x++)
{
      if(*(pointer + x) == " ")
      { 
         *(pointer + x) = delimiter;
      }
}

And guess what. Yea, didn't work.

" " defines a string constant. You want to use ' '. (There is a space between each of those.)

Is delimiter of type char?
  #6  
Old 15-Apr-2004, 18:53
Mjkramer21's Avatar
Mjkramer21 Mjkramer21 is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Orem, Ut
Posts: 36
Mjkramer21 is on a distinguished road
Turning those quotes into single quote worked. How about that. I knew better . Thanks.

This assignment sucks! I have no idea how to do this. For one thing, I don't understand why I'm placing a space delimiter into an array that already has spaces as natural delimiters. I could understand if I was placing nulls into the places where there are spaces. I'm so close to getting this wrapped up. But I don't know what my next step is because I don't know what my instructor wants me to do exactly. The instructions say to pass a space delimiter, but thats gotta be wrong. With those instructions, as you can see in my code, im turning a ' ' into ' '. what is the point? Its gotta be a typo. He must want me to turn ' ' into '\0' or NULL.

Is the whole point to find where there is a space in the input string, and then simply return the string all the way up until that space, at which time you would increment some temp pointer position in main and pass it again to read the next string until it finds a space? I hope that made sense. Here is my revised code if anyone wants to look over it and see if they can determine what Im trying to do or what my next step SHOULD be.

CPP / C++ / C Code:
//------------------main(drive.cpp)----------------
#include <iostream>
using namespace std;

#include "tokenizer.cpp";

int funcSize(char[]);

int main()
{
	bool loopFlag = false;
	char* thePointer;
	int arraySize;
	char delimit = ' ';


	const int MAX = 100;
	char theString[MAX];

		cout << "Enter a string of characters " << endl;
		cin.get(theString, MAX);

		cout << theString;


	arraySize = funcSize(theString);

	stringTokenizer sP(theString, arraySize, delimit);

	thePointer = sP.next();

	cout << "\nThe pointer is: " << thePointer << endl;
}

//************function funcSize*************************

int funcSize(char theString[])
{
	int size = 0;

	while(theString[size] != NULL)
		{

			cout << size << " " << theString[size] << endl;

			size++;
		}

	return (size + 1);
}
//---------------class implementation(stringTokenizer.cpp--------------
#include "tokenizer.h";

#include <iostream>
using namespace::std;

stringTokenizer::stringTokenizer(char theString[], int AS, char delimit)
{

	thePointer = theString;
	delimiter = delimit;
	arraySize = AS;
}
char* stringTokenizer::next()
{
	int temp;

	for(int x = 0; x < arraySize; x++)
	{
	      if(*(thePointer + x) == ' ')
	      {
	         *(thePointer + x) = delimiter;
	      }
	}

	return thePointer;
}
//----------------class header(stringTokenizer.h)-------------------
#ifndef studentInfoH
#define studentInfoH

#include <iostream>

class stringTokenizer
{
   private:

   	char* thePointer;
   	char delimiter;
   	int arraySize;

	public:
      // Constructor
      // Purpose: Initializes a stringTokenizer object
      // Parameters: string (character array)
      // Returns: none
      // Pre-conditions: none
      // Post-conditions: none
      stringTokenizer (char [], int, char);

      // Function: next
      // Purpose: returns pointer to next token in string
      // Returns: pointer to next token in string
      // Pre-conditions: none
      // Post-conditions: none
      char* next( );

};
 #endif
  #7  
Old 15-Apr-2004, 19:12
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
yes that was precisely my point. your space itself is the delimiter, i think you need to place a null in place of a space and then assign a pointer to the next char element and repeat. that's the most efficient way.

the otherway is to assign new memory and use strcpy or lstrcpy functions.
  #8  
Old 15-Apr-2004, 19:14
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Mjkramer21
Is the whole point to find where there is a space in the input string, and then simply return the string all the way up until that space, at which time you would increment some temp pointer position in main and pass it again to read

I just read through your assignment instructions. Your above sentence is basically correct. This is very similar to the strtok function if you are familiar with that.

Do you want to modify the original string? (I would think not).

If not, I would psuedo code this something like:
  • If your track pointer is at null, newline or return char - return Null.
  • Else set a parsing pointer at the track pointer position
  • index track pointer until it finds the delimiter (or null or newline or return char)
  • save this character to a temp char.
  • plug the track pointer with NULL.
  • Using your favorite replacement for malloc and strcpy, make a new char * and copy the characters into it from your track pointer to your parse pointer (this can easily be done with strcpy...)
  • Place the temp char back into the string at track pointer position.
  • if temp char is not equal to null or newline or return, index it to the first character of the next word.

I don't know if that helps or not, but sometimes it helps to break it down a bit.

Good luck!
Last edited by dsmith : 16-Apr-2004 at 08:17.
  #9  
Old 15-Apr-2004, 19:28
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
save which character to a temp char? and what do you mean by "lug" the track pointer with NULL?
  #10  
Old 15-Apr-2004, 23:01
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Here's what I see:
The constructor creates a char buffer and copies the parameter string into it.
It creates two pointers and loads the beginning of the string into them. One pointer tokrtn will be the address of the token to be returned, the other the pointer to the next token toknxt .
The delimiter is placed in the class delimiter character.

The next method will
  • load toknxt into tokrtn
  • test toknxt character for either SPACE or '\0'
  • -- if neither, increment toknxt and test again
  • if SPACE, change it to NULL, increment once more
  • if '\0', load toknxt with NULL
  • return tokrtn

This will return NULL pointer when you've reached the end of the string.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
string conversion + pointers Rolapin CPP / C++ Forum 0 02-Apr-2004 07:36
Using character pointers to store the person's name internethesabi C Programming Language 9 12-Mar-2004 15:44
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 21:23
c: array comparison jack C Programming Language 7 26-Jan-2004 11:21
pointers and arrays jack C Programming Language 4 15-Jan-2004 12:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.