Functions+Arrays+pointers

This is a discussion on Functions+Arrays+pointers within the C++ Programming forums, part of the General Programming Boards category; Well here is practically the same thing but using one pass by reference, the other function just uses a pass ...

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Well here is practically the same thing but using one pass by reference, the other function just uses a pass by value.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    
    using namespace std;
    using std::string;
    using std::cout;
    using std::endl;
    using std::cin;
    
    void GetUserInput(string &Input)
    
    {
        cout << "Please enter Amino Acid Sequence \"H,P\": " << endl;
        cin >> Input;
        cout << "Worked" << Input << endl;    
        cin.get();
    }
    
    void GetSetupString(string theInput)
    
    {
       int Output[50]; 
       theInput;
       int index;
       
       for (index = 0; index < 50; ++index)
       {
           switch (theInput[index])
           {
               case 'H':
                   Output[index] = 1; 
                   break;
               case 'P':
                   Output[index] = 0;
                   break;
               default:
                   break;
        
           }    
       }  
    
    cout << "New" << endl << Output << endl;
    
    
    
    cin.get();
    }    
    
    int main ()
    
    {
        string Input;
    
        GetUserInput(Input);
        GetSetupString(Input);
    
    }
    Included some Switch/Case which was supplied by Mario in another thread, though i edited some of it, still couldn't get it to work correctly.

    When it was setup as string Output = theInput, + switch (Output[index]) it just gave me gibberish. Maybe becauseof changing a char to an int i don't know.

    So i changed it to what it is now which just gives me amemory address, 0x22ef00 etc.

    any ideas what could cause it? if i'am allowed to ask that is?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    You have a potential array out of bounds error. theInput[index] is only valid if index < theInput.length(). Where does the 50 come from?
    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

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Ahhh, well i placed it in because i did the same thing in another for loop, thinking about it having "< 50" probably isn't a good idea considering it may continue on even when there is no data in it?

    know of a good way to keep "index < theInput.length(). "?

    If i move cout << "New" << endl << Output << endl; inside the loop i still get the mem error damn

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Cdrwolfe
    So i changed it to what it is now which just gives me amemory address, 0x22ef00 etc.

    any ideas what could cause it? if i'am allowed to ask that is?
    Yes, there is an overloaded version of the stream insertion operator (<<) that is called when outputting the content stored in character arrays/pointers. For integer arrays however, the computer does not know how to handle something like this:
    Code:
    int Output[50]; 
    ...
    cout << "New" << endl << Output << endl;
    The end result is that the compiler has to decide what you mean when you attempt to output an array to the console. The output you end up seeing is the address of the Output array itself. You'd have to output the individual array values one by one yourself in a loop.

    The stream insertion operator is given only the starting address/pointer of the array as an argument, no size/length parameter is passed along with this information. Character arrays/pointers can be resolved because there is an understanding that there should be a terminating character (the NULL) at some point which defines the stopping point. Given a starting address, the compiler can generate code that walks through the values starting at that address and continuing until it encounters the NULL terminating character. For an integer array, given only the starting address, the compiler cannot generate a loop that would ever stop at some terminating value because what value in an int array should be interpreted as a terminating value? There is none. Thus, the only safe thing the compiler can do is generate code to output the address in the above instance (it's not a memory error). That said, even character arrays that don't have a proper terminating NULL character can end up outputting gibberish to the screen when the code overruns the end of the array bounds.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks, thats some hard info, i seem to be told by some people i should read tutorials or books (as if i haven't ) but how i would have been able to find out what you said i don't know not in any tutorials i've read thats for sure .

    Appreciate the help from both of you.

    Before you replied i changed cout, to be << Output[index] which showed me it was atleast getting the switch statement right for the elements that were correct/inputted.

    Is there anyway to tell it to stop once it reaches the last char that was inputted.

    I know there is "/0" placed at the end to signal the end or something is there someway to tell it to stop when it reaches this character?

    Thank you.

  6. #21
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Right added Laser's "< theInput.length(). " to loop, why it works now when it didn't before bah who knows

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    I know there is "/0" placed at the end to signal the end or something is there someway to tell it to stop when it reaches this character?
    That applies to null terminated strings, not to arrays of int. I suggest that you use a vector<int> instead.

    An example would be:
    Code:
    void GetSetupString(string theInput)
    {
    	vector<int> output(theInput.length());
    	for (unsigned int i = 0; i < theInput.length(); ++i)
    	{
    		switch (theInput[i])
    		{
    		case 'H':
    			output[i] = 1;
    			break;
    		case 'P':
    			output[i] = 0;
    			break;
    		}
    	}
    }
    Though I dont know what you actually intend to do with the array, so my example doesnt really do anything constructive.
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  2. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 09:44 AM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

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