473,795 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can you take a look at this code for me plz?

29 New Member
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 binaryConversio nUsingArithemat icOperators(uns igned long)
2- convert a digital number to binary using bitwise operators by using the function void binaryConversio nUsingBitwiseOp erators(unsigne d long)
3- check if the word is a palindrome using iteration by using this function
void bool isPalindromeUsi ngIteration(str ing)
4- check if the word is a palindrome using recursion by using this function
void bool isPalindromeUsi ngRecursion(str ing)

here is what i wrote:
code:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<cctype>
  4. using std::string;
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. // Here are some function declarations
  10.  
  11. unsigned short getMenuSelection( );
  12. void binaryConversionUsingArithmeticOperators(unsigned long);
  13. void binaryConversionUsingBitwiseOperators (unsigned long);
  14. void isPalindromeUsingIteration(string);
  15. void isPalindromeUsingRecursion(string);
  16.  
  17. // Utilityfunctions declarations
  18. void initializeArray(unsigned short[],int);
  19. void initializeArray(string[],int);
  20. void printBinary(unsigned short[],int);
  21. void printBinary(string[],int);
  22.  
  23. int main()
  24. {
  25.     unsigned short choice;
  26.     do
  27.     {
  28.         choice = getMenuSelection();
  29.  
  30.         cout <<"You chose Menu Option #"<<choice<<endl ;
  31.         switch(choice)
  32.         {
  33.         case 1 :
  34.             unsigned long a;
  35.             cout<<" Please enter an integer " ;
  36.             cin>>a ;
  37.             binaryConversionUsingArithmeticOperators(a);
  38.             break ;
  39.         case 2 :
  40.             cout<<" Please enter an integer " ;
  41.             cin >>a ;
  42.             binaryConversionUsingBitwiseOperators(a) ;
  43.             break ;
  44.         case 3 :
  45.             string b;
  46.             cout<<"Please enter a string ";
  47.             cin>>b;
  48.             isPalindromeUsingIteration(b);
  49.             break;
  50.         case 4:
  51.             string b;
  52.             cout<<"Please enter a string ";
  53.             cin>>b;
  54.             isPalindromeUsingIteration(b);
  55.             break;
  56.         case 5:
  57.             break;
  58.         default:
  59.             cout<<"you should never get here\a\a\a\a!!!";
  60.         }
  61.     }while(choice != 5);
  62. cout<<"you choose to quit\a\a\a"<<endl;
  63. return 0;
  64. }
  65.  
  66. //function definitions
  67. unsigned short getMenuSelection()
  68. {
  69.     unsigned short selection=0;
  70.     do
  71.     {
  72.         cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
  73.         cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
  74.         cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
  75.         cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
  76.         cout<<"-5- Quit"<<endl;
  77.         cout<<"please enter your selection <1-5>:"<<endl;
  78.         cin>>selection;
  79.     }
  80.     while(selection<1 || selection>5);
  81.     return selection;
  82. }
  83. void binaryConversionUsingArthematicOperators(unsigned long x)
  84. {
  85.     unsigned short binaryDigit[100],
  86.         numberOfDigits=0;
  87.     initializeArray(binaryDigit,100);
  88.     do
  89.     {
  90.         binaryDigit[numberOfDigits++]=x%2;
  91.         x=x/2;
  92.     }while(x!=0);
  93.     printBinary(binaryDigit,numberOfDigits);
  94. }
  95.  
  96. void binaryConversionUsingBitwiseOperator(unsigned long x)
  97. {
  98.     unsigned short binaryDigit [100],
  99.         numberOfDigits=0;
  100.     initializeArray(binaryDigit , 100);
  101.     do
  102.     {
  103.         binaryDigit[numberOfDigits++]=x&1;
  104.         x=x>>1;
  105.     }while(x!=0);
  106.     printBinary(binaryDigit, numberOfDigits);
  107. }
  108.  
  109.  
  110. void isPalindromeUsingIteration(string y)
  111. {
  112. while (length>1)
  113.    {
  114.         if ( y[] != [y + length - 1])
  115.         {
  116.             return false;
  117.         }
  118.         ++y;
  119.        length=length-2;
  120.    }
  121.    return true;
  122. }
  123. void isPalindromeUsingRecursion(string y)
  124. {
  125. if ( length<2 )
  126.    {
  127.         return true;
  128.    }
  129.    else
  130.    {
  131.         if ( y[] != [y + length - 1])
  132.         {
  133.             return false;
  134.         }
  135.         else
  136.         {
  137.             return ispalindromeUsingRecursion(string y+1);
  138.         }
  139.    }
  140.  
  141. }
  142. //utality function definitions
  143. void initializeArray (unsigned short a[], int size)
  144. {
  145.     for(int i=0;i<size; i++)
  146.         a[i]=0;
  147. }
  148.  
  149. void printBinary(unsigned short a[], int size)
  150. {
  151.     for(int i=size-1; i>=0; i--)
  152.         cout<<a[i];
  153.     cout<<endl;
  154. }
  155. void initializeArray (string b[], int length)
  156. {
  157.     for(int i=0;i<length; i++)
  158.         b[i]=b;
  159. }
  160.  
  161. void printBinary(string b[], int length)
  162. {
  163.     for(int i=length-1; i>=0; i--)
  164.         cout<<b[i];
  165.     cout<<endl;
  166. }
but there are 22 errors!!! i really need some help here..
Jan 20 '07 #1
1 1330
Banfa
9,065 Recognized Expert Moderator Expert
Comments in bold

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<cctype>
  4. using std::string;
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. // Here are some function declarations
  10.  
  11. unsigned short getMenuSelection( );
  12. void binaryConversionUsingArithmeticOperators(unsigned long);
  13. void binaryConversionUsingBitwiseOperators (unsigned long);
  14. void isPalindromeUsingIteration(string);
  15. void isPalindromeUsingRecursion(string);
  16.  
  17. // Utilityfunctions declarations
  18. void initializeArray(unsigned short[],int);
  19. void initializeArray(string[],int);
  20. void printBinary(unsigned short[],int);
  21. void printBinary(string[],int);
  22.  
  23. int main()
  24. {
  25.     unsigned short choice;
  26.     do
  27.     {
  28.         choice = getMenuSelection();
  29.  
  30.         cout <<"You chose Menu Option #"<<choice<<endl ;
  31.         switch(choice)
  32.         {
  33.         case 1 :
  34. /* Declaration of data in a switch statement is ill advised 
  35.    at best, it only works here because unsigned long is a 
  36.    basic type, i.e. part of the base language, unlike ... */
  37.             unsigned long a;
  38.             cout<<" Please enter an integer " ;
  39.             cin>>a ;
  40.             binaryConversionUsingArithmeticOperators(a);
  41.             break ;
  42.         case 2 :
  43.             cout<<" Please enter an integer " ;
  44.             cin >>a ;
  45.             binaryConversionUsingBitwiseOperators(a) ;
  46.             break ;
  47.         case 3 :
  48. /* ... here.  string is not a basic type. It is a class and requires 
  49.    constructing.  You have declared string twice, once here and 
  50.    once in case 4.  However these are in the same code block so
  51.    you have multiply defined the variable in this code block and an 
  52.    error is produced.  Not only that but if you remove the declaration 
  53.    in 4 then although the multiply defined error will go away but you 
  54.    would be trying to use the variable without having called the 
  55.    constructor, a BIG mistake.  You could create some artifical code 
  56.    blocks by just adding braces insice your case statements but
  57.    all in all the best thing to do is NOT DECLARE DATA IN A 
  58.    SWITCH.  Declare it before the switch.*/
  59.             string b;
  60.             cout<<"Please enter a string ";
  61.             cin>>b;
  62.             isPalindromeUsingIteration(b);
  63.             break;
  64.         case 4:
  65.             string b;
  66.             cout<<"Please enter a string ";
  67.             cin>>b;
  68.             isPalindromeUsingIteration(b);
  69.             break;
  70.         case 5:
  71.             break;
  72.         default:
  73.             cout<<"you should never get here\a\a\a\a!!!";
  74.         }
  75.     }while(choice != 5);
  76. cout<<"you choose to quit\a\a\a"<<endl;
  77. return 0;
  78. }
  79.  
  80. //function definitions
  81. unsigned short getMenuSelection()
  82. {
  83.     unsigned short selection=0;
  84.     do
  85.     {
  86.         cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
  87.         cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
  88.         cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
  89.         cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
  90.         cout<<"-5- Quit"<<endl;
  91.         cout<<"please enter your selection <1-5>:"<<endl;
  92.         cin>>selection;
  93.     }
  94.     while(selection<1 || selection>5);
  95.     return selection;
  96. }
  97.  
  98. /* This function does not have the same name as the one 
  99.    declared and called */
  100. void binaryConversionUsingArthematicOperators(unsigned long x)
  101. {
  102.     unsigned short binaryDigit[100],
  103.         numberOfDigits=0;
  104.     initializeArray(binaryDigit,100);
  105.     do
  106.     {
  107.         binaryDigit[numberOfDigits++]=x%2;
  108.         x=x/2;
  109.     }while(x!=0);
  110.     printBinary(binaryDigit,numberOfDigits);
  111. }
  112.  
  113. /* This function does not have the same name as the one 
  114.    declared and called */
  115. void binaryConversionUsingBitwiseOperator(unsigned long x)
  116. {
  117.     unsigned short binaryDigit [100],
  118.         numberOfDigits=0;
  119.     initializeArray(binaryDigit , 100);
  120.     do
  121.     {
  122.         binaryDigit[numberOfDigits++]=x&1;
  123.         x=x>>1;
  124.     }while(x!=0);
  125.     printBinary(binaryDigit, numberOfDigits);
  126. }
  127.  
  128.  
  129. void isPalindromeUsingIteration(string y)
  130. {
  131. /* The variable length is used without being without defined. */
  132. /* Logical appears to be incorrectly copied from isPalindromeUsingRecursion. */
  133. while (length>1)
  134.    {
  135.         if ( y[] != [y + length - 1])
  136.         {
  137. /* Attempt to return a value from a void function */
  138.             return false;
  139.         }
  140.         ++y;
  141.        length=length-2;
  142.    }
  143. /* Attempt to return a value from a void function */
  144.    return true;
  145. }
  146. void isPalindromeUsingRecursion(string y)
  147. {
  148. /* The variable length is used without being without defined. */
  149. if ( length<2 )
  150.    {
  151. /* Attempt to return a value from a void function */
  152.         return true;
  153.    }
  154.    else
  155.    {
  156. /* Syntax is wrong, this should be comparing the first and last 
  157.    character in the string y. */
  158.         if ( y[] != [y + length - 1])
  159.         {
  160. /* Attempt to return a value from a void function */
  161.             return false;
  162.         }
  163.         else
  164.         {
  165. /* string y+1 is not a valid expression to provide a parameter.
  166.    An expression returning the string y with the first and last 
  167.    characters removed is required, look up string::substr() */
  168.             return ispalindromeUsingRecursion(string y+1);
  169.         }
  170.    }
  171.  
  172. }
  173. //utality function definitions
  174. void initializeArray (unsigned short a[], int size)
  175. {
  176.     for(int i=0;i<size; i++)
  177.         a[i]=0;
  178. }
  179.  
  180. void printBinary(unsigned short a[], int size)
  181. {
  182.     for(int i=size-1; i>=0; i--)
  183.         cout<<a[i];
  184.     cout<<endl;
  185. }
  186. void initializeArray (string b[], int length)
  187. {
  188.     for(int i=0;i<length; i++)
  189. /* b[i] and b must (by definition) be different types so an assignment
  190.    between them is unlikely to work. */
  191.         b[i]=b;
  192. }
  193.  
  194. void printBinary(string b[], int length)
  195. {
  196.     for(int i=length-1; i>=0; i--)
  197.         cout<<b[i];
  198.     cout<<endl;
  199. }
Jan 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1339
by: uli2003wien | last post by:
Dear group, PASS (SQL-Server user group) is about to start founding a branch in Vienna. I went to SQLCON and met some guys from the German PASS group and decided to become a member of PASS, decided to look for other individuals to take part in an Austrian branch of PASS based in Vienna, so whoever is interested may take a look at http://www.sqlpass.de/Default.aspx?tabid=191 in order to get to contact me to get things going....
0
1573
by: orshov | last post by:
Hi there, Please take a look at the new internet program that I recently joined. They give out Free Internet resources delivered to your email box daily. Not only is it FREE, but you Get Paid as well! You'll Earn $10 Just For Signing Up...FREE! I think, it would be best if you just take a look at it
8
4098
by: zalzon | last post by:
Do I have to use char * string and then malloc memory to it? But malloc(size) can only gurantee that size is allocated. What if the string is more than size? Is there any way to find the size of the string before taking it in and THEN malloc the needed amount? I'm in a rut, help me please :(
9
2825
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
2
1482
by: Giovane Calabrese | last post by:
( aspx + vb ) hi everyone ! I really need make that function work ! im brazilian , and i want to make a multilanguage system , that function above must look at all ASPX take the labels ID and send as a parameter ,... so Please , help me ,.. cause it doest work ! '---------------------------------------------------------- --------------
0
1795
by: Giovane Calabrese | last post by:
ok. im try to make a multilanguage system based on that tutorial : http://www.123aspx.com/redir.aspx?res=29112 in my aspx pages all text are in labels , and i want to take the name labels ( in one loop ) and for each label take on the XML document the right(in the right language) Value (text).
8
2581
by: Paw | last post by:
Greetings. I use asp. what I need is is when a visitor comes to the site, I need it to check the host name. if "www.hometowndigest.com" is the host, then check a folder named "something" and if the folder does not exsist, create folder "www" and then copy folder "temp" and its contents. If the folder "www" is there, look in it, check to see if the files in folder "temp" are in there, if not, copy the files that are not from folder...
16
3502
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
0
1386
by: watches0958 | last post by:
With today's fashion trends in women's clothing, it can take more effort to dress for success every day, than to get the job in the first place. When you're working every day, you have different roles. You may be meeting clients, or working at a desk, or going from the office to a business event. One business outfit doesn't work for every occasion any more. Here are 3 tips for dressing for success in 2008
2
2897
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the user for angle and start calculating the vertical and horizantal positions. when the vertical position gets negative program should stop and check the horizantal position and print out different messeges based on the value of the horizantal speed. ...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.