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: -
-
#include <iostream>
-
#include <cstring>
-
using namespace std;
-
-
const int maxchars = 81;
-
char test[maxchars];
-
bool palindrome;
-
-
void main ()
-
{
-
//Input loop
-
while (1)
-
{
-
cout << "Please enter a possible palindrome (no more than 80 characters):" << endl;
-
cin.getline(test, maxchars);
-
-
if (test == "END")
-
break;
-
else {
-
if (palindrome(test)) //Getting an error here that says the function does not evaluate to a function taking 1 argument
-
cout << test << " is a palindrome." << endl;
-
else
-
cout << test << " is not a palindrome." << endl;
-
}
-
}
-
}
-
-
//bool definition
-
bool palindrome(test) //Getting an error here that says function style initializer appears to be a function style definition
-
{
-
int length, middle, stop, place, revplace;
-
length = strlen(test);
-
if (length < 2) //short palindrome
-
return true;
-
-
middle = ((length - 1) / 2)
-
if (2 * middle + 1 == length) //length is odd
-
stop = middle - 1; //ignores middle character
-
else //length is even
-
stop = middle;
-
-
revplace = length - 1;
-
for (place = 0; place <= stop,; place++, revplace--)
-
{
-
if (test[place] != test[revplace])
-
return false;
-
}
-
return true;
-
}
5 2355
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: - <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 - if (strcmp(test, "END") == 0)
than to use:
Good Luck
Alright, I changed the if statement to the strcmp like you suggest as well as changed:
to - 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: -
-
while (1)
-
{
-
cout << "Please enter a possible palindrome (no more than 80 characters):" << endl;
-
cin.getline(test, maxchars);
-
if (strcmp(test, "END") == 0)
-
break;
-
else {
-
if (palindrome(test))
-
cout << test << " is a palindrome." << endl;
-
else
-
cout << test << " is not a palindrome." << endl;
-
}
I think my problem is with my if(palindrom(test)) statement, and I'm sure it's just imporperly formatted. Any ideas?
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
Thank you very much, I'll do my best to remember this formatting for functions in the future! You've been a great help.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: Paul |
last post by:
Hi,
Any one knows how to convert CString to LPCWSTR?
Please advice, thanks!
-P
|
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...
|
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...
|
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>...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |