Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-09-2005, 04:03 AM   #1 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

Magnetic Life

Question C++ problem

I made a program to check weather an entered number is a palindrome or not ...

Code:
#include<iomanip>
#include<cctype>

using namespace std;
int main()
{
  int count=0,d=0;
  long int array[count];
  cout << "Enter a number";
  for(count=0;;count++)
  {
    cin >> array[count];
    if(!isdigit(array[count]))
    	break;
  }

  for(int check=0; check<=count; check++,count--)
  {
    if(array[check]==array[count])
      d++;
    if(d==count)
      cout << "the number is a palindrome";
  }

return 0;
}
There is some error, can any one help me ...
Magnetic Life is offline  
Old 03-09-2005, 05:35 AM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

In this part of you're code you're going to enter an endless loop

Code:
  for(count=0;;count++)
  {
    cin >> array[count];
    if(!isdigit(array[count]))
    	break;
  }
You need to have a condition for when to terminate the loop. I forget what it is, but I'll be there's a function somewhat similar to the java arraylen that you can pass the array into to determine the length. Set that as the termination criteria.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 03-09-2005, 08:32 AM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

Magnetic Life

Default

Please anyone who knows what to put in condition area
Magnetic Life is offline  
Old 03-09-2005, 09:08 AM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

The break; statment should suffice. I don't see anything wrong with that section of code. What error are you getting exactly?
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 03-09-2005, 12:05 PM   #5 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Your reading data into a single long int value, but looping over multiple long int values (which aren't there). If you want to do things that way, read into a character array.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-09-2005, 06:38 PM   #6 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

Code:
#include<iostream>
#include<iomanip>
#include<cctype>

using namespace std;

int main() {
        int count=0,d=0,noChars=0;
        char array[count]; //you want to read in a string of chars not a number
        cout << "Enter a number: ";

        for(count=0;;count++){
                cin >> array[count];
                if(!isdigit(array[count]))
                break;
        }
        count--; //to remove whatever non digit was used to terminate the for loop
        noChars = count;        //you are changing count in the following loop so
                                //cannot use it in the last if statement


        for(int check=0; check<count; check++,count--){
                if(array[check]==array[count]){
                        d++;
                }else{
                        cout << "the number is not a palindrome" << endl;
                        break;
                }

                if(d==noChars/2)
                        cout << "the number is a palindrome" << endl;
        }
        return 0;
}
I was feeling generous today - it's still not perfect though - it will not deal correctly with palindromes with an even number of characters but that shouldnt take too long to fix.

A couple of pointers:
1. You want to check characters at either end of a number, so you need to be using a character array not a long int. Dont be fooled into thinking that you have to use a number data type just because it is a number. If you are not doing mathematical calculations on it then you can use a char[].

2. You are terminating your first loop by entering a non digit, so you want to subtract this from count

3. break is not tidy! - Ok im a java programmer and in java it is not considered 'good code' to use a break (perhaps it is in C++) but you should always think of ways out of your loop rather than relying on break.

I assume this was some homework of some sort, so i shouldnt have done as much for you as i have, so at least try and understand wat is going on else you are wasting your time - changes some things, see what happens, try and fix the bug that is still in the program!
fitzjj is offline  
Old 03-09-2005, 07:55 PM   #7 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Tip: The easyest way to check is to reverse the string.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On