Need help with the conversion

This is a discussion on Need help with the conversion within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cstdlib> using namespace std; void search(char a,char b[], int size); int main() { int j = ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    18

    Need help with the conversion

    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    void search(char a,char b[], int size);
    
    
    int main()
    {
        int j = 0;
        char *a,*b;
        int size = 5;
        a = new char[size];
        b = new char;
        int i;
        
        for(int i = 0 ; i < size ; i++)
        cin>>a[i];
          for( i = 0; i < size; i++)
           if(!search(a,b,size))    //This is my main problem of conversion
           {
                 b[j]= a[i];
                   j++;
           }
        for(int i= 0; i < j; i++)
        cout>>b[i];
           
                            
        
        
        cout<<endl;
        
          system("PAUSE");
          return 0;
    }
    void search(char a,char b[], int size)
    {    
         bool found = false;
         for(int i = 0 ; i < size ; i++)
                  if (a == b[i])
                  found = true;  
                  return found;    
    }
    Here is my code and I need help from the conversion. Even I try to pre-write the code to avoid that mistake but it still appear in another line, and I don't know how to fix this. Any suggestion?????

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Here a few errors in your program :

    Code:
    void search(char a,char b[], int size);
    Change the return type to bool.

    Code:
    if(!search(a,b,size))    //This is my main problem of conversion
    Your function takes in 'char' as the first argument. a is of type 'char*'. I feel that you have to send a[i] as the first argument and not a.

    Code:
    cout>>b[i];
    This should be cout<<b[i].

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    18
    I change it to *a and it works but the function isn't running completely.
    Can anyone tell me what do I need in order to store the same number become 1 and show in new array.
    Example: i have original array is 223344
    and it stores to new array 234
    In my code it seems like store only first character of the array! Is my pointer setting wrong??????
    Last edited by erictu; 03-18-2012 at 04:00 PM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Change this : search(a,b,size)
    to this : search(a[i],b,size)

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    18
    i figured out, it worked with *(a+i)
    Thanks for helping

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,939
    Quote Originally Posted by erictu
    i figured out, it worked with *(a+i)
    That is equivalent to a[i], which is more readable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    18
    at first i tried the a[i] but it didn't work but I realized i had missed the [size] for point *b.For both case it works perfectly thanks a lot

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Why do you use C-strings in the first place? Why aren't you using std::string?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C to C++ conversion
    By racerday182 in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2008, 05:20 PM
  2. US vs UK Conversion
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-11-2004, 12:32 PM
  3. Conversion?
    By mart_man in forum C Programming
    Replies: 7
    Last Post: 12-08-2002, 06:23 PM
  4. conversion
    By sonict in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2002, 07:01 PM
  5. Conversion
    By CyberMax8 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21