[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Can someone debug this program for me?

Posted on 2003-03-17
6
Medium Priority
?
225 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
  • 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

590 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