?
Solved

Can someone debug this program for me?

Posted on 2003-03-17
6
Medium Priority
?
211 Views
Last Modified: 2010-04-01
This program suppose to test for Palindrome
Here is the source

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

bool test Palindrome ( char pal [], int size ) {
     if ( size == 0 || size == 1)
          return true;

     if ( pal [0] == pal [ size - 1] ) {
          return test Palindrome ( pal + 1, size - 2 );
     }
     else {
          return false;
     }
}


void cleanString (char[]);

int main ()
{
     cout <<"Enter a sentence" << endl;
     char sentence [80];
     cin.getline (sentence, 80,'n\');

     cleanString (sentence);
     if (test Palindrome (sentence, string (sentence)))
          cout <<"This is a palindrome." << endl;
     else
          cout <<"This is not a palindrome." << endl;
     return 0;
}

void cleanString (char str[]) {
     int source = 0;
     int dest = 0;
     while (str [source] != '\0') {
          if (isspace (str[source]) || ispunct (str[source])) {
               source++;}
          else {
               str[dest++] = toupper (str[source++]);
          }
     }
     str[dest = '\0';
     endl;
}
0
Comment
Question by:Svy-20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 140 total points
ID: 8157382
>> bool test Palindrome ( char pal [], int size ) {

You can't have a space in a function-name. Make it:

bool test_Palindrome ( char pal[], int size ) {

>> return test Palindrome ( pal + 1, size - 2 );

return test_Palindrome ( pal + 1, size - 1 ) ; // notice the 'size - 1'

>> cin.getline (sentence, 80,'n\');

Make it:

cin.getline ( sentence, 80, '\n' ) ;

or simply:

cin.getline ( sentence, 80 ) ; // '\n' is default

>> str[dest = '\0';

str[dest] = '\0' ;

Hope that helps!

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157385
Also,

>> if (test Palindrome (sentence, string (sentence)))

if (test_Palindrome (sentence, string (sentence)))

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157388
And oh! I think it should be:

if (test_Palindrome (sentence, strlen (sentence)))

Mayank.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Svy-20
ID: 8162411
Thank you very much!
0
 

Author Comment

by:Svy-20
ID: 8162447
Can you debug this one for me too

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int FirstClass (int[], int, int);
int Economy (int[], int, int);
void Indication (int, int);

int main()
{
    int num, seat;
    const int Seat = 10;
    int assignSeat [Seat] = {0,0,0,0,0,0,0,0,0,0};

    while (true) {
         
         cout <<"Please type 1 for First Class" << endl;
         cout <<"Please type 2 for Economy" << endl;
         cin >> num;
     
         if (num == 1) {
              seat = FirstClass (assignSeat, Seat, FirstClass);
         }
         if (num == 2) {
              seat = Economy (assignSeat, Seat, Economy);
         }
         if (seat == -1)
              cout <<"Next flight leaves in 3 hours." << endl;
         else
              Indication (seat, Economy);
    }
    return 0;
}

int FirstClass (int assignSeat[], int Seat, int FirstClass) {
    char answer;
    for (int seat = 1; seat <= 5; seat++) {
         if (assignSeat [seat-1] == 0) {
             assignSeat [seat-1] = 1;
              return seat;
         }
    }

    cout <<"The First Class section is full" << endl;
    cout <<"Would you like to sit in the Economy section (y or n)";
    cin >> answer;
   
     if (answer == 'y' || answer == 'Y') {
         return Economy (assignSeat, Seat, Economy);
    }
    else {
         return -1;
    }
}

int Economy (int assignSeat [], int Seat, int Economy) {
    char answer;
    for (int seat = 6; seat <= 10; seat++) {
         assignSeat [seat-1] = 0; {
         assignSeat [seat-1] = 1;
         return seat;
         }
    }
    cout <<"Next flight leaves in 3 hours" << endl;
}


void Indication (int seat, int Economy) {
    if (seat < Economy)
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in First Class section" << endl;
    else
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in Economy section" << endl;
}  
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8164135
>> seat = FirstClass (assignSeat, Seat, FirstClass);

FirstClass is the name of the function itself and you are also passing it as the third argument (where an integer is to be passed). As per your need, I see that you are not using the third argument anywhere in the function. The same error has occured on:

>> seat = Economy (assignSeat, Seat, Economy);

You can change the function prototypes to:

int FirstClass (int[], int);
int Economy (int[], int);
void Indication (int, int);

[In fact, I see that you're also not using the second argument.]

Please change the function calls to:

seat = FirstClass (assignSeat, Seat);
seat = Economy (assignSeat, Seat);

Also, in the function definitions, please write:

int FirstClass (int assignSeat[], int Seat) {

int FirstClass (int assignSeat[], int Seat) {

In the function call:

Indication (seat, Economy);

I see that in this function, you are using the second argument, but please change the name from Economy to something else. Also, while calling, you should pass an integer value and if you want to pass the value returned by the Economy () function, then you should make it:

Indication ( seat, Economy ( assignSeat, Seat ) ) ;


Hope that helps!

Mayank.

PS: From now on, please post new doubts under new questions! Cheers :-)



0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question