View Full Version: i really need help in this program

C++ Learning Community > C++ Help > i really need help in this program


Title: i really need help in this program
Description: binary conversion and Palindromes


elsa - January 20, 2007 08:21 AM (GMT)
hi..
im trying to build a program using arrays to do the following things:
1- convert a digital number to binary using mathematical operators by using the function void binaryConversionUsingArithematicOperators(unsigned long)
2- convert a digital number to binary using bitwise operators by using the function void binaryConversionUsingBitwiseOperators(unsigned long)
3- check if the word is a palindrome using iteration by using this function
void bool isPalindromeUsingIteration(string)
4- check if the word is a palindrome using recursion by using this function
void bool isPalindromeUsingRecursion(string)

here is what i wrote:
CODE
#include<iostream>
#include<string>
#include<cctype>
using std::string;
using std::cout;
using std::cin;
using std::endl;

// Here are some function declarations

unsigned short getMenuSelection( );
void binaryConversionUsingArithmeticOperators(unsigned long);
void binaryConversionUsingBitwiseOperators (unsigned long);
void isPalindromeUsingIteration(string);
void isPalindromeUsingRecursion(string);

// Utilityfunctions declarations
void initializeArray(unsigned short[],int);
void initializeArray(string[],int);
void printBinary(unsigned short[],int);
void printBinary(string[],int);

int main()
{
unsigned short choice;
do
{
choice = getMenuSelection();

cout <<"You chose Menu Option #"<<choice<<endl;
switch(choice)
{
case 1 :
unsigned long a;
cout<<" Please enter an integer ";
cin>>a;
binaryConversionUsingArithmeticOperators(a);
break;
case 2 :
cout<<" Please enter an integer ";
cin >>a;
binaryConversionUsingBitwiseOperators(a);
break;
case 3 :
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 4:
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 5:
break;
default:
cout<<"you should never get here\a\a\a\a!!!";
}
}while(choice != 5);
cout<<"you choose to quit\a\a\a"<<endl;
return 0;
}

//function definitions
unsigned short getMenuSelection()
{
unsigned short selection=0;
do
{
cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
cout<<"-5- Quit"<<endl;
cout<<"please enter your selection <1-5>:"<<endl;
cin>>selection;
}
while(selection<1 || selection>5);
return selection;
}
void binaryConversionUsingArthematicOperators(unsigned long x)
{
unsigned short binaryDigit[100],
numberOfDigits=0;
initializeArray(binaryDigit,100);
do
{
binaryDigit[numberOfDigits++]=x%2;
x=x/2;
}while(x!=0);
printBinary(binaryDigit,numberOfDigits);
}

void binaryConversionUsingBitwiseOperator(unsigned long x)
{
unsigned short binaryDigit [100],
numberOfDigits=0;
initializeArray(binaryDigit , 100);
do
{
binaryDigit[numberOfDigits++]=x&1;
x=x>>1;
}while(x!=0);
printBinary(binaryDigit, numberOfDigits);
}


void isPalindromeUsingIteration(string y)
{
while (length>1)
{
if ( y[] != [y + length - 1])
{
return false;
}
++y;
length=length-2;
}
return true;
}
void isPalindromeUsingRecursion(string y)
{
if ( length<2 )
{
return true;
}
else
{
if ( y[] != [y + length - 1])
{
return false;
}
else
{
return ispalindromeUsingRecursion(string y+1);
}
}

}
//utality function definitions
void initializeArray (unsigned short a[], int size)
{
for(int i=0;i<size; i++)
a[i]=0;
}

void printBinary(unsigned short a[], int size)
{
for(int i=size-1; i>=0; i--)
cout<<a[i];
cout<<endl;
}
void initializeArray (string b[], int length)
{
for(int i=0;i<length; i++)
b[i]=b;
}

void printBinary(string b[], int length)
{
for(int i=length-1; i>=0; i--)
cout<<b[i];
cout<<endl;

but there are 22 errors!!! i really need some help here..

Zaqufant - January 20, 2007 01:20 PM (GMT)
I can't barley read that. You should indent your code so it's easier to read.

elsa - January 20, 2007 02:14 PM (GMT)
hi..sorry for that..here is the code in a neat way and after some corrections that made the errors go down from 22 to 17..plz help me

CODE
#include<iostream>
#include<string>
#include<cctype>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int length;
// Here are some function declarations

unsigned short getMenuSelection( );
void binaryConversionUsingArithmeticOperators(unsigned long);
void binaryConversionUsingBitwiseOperators (unsigned long);
void isPalindromeUsingIteration(string);
void isPalindromeUsingRecursion(string);

// Utilityfunctions declarations
void initializeArray(unsigned short[],int);
void initializeArray(string[],int);
void printBinary(unsigned short[],int);
void printBinary(string[],int);

int main()
{
unsigned short choice;
do
{
 choice = getMenuSelection();
 
 cout <<"You chose Menu Option #"<<choice<<endl;
 switch(choice)
 {
 case 1 :
  unsigned long a;
  cout<<" Please enter an integer ";
  cin>>a;
  binaryConversionUsingArithmeticOperators(a);
  break;
 case 2 :
  cout<<" Please enter an integer ";
  cin >>a;
  binaryConversionUsingBitwiseOperators(a);
  break;
 case 3 :
  string b;
  cout<<"Please enter a string ";
  cin>>b;
  isPalindromeUsingIteration(b);
  break;
 case 4:
  string b;
  cout<<"Please enter a string ";
  cin>>b;
  isPalindromeUsingIteration(b);
  break;
 case 5:
  break;
 default:
  cout<<"you should never get here\a\a\a\a!!!";
 }
}while(choice != 5);
cout<<"you choose to quit\a\a\a"<<endl;
return 0;
}

//function definitions
unsigned short getMenuSelection()
{
unsigned short selection=0;
do
{
 cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
 cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
 cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
 cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
 cout<<"-5- Quit"<<endl;
 cout<<"please enter your selection <1-5>:"<<endl;
 cin>>selection;
}
while(selection<1 || selection>5);
return selection;
}
void binaryConversionUsingArthematicOperators(unsigned long x)
{
unsigned short binaryDigit[100],
 numberOfDigits=0;
initializeArray(binaryDigit,100);
do
{
 binaryDigit[numberOfDigits++]=x%2;
 x=x/2;
}while(x!=0);
printBinary(binaryDigit,numberOfDigits);
}

void binaryConversionUsingBitwiseOperator(unsigned long x)
{
unsigned short binaryDigit [100],
 numberOfDigits=0;
initializeArray(binaryDigit , 100);
do
{
 binaryDigit[numberOfDigits++]=x&1;
 x=x>>1;
}while(x!=0);
printBinary(binaryDigit, numberOfDigits);
}


void isPalindromeUsingIteration(string y)
{
while (length>1)
  {
       if (y[] != [y + length - 1])
       {
           cout<<"false"<<endl;
       }
       ++y;
      length=length-2;
  }
  cout<<"true"<<endl;
}
void isPalindromeUsingRecursion(string y)
{
if ( length<2 )
  {
       cout<<"true"<<endl;
  }
  else
  {
       if ( y[] != [y + length - 1])
       {
           cout<<"false"<<endl;
       }
       else
       {
           cout<<ispalindromeUsingRecursion(string y+1)<<endl;
       }
  }

}
//utality function definitions
void initializeArray (unsigned short a[], int size)
{
for(int i=0;i<size; i++)
 a[i]=0;
}

void printBinary(unsigned short a[], int size)
{
for(int i=size-1; i>=0; i--)
 cout<<a[i];
cout<<endl;
}
void initializeArray (string b[], int length)
{
for(int i=0;i<length; i++)

}

void printBinary(string b[], int length)
{
for(int i=length-1; i>=0; i--)
 cout<<b[i];
cout<<endl;
}

the errors i get are as the following
QUOTE

error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(51) : error C2086: 'b' : redefinition
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(51) : see declaration of 'b'
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(51) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(45) : see declaration of 'b'
(114) : error C2059: syntax error : ']'
(115) : error C2143: syntax error : missing ';' before '{'
(118) : error C2675: unary '++' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this opera
tor or a conversion to a type acceptable to the predefined operator
(131) : error C2059: syntax error : ']'
(132) : error C2143: syntax error : missing ';' before '{'
(135) : error C2181: illegal else without matching if
(137) : error C2065: 'ispalindromeUsingRecursion' : undeclared identifier
(137) : error C2275: 'string' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\xstring(612) : see declaration of 'string'


plz help me..the assignment is due in 2 days..

Zaqufant - January 20, 2007 02:51 PM (GMT)
Well, I think for one thing, there already is a declaration of the string b. Try chaning the name to somthing more descriptive, or unique, so not to get those errors about the b string.

And, you shouldn't define variables inside of statments like that. This is not BASIC. Declare and define all the variables you are using in that function first, then use then normally, just without the definition. so instead of puting the
CODE
string b;
and
CODE
unsighned long a;
inside the switch staments, you could just put both of them right after you go into the main() function.

CODE
int main()
{
string b;
unsighned long a;
unsigned short choice;

do
{
//bla bla
}while(/*bla bla*/)
}
Another reason you do this, is because you may have to go into the 2 switch before you get to the 1 switch, and there is no defined a variable, and you would screw up the program.

[code]if (y[] != [y + length - 1])

You can't add a string onto a intiger. But, you could add the length of the string by doing
CODE
lenght+=stringb.size();



CODE
void isPalindromeUsingRecursion(string y)
{
if ( length<2 )
 {
      cout<<"true"<<endl;
 }
 else
 {
      if ( y[] != [y + length - 1])
      {
          cout<<"false"<<endl;
      }
      else
      {
          cout<<[b][u]ispalindromeUsingRecursion(string y+1)[b][u]<<endl;
      }
 }

}

C++ is case sensitive. You must use the exact same name as the function you would like to access. it has so have the exact same casing too.

That's all I can see for now. Now, don't bet your lunch money on what I've just told you. I may be wrong. So, keep working on it, and wait for the other much smarter people come to fix it.

biggoron - January 20, 2007 10:49 PM (GMT)
To declare a variable in a case you need to but braces around it like this:

case <whatever> : {
<type> <whatever>;
}

I don't think you can do this either:
QUOTE
      if ( y[] != [y + length - 1])




Hosted for free by InvisionFree