![]() |
|
#1
|
|||
|
|||
Using pointers and stringsI 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:
Anyone have any suggestions? thanks |
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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:
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:
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
|
|||
|
|||
|
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:
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
|
|||
|
|||
|
Quote:
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:
(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
|
|||
|
|||
|
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
|
|||
|
|||
|
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:
|
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The