Help with function :)

This is a discussion on Help with function :) within the C++ Programming forums, part of the General Programming Boards category; ok i have a function to return a certain letter for right now. I just want to get the function ...

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    Help with function :)

    ok i have a function to return a certain letter for right now. I just want to get the function to work, later i will have a graphic thing i will print that will correspond to the letter in each of the memory spaces. ok well here it is...

    Code:
    #include <iostream> 
    #include <cstring> 
    using namespace std;
    
    #define wait 900000
    
    char abcZ(char input[03]);
    
    int main()
    { 
        char user[03];
        int count; 
        cout << "Translate: ";
        cin.getline(user, 30);
        do{
             abcZ(user[30]);
        }while(count < 30);
    }
    
    char abcZ(char input[26])
    {
         int count = 0;
         char letter[30] = "abcdefghijklmnopqrstuvwxyz";
         if(strcmp(input[count], letter[count]) == 0)
         {
              count ++;
              return letter letter;
         }
    }
    it sends me an error on line 16 saying "invalid conversion 'char' to char''. Any advice would be nice ty .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > char user[03];
    Surely you meant 30

    > return letter letter;
    Maybe return letter[count];

    > abcZ(user[30]);
    To pass the array, it would be
    abcZ(user);

    > while(count < 30)
    count isn't initialised
    count isn't modified
    This either loops one or loops forever, depending on what random value count gets initialised with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    ok here i realize i made some mistakes in typing the code last time i will show you the copied from the editor...
    Code:
    #include <iostream> 
    #include <cstring> 
    using namespace std;
    
    #define wait 900000
    
    char abcZ(char input[03]);
    
    int main()
    { 
        char user[03];
        int count; 
        cout << "Translate: ";
        cin.getline(user, 30);
        do{
             abcZ(user[30]);
        }while(count < 30);
    }
    
    char abcZ(char input[26])
    {
         int count = 0;
         char letter[30] = "abcdefghijklmnopqrstuvwxyz";
         if(strcmp(input[count], letter[count]) == 0)
         {
              count ++;
              return letter letter;
         }
    }
    and in the function it gives me the same error, how do i compare specific array elements?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > ok here i realize i made some mistakes in typing the code last time i will show you the copied from the editor
    Apparently, this looks the same as last time, so I'm not going to waste any more bandwidth on you.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Location
    Silale, Lithuania
    Posts
    3
    It might be this what the young lad wants:

    Code:
    #include <iostream> 
    #include <cstring> 
    using namespace std;
    
    #define wait 900000
    
    char abcZ(char input[30], unsigned int& count);
    
    int main()
    { 
        char user[30];
        unsigned int count = 0; 
    
        cout << "Translate: ";
        cin.getline(user, 30);
        
         // Do "translation"(?) until we reach the end of user input
         do
         {
              cout << "abcZ returned: ";
              cout << abcZ(user, count) << endl;
         }
         while( count < strlen(user));
    
         return 0;
    }
    
    char abcZ(char input[30], unsigned int& count)
    {
         char letter[30] = "abcdefghijklmnopqrstuvwxyz";
    	 
         // For every char in letter array
         for ( unsigned int i = 0; i < strlen(letter); i++)
         {
              // See if current character in user input matches a character from input array
              if ( input[count] == letter[i])
              {
    		// If so return match
    		count++;
    		return letter[i];
              }
         }
    
         // If not - return whatever?
         count++;
         return char(' ');
    }
    Dunno, just a guess...
    Last edited by mcpunky; 08-20-2006 at 11:00 AM.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    I might be wrong, but I thought functions were supposed to go before main in the code?

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    More correctly, functions need to be defined before they are called. You could have function f() call function g(),
    but g() would need to be defined first, for instance. In a program of reasonable size this quickly becomes a pain so mcpunky wrote a prototype near the top:
    Code:
    char abcZ(char input[30], unsigned int& count);
    The compiler will see this and know exactly what function to call, therefore, he can write the function anywhere in the file.

  8. #8
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    edit: citizen typed what I wanted to say before I could say it and better to boot.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> More correctly, functions need to be defined before they are called.
    They need to be declared (also known as prototyped). They can be defined later.

  10. #10
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    ok so i did declare the function, and if you look closely both of my sc posts are different. and i will try. BUT y do i need to declare count in the function outside the array brackets?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> ok so i did declare the function
    You did, that conversation was about Dash_Riprock's post.

    >> if you look closely both of my sc posts are different.
    I looked closely, and I didn't see a difference. Besides, the point was that Salem gave you four things to look at, and you didn't change or respond to any of them.

    >> y do i need to declare count in the function outside the array brackets?
    You don't need to, mcpunky was apparently showing you one way to make sure count gets updated. Passing it by reference to the function that uses it allows the original variable to be updated.

    You shouldn't really be concerned about that now, I would suggest getting your code to compile first. Perhaps start with an empty main and then add things one at a time, compiling as you go so that you can catch all your errors before they pile up into a largely incorrect program.

  12. #12
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    thank you all i will post the ultimate program later, it is a pointless program but im trying to learn new things. ty again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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