473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple problem with bools and <cstring>

16
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've missed 2.5 weeks of lecture and there's only so much I can learn from my outdated book. I'm doing ye old palindrome checker, but instead of just ripping off code from others I tried it myself and I'm getting errors from my bool arguments. There's literally only 3 pages on bools in my textbook and none if it seems to help my situation. Here's the section of my code involving my bools:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const    int maxchars = 81;
  7. char    test[maxchars];
  8. bool    palindrome;
  9.  
  10. void main ()
  11. {
  12. //Input loop
  13. while (1)
  14.        {      
  15.        cout << "Please enter a possible palindrome (no more than 80 characters):" << endl;
  16.        cin.getline(test, maxchars);
  17.  
  18.        if (test == "END")
  19.             break;
  20.        else {
  21.               if (palindrome(test)) //Getting an error here that says the function does not evaluate to a function taking 1 argument 
  22.                    cout << test << " is a palindrome." << endl;
  23.               else
  24.                    cout << test << " is not a palindrome." << endl;
  25.                }
  26.          }
  27. }
  28.  
  29. //bool definition
  30. bool palindrome(test) //Getting an error here that says function style initializer appears to be a function style definition 
  31.     {
  32.     int length, middle, stop, place, revplace;
  33.     length = strlen(test);
  34.     if (length < 2)                    //short palindrome
  35.         return true;
  36.  
  37.     middle = ((length - 1) / 2)
  38.     if (2 * middle + 1 == length)    //length is odd
  39.         stop = middle - 1;      //ignores middle character
  40.     else                            //length is even
  41.         stop = middle;
  42.  
  43.     revplace = length - 1;
  44.     for (place = 0; place <= stop,; place++, revplace--)
  45.         {
  46.         if (test[place] != test[revplace])
  47.             return false;
  48.         }
  49.     return true;
  50.     } 
Feb 28 '07 #1
5 2355
willakawill
1,646 1GB
Hi. There are a couple of problems with your code.
When you define a function like yours in c++ you need to use the following:
Expand|Select|Wrap|Line Numbers
  1. <return type> <function name>(<parameter type> <parameter name>);
the parameter cannot be something that has already been defined. You can use a global variable in your function instead of a parameter and declare your parameters as void. You have to repeat this syntax when you are defining the body of the function.

It is also better to use
Expand|Select|Wrap|Line Numbers
  1. if (strcmp(test, "END") == 0)
than to use:
Expand|Select|Wrap|Line Numbers
  1. if (test == "END")
Good Luck
Feb 28 '07 #2
Randeh
16
Alright, I changed the if statement to the strcmp like you suggest as well as changed:

Expand|Select|Wrap|Line Numbers
  1. bool palindrome(test)
to

Expand|Select|Wrap|Line Numbers
  1. bool palindrome(string ispal)
and the error on that line disappeared, but I'm still stuck with "error C2064: term does not evaluate to a function taking 1 arguments" in my repeating loop:

Expand|Select|Wrap|Line Numbers
  1.  
  2. while (1)
  3.      {
  4.      cout << "Please enter a possible palindrome (no more than 80 characters):" << endl;
  5.      cin.getline(test, maxchars);
  6.           if (strcmp(test, "END") == 0)
  7.     break;
  8.           else {
  9.     if (palindrome(test))
  10.          cout << test << " is a palindrome." << endl;
  11.     else
  12.          cout << test << " is not a palindrome." << endl;
  13.      }
I think my problem is with my if(palindrom(test)) statement, and I'm sure it's just imporperly formatted. Any ideas?
Feb 28 '07 #3
willakawill
1,646 1GB
That is not the problem. You have to re write the function header twice and the parameter is char* not string

bool palindrome; is not correct
Feb 28 '07 #4
Randeh
16
Thank you very much, I'll do my best to remember this formatting for functions in the future! You've been a great help.
Feb 28 '07 #5
willakawill
1,646 1GB
Thank you very much, I'll do my best to remember this formatting for functions in the future! You've been a great help.
You are very welcome
Feb 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
5
by: Paul | last post by:
Hi, Any one knows how to convert CString to LPCWSTR? Please advice, thanks! -P
0
by: danip | last post by:
Hi, I converted a VC++ 6.0 dll to VC++ 2003. It's unmanaged. Now when I'm trying to implement calling to static method that returns CString I receive the error message above in the Link. What I'm...
3
by: howachen | last post by:
from many books said, to use strlen(), you need to include <cstringor <string.h>, but i found the following program work, why? #include <iostream> using namespace std; int main() { char...
7
by: Old Wolf | last post by:
On one particular compiler, this program fails to compile because memset is an undeclared symbol (it's in namespace std but not in the global namespace): #include <cstring> #include <string.h>...
2
by: Sean F. Aitken | last post by:
Good afternoon, We have an app that uses a CMap with CString types for values and accepts LPCSTR as the parameter. The object being created is on the heap (created dynamically). A leak detector...
8
by: Carmen Sei | last post by:
it seem to me that when doing include - #include <string.h- is CRT #inlcude <string- is C++ standard library Is that true those header with .h extension is CRT and those without extension...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.