Thread: Outputting a string to the screen

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Outputting a string to the screen

    If you declare a string 32 bits long after you put thirty two bits in it it should only print those 32 bits correct???? It shouldn't print all these null characters then another 32 bit string??? sorry just venting a little

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>sorry just venting a little

    Is there a specific program you are having trouble with? If so post the code and we'll be more then happy to look it over...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It shouldn't print all these null characters then another 32 bit string???
    Not unless you are using a C string that you forgot to terminate with a nul character.

    -Prelude
    My best code is written with the delete key.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes, if you declare a string 32 elements long, you may access elements 0-31 (effectively 32 elements) You cannot access the 32nd element, as, in essence it's actually the 33rd element of your string. Remember, counting is zero-based.

    char *pszString = { "Hello" };

    pszString[0] now contains 'H'
    pszString[1] = 'e'
    pszString[2] = 'l'
    pszString[3] = 'l'
    pszString[4] = 'o'
    pszString[5] = '\0' Null terminator, overwrite this and you'll overflow when attemping to print the string

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    7
    OK here goes I am making a simulator and trying to make an and gate work in the ALU(I have the same problems with or gates, nor, etc)

    Code:
    void main()
    {
    
    	//ALU
    	char CRYOUT = '0';			//Carry out flag
    	char OVF = '0';				//Overflow flag
    	char ZERO = '0';				//Zero flag
    	char SGN = '0';				//Sign bit that comes of ALU, MSB
    	//ALU controls
    	char CTLALU[5];			//Control ALU
    	char CTLCIN = '0';			//Carry in
    
    	char AluInputA[33];		//Output saved from	MUXALUA and is input to ALU A
    	char AluInputB[33];		//Output saved from	MUXALUB and is input to ALU B
    	char AluOutput[33];		//Output saved from the ALU
    
    
    
    cout << "Testing ALU function." << endl;
    	ALU(AluInputA, AluInputB, CTLALU, CTLCIN, CRYOUT, OVF, ZERO, SGN, AluOutput);
    
    	cout << "And Output = " << AluOutput << endl;
    }
    
    
    //--------------------AND--------------------
    void and(char InputA[33], char InputB[33], char Output[33])
    {
    	for(int i = 31; i >= 0; i--)
    	{
    		if(InputA[i] == '1' && InputB[i] == '1')
    			Output[i] = '1';
    		else
    			Output[i] = '0';
    	}
    
    }
    The Output looks like this:

    The binary number in decimal is 0
    4 to 1 Multiplexer = 00000000000000000000000000000010
    MUXALUB
    The binary number in decimal is 3
    4 to 1 Multiplexer = 00000000000000000000000000001111
    Enter in ALU Control: 0001
    Testing ALU function.
    The binary number in decimal is 1
    Output: 00000000000000000000000000000010¨d¨d¨d¨d0000000000 0000000000000000001111

    Press any key to continue


    so as you see the first 32 bits is right but the other characters then InputB from the 4-1 mux is also in the string. I hope someone can help. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM