GIDForums  

Go Back   GIDForums > Computer Programming Forums > 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 30-Nov-2004, 22:08
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road

Using pointers and strings


I am currently writing a program that will detect both word and phrase palindromes. I have the part that is supposed to detect the palindromes written but it is not working correctly. I can only use strings in the program, no arrays or subscripts.
here is what I currently have:
I am getting an error that my pointer is being used without being defined..
I am wanting to set the "leftPtr" to start at the begining of the string and the "rightPtr" to start at the end of the string and then they are compared to see if matches are found.

CPP / C++ / C Code:
int main()
{
	bool isPal = true;
	string userInput;
	string *leftPtr, *rightPtr;

	cout << "Please enter a sentence: ";
	getline(cin, userInput);
	userInput.length();

	*leftPtr = userInput.length();
	*rightPtr = userInput.length() - 1;

	while (leftPtr <= rightPtr) 
	{
		if (*leftPtr != *rightPtr)
		{
			isPal = false;
		}
		++leftPtr;
		--rightPtr;
	}

	if(isPal == true)
		cout << "\nPalindrome found!!";
	else
		cout << "\nNot a palindrome!!";
	
	return 0;
}

Anyone have any suggestions? thanks
  #2  
Old 30-Nov-2004, 23:08
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
Can you post?:

1- The entire source code for main, i.e. include directives, etc..

2- The acutal error messages from your compiler.

3- What compiler you are using.

I'm still pretty new to C/C++ so this information would help me to help you. For example, I've copied your code into MS VC++ 6.0 and getline is not recognized, so I imagine I'm not using the same headers as your are.

Good luck.
  #3  
Old 01-Dec-2004, 00:15
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
Here are my directives:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

My compiler is Microsoft Visual Studio .Net 2003
The error message is "rightPtr and leftPtr are being used without being defined"

Thanks
  #4  
Old 01-Dec-2004, 02:01
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
Quote:
Originally Posted by dilmv
I am getting an error that my pointer is being used without being defined..
It's hard to tell when the are two values that can be defined as "my pointer". Please give enough information to understand the problem.

CPP / C++ / C Code:
cout << "Please enter a sentence: ";
getline(cin, userInput);
userInput.length();    // what does this do?  This is a "nop" (non-operation) 
                       // because the 'value' is not assigned anywhere

*leftPtr = userInput.length();  // what is the value of userInput.length()?
*rightPtr = userInput.length() - 1;

Hope these suggestions help.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 01-Dec-2004, 06:02
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
Okay, here is my take. A pointer variable needs to be assigned a memory address before it can be dereferenced properly. Right now, you are using the * operator on pointers that haven't been assigned memory addresses.

BTW, I believe pointers that don't have memory addresses are called wild pointers.

HTH.
  #6  
Old 01-Dec-2004, 10:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dilmv
I am currently writing a program that will detect both word and phrase palindromes. I have the part that is supposed to detect the palindromes written but it is not working correctly. I can only use strings in the program, no arrays or subscripts.


Anyone have any suggestions? thanks

Your loop is perfect for c-style strings (and, can easily be modified to perform the same indexing with pointers to char), but the instructor obviously wants you to become familiar with C++ strings and functions.

Here is an example that uses lots of member functions of the string class:

http://www.yolinux.com/TUTORIALS/Lin...ringClass.html

Suppose you have two int variables, say leftNdx and rightNdx. The variable leftNdx starts at the first char in the string and increments after every comparison. The variable rightNdx starts at the last char in the string, and decrements. You want to compare the char pointed to by leftNdx and rightNdx.

So you could have something like
CPP / C++ / C Code:
    if (userInput.substr(leftNdx, 1) != userInput.substr(rightNdx, 1))
    {
      isPal = false;
      break;
    }
    leftNdx++;
    rightNdx--;
  }

See? You are comparing substrings with length 1 at each step. (So the comparisons are one character at a time.)

Now, how do you set the value of leftNdx so that the first comparison will look at the first element of the array? This should do it:

CPP / C++ / C Code:
 leftNdx = 0;

How do you set the value of rightNdx so that its use at the first comparison will be the last element of the array? Well, it's initial value will be one less than the length of the string, where you use the member function userInput.length() to tell you what the string length is.

Regards,

Dave
  #7  
Old 01-Dec-2004, 12:13
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
thanks for the input everyone. I guess the main problem I have is that I am trying to assign one of my pointers to the begining of the string and one to the end of the string. Once I get that part solved and rest is fairly straight forward.

right here:
CPP / C++ / C Code:
leftPtr = userInput.length();
rightPtr = userInput.length() - 1;

I am trying to assign the leftPtr to point to the first char in the string and the right pointer to point to the last char in the string. My compiler gives the following error:
"error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>::size_type' to 'std::string *'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
"
  #8  
Old 01-Dec-2004, 13:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dilmv
thanks for the input everyone. I guess the main problem I have is that I am trying to assign one of my pointers to the begining of the string and one to the end of the string. Once I get that part solved and rest is fairly straight forward.

right here:
CPP / C++ / C Code:
leftPtr = userInput.length();
rightPtr = userInput.length() - 1;

I am trying to assign the leftPtr to point to the first char in the string and the right pointer to point to the last char in the string. My compiler gives the following error:
"error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>::size_type' to 'std::string *'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
"

You declare leftPtr and rightPtr to be of type "pointer to string"

The function userInput.length() returns an int (the length of the string). The error message tells you that you can't convert the result of length() to a "pointer to a string".

Now, it just happens that if you declare leftPtr and rightPtr to be "pointer to char", then you can do something like this:

CPP / C++ / C Code:
leftPtr = &userInput[0];
rightPtr = &userInput[userInput.length()-1];

(It doesn't really "just happen"; that's the way that the string class is defined to work.)

Then compare *leftPtr with *rightPtr. increment leftPtr, decrement rightPtr, etc.


In any case, you will be comparing characters one at a time, starting at opposite ends of the string.
Regards,

Dave
  #9  
Old 01-Dec-2004, 14:16
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
I really believe the problem is that you are assigning the length of the user inputted string to the pointer. That's not a memory address. And consequently, the pointer arithmetic does not work.

Edit: Dave is alluding to the same problem.
  #10  
Old 02-Dec-2004, 00:24
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
ok I see what I was doing wrong. I kinda forgot what a pointer was all about. I have that squared away now and my program detects single word strings that are palindromes. I am having trouble making the program recognize palindromes within a sentence. For example, when a string is entered that may contain something like "The radar gun" "radar" is a palindrome but the rest of the sentence is not. I know that in order to do this I will have to detect words and do each word one by one to determine if it is a palindrome. I know that there is a function called isspace() that will tell me if there is a space or not...but I'm not quite sure how to go about implementing it into the program... any suggestions? My program also needs to detect phrase palindromes like "A man, a plan, a canal - Panama".. so I will probably need more than one function to process each type of palindrome right?

CPP / C++ / C Code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;


//function Prototype
bool IsPalin(string);

int main()
{

	string userInput;

	cout << "Please enter a sentence: ";
	getline(cin, userInput);
	
	if(IsPalin(userInput) == true)
		cout << "\nPalindrome found!!";
	else
		cout << "\nNot a palindrome!!";
	return 0;
}

bool IsPalin(string userInput)
{
	char *leftPtr, *rightPtr;
	bool isPal = true;

	leftPtr = &userInput[0];
	rightPtr = &userInput[userInput.length()-1];

	while (leftPtr <= rightPtr) 
	{
		if (*leftPtr != *rightPtr)
		{
			isPal = false;
		}
		++leftPtr;
		--rightPtr;
	}

	return isPal;
}
 
 

Recent GIDBlogWriting a book 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

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

All times are GMT -6. The time now is 06:31.


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